diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-01-17 09:28:43 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-17 09:28:43 -0500 |
commit | 91bc999d022a486334b9054859e2ef6f03ca9666 (patch) | |
tree | d71c32f3b47c2b137d6b9543a605feb8380472a7 /examples/argparse_example.py | |
parent | 6895dea2e19210093e38fa411ef28dfb7f99c32c (diff) | |
parent | 1da904585db4cd1e77c312190149daa946366fa0 (diff) | |
download | cmd2-git-91bc999d022a486334b9054859e2ef6f03ca9666.tar.gz |
Merge pull request #249 from python-cmd2/arglist
decorator changes for replacing optparse with argparse
Diffstat (limited to 'examples/argparse_example.py')
-rwxr-xr-x | examples/argparse_example.py | 46 |
1 files changed, 29 insertions, 17 deletions
diff --git a/examples/argparse_example.py b/examples/argparse_example.py index d784ccf5..ae45411c 100755 --- a/examples/argparse_example.py +++ b/examples/argparse_example.py @@ -14,7 +14,8 @@ verifying that the output produced matches the transcript. import argparse import sys -from cmd2 import Cmd, make_option, options, with_argument_parser +from cmd2 import Cmd, options, with_argument_parser, with_argument_list +from optparse import make_option class CmdLineApp(Cmd): @@ -40,14 +41,14 @@ 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 + speak_parser = argparse.ArgumentParser() + speak_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay') + speak_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE') + speak_parser.add_argument('-r', '--repeat', type=int, help='output [n] times') + speak_parser.add_argument('words', nargs='+', help='words to say') - argparser = argparse.ArgumentParser() - 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, cmdline, args=None): + @with_argument_parser(speak_parser) + def do_speak(self, args): """Repeats what you tell me to.""" words = [] for word in args.words: @@ -63,16 +64,26 @@ class CmdLineApp(Cmd): do_say = do_speak # now "say" is a synonym for "speak" do_orate = do_speak # another synonym, but this one takes multi-line input + tag_parser = argparse.ArgumentParser(description='create a html tag') + tag_parser.add_argument('tag', nargs=1, help='tag') + tag_parser.add_argument('content', nargs='+', help='content to surround with tag') + + @with_argument_parser(tag_parser) + def do_tag(self, args): + """create a html tag""" + self.poutput('<{0}>{1}</{0}>'.format(args.tag[0], ' '.join(args.content))) + + + @with_argument_list + def do_tagg(self, arglist): + """verion of creating an html tag using arglist instead of argparser""" + if len(arglist) >= 2: + tag = arglist[0] + content = arglist[1:] + self.poutput('<{0}>{1}</{0}>'.format(tag, ' '.join(content))) + else: + self.perror("tagg requires at least 2 arguments") - 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 @@ -97,6 +108,7 @@ class CmdLineApp(Cmd): # self.stdout.write is better than "print", because Cmd can be # initialized with a non-standard output destination + 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.') |