diff options
| author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-01-10 20:11:22 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-01-10 20:11:22 -0500 |
| commit | 38312dead63ea9d95cdb7f7b34a4d6030dbf0479 (patch) | |
| tree | 684cdc7a22290d4595d5a25caae0f433b89ba0e3 /examples | |
| parent | e6672fe278b877c82f8d011281b17dcf2654d257 (diff) | |
| parent | 73e7b149a34722504281d48bdb252e477625d4d0 (diff) | |
| download | cmd2-git-38312dead63ea9d95cdb7f7b34a4d6030dbf0479.tar.gz | |
Merge pull request #247 from python-cmd2/argparse
Argparse support
Diffstat (limited to 'examples')
| -rwxr-xr-x | examples/argparse_example.py | 77 |
1 files changed, 58 insertions, 19 deletions
diff --git a/examples/argparse_example.py b/examples/argparse_example.py index 6fc2b15b..4c5d6f59 100755 --- a/examples/argparse_example.py +++ b/examples/argparse_example.py @@ -1,19 +1,20 @@ #!/usr/bin/env python # coding=utf-8 -"""A sample application for cmd2 showing how to use Argparse to process command line arguments for your application. -It parses command line arguments looking for known arguments, but then still passes any unknown arguments onto cmd2 -to treat them as arguments at invocation. +"""A sample application for cmd2 showing how to use argparse to +process command line arguments for your application. -Thanks to cmd2's built-in transcript testing capability, it also serves as a test suite for argparse_example.py when -used with the exampleSession.txt transcript. +Thanks to cmd2's built-in transcript testing capability, it also +serves as a test suite for argparse_example.py when used with the +exampleSession.txt transcript. -Running `python argparse_example.py -t exampleSession.txt` will run all the commands in the transcript against -argparse_example.py, verifying that the output produced matches the transcript. +Running `python argparse_example.py -t exampleSession.txt` will run +all the commands in the transcript against argparse_example.py, +verifying that the output produced matches the transcript. """ import argparse import sys -from cmd2 import Cmd, make_option, options +from cmd2 import Cmd, make_option, options, with_argument_parser class CmdLineApp(Cmd): @@ -39,28 +40,66 @@ class CmdLineApp(Cmd): # Setting this true makes it run a shell command if a cmd2/cmd command doesn't exist # self.default_to_shell = True + + argparser = argparse.ArgumentParser(prog='speak') + argparser.add_argument('-p', '--piglatin', action='store_true', help='atinLay') + argparser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE') + argparser.add_argument('-r', '--repeat', type=int, help='output [n] times') + argparser.add_argument('words', nargs='+', help='words to say') + @with_argument_parser(argparser) + def do_speak(self, argv, args=None): + """Repeats what you tell me to.""" + words = [] + for word in args.words: + if args.piglatin: + word = '%s%say' % (word[1:], word[0]) + if args.shout: + word = word.upper() + words.append(word) + repetitions = args.repeat or 1 + for i in range(min(repetitions, self.maxrepeats)): + self.stdout.write(' '.join(words)) + self.stdout.write('\n') + # self.stdout.write is better than "print", because Cmd can be + # initialized with a non-standard output destination + + do_say = do_speak # now "say" is a synonym for "speak" + do_orate = do_speak # another synonym, but this one takes multi-line input + + + argparser = argparse.ArgumentParser(description='create a html tag') + argparser.add_argument('tag', nargs=1, help='tag') + argparser.add_argument('content', nargs='+', help='content to surround with tag') + @with_argument_parser(argparser) + def do_tag(self, argv, args=None): + self.stdout.write('<{0}>{1}</{0}>'.format(args.tag[0], ' '.join(args.content))) + self.stdout.write('\n') + # self.stdout.write is better than "print", because Cmd can be + # initialized with a non-standard output destination + + # @options uses the python optparse module which has been deprecated + # since 2011. Use @with_argument_parser instead, which utilizes the + # python argparse module @options([make_option('-p', '--piglatin', action="store_true", help="atinLay"), make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE"), make_option('-r', '--repeat', type="int", help="output [n] times") ]) - def do_speak(self, arg, opts=None): + def do_deprecated_speak(self, arg, opts=None): """Repeats what you tell me to.""" - arg = ''.join(arg) - if opts.piglatin: - arg = '%s%say' % (arg[1:], arg[0]) - if opts.shout: - arg = arg.upper() + words = [] + for word in arg: + if opts.piglatin: + word = '%s%say' % (word[1:], word[0]) + if opts.shout: + arg = arg.upper() + words.append(word) repetitions = opts.repeat or 1 for i in range(min(repetitions, self.maxrepeats)): - self.stdout.write(arg) + self.stdout.write(' '.join(words)) self.stdout.write('\n') # self.stdout.write is better than "print", because Cmd can be # initialized with a non-standard output destination - do_say = do_speak # now "say" is a synonym for "speak" - do_orate = do_speak # another synonym, but this one takes multi-line input - - if __name__ == '__main__': # You can do your custom Argparse parsing here to meet your application's needs parser = argparse.ArgumentParser(description='Process the arguments however you like.') |
