From 4f57dc14a7ffc2cbd741622da7d05d42ba7f7789 Mon Sep 17 00:00:00 2001 From: Jared Crapo Date: Mon, 11 Dec 2017 19:14:09 -0700 Subject: Plan and first working code for argparse decorator --- examples/argparse_example.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'examples/argparse_example.py') diff --git a/examples/argparse_example.py b/examples/argparse_example.py index 1358559c..8c25b0ef 100755 --- a/examples/argparse_example.py +++ b/examples/argparse_example.py @@ -11,7 +11,7 @@ argparse_example.py, verifying that the output produced matches the transcript. """ import argparse -from cmd2 import Cmd, make_option, options +from cmd2 import Cmd, make_option, options, with_argument_parser class CmdLineApp(Cmd): @@ -55,6 +55,30 @@ class CmdLineApp(Cmd): # self.stdout.write is better than "print", because Cmd can be # initialized with a non-standard output destination + argparser = argparse.ArgumentParser( + prog='sspeak', + description='Repeats what you tell me to' + ) + 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('word', nargs='?', help='word to say') + @with_argument_parser(argparser) + def do_sspeak(self, rawarg, args=None): + """Repeats what you tell me to.""" + word = args.word + if word is None: + word = '' + if args.piglatin: + word = '%s%say' % (word[1:], word[0]) + if args.shout: + word = word.upper() + repetitions = args.repeat or 1 + for i in range(min(repetitions, self.maxrepeats)): + self.stdout.write(word) + 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 -- cgit v1.2.1 From cc32105daa5c096a5c7fcd2b47729eebc871ebef Mon Sep 17 00:00:00 2001 From: kotfu Date: Sun, 7 Jan 2018 13:51:53 -0700 Subject: Default posix and quote removal working. --- examples/argparse_example.py | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) (limited to 'examples/argparse_example.py') diff --git a/examples/argparse_example.py b/examples/argparse_example.py index 805bab77..96f8118d 100755 --- a/examples/argparse_example.py +++ b/examples/argparse_example.py @@ -45,14 +45,16 @@ class CmdLineApp(Cmd): ]) def do_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 @@ -64,20 +66,20 @@ class CmdLineApp(Cmd): 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('word', nargs='?', help='word to say') + argparser.add_argument('words', nargs='+', help='words to say') @with_argument_parser(argparser) def do_sspeak(self, rawarg, args=None): """Repeats what you tell me to.""" - word = args.word - if word is None: - word = '' - if args.piglatin: - word = '%s%say' % (word[1:], word[0]) - if args.shout: - word = word.upper() + 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(word) + 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 -- cgit v1.2.1 From c25a2b7949c02449279f548db1c8de9d10214cdc Mon Sep 17 00:00:00 2001 From: kotfu Date: Sun, 7 Jan 2018 14:12:57 -0700 Subject: Add tests for POSIX=true and arguments containing spaces --- examples/argparse_example.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'examples/argparse_example.py') diff --git a/examples/argparse_example.py b/examples/argparse_example.py index 96f8118d..16de343c 100755 --- a/examples/argparse_example.py +++ b/examples/argparse_example.py @@ -59,6 +59,10 @@ class CmdLineApp(Cmd): # 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( prog='sspeak', description='Repeats what you tell me to' @@ -83,8 +87,20 @@ class CmdLineApp(Cmd): 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( + prog='tag', + description='create an html tag, the first argument is the tag, the rest is the contents' + ) + 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, cmdline, args=None): + self.stdout.write('<{0}>{1}'.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 if __name__ == '__main__': -- cgit v1.2.1 From 8c58bb558adceb8ff32c7a3e1a88d2a13371dbfa Mon Sep 17 00:00:00 2001 From: kotfu Date: Sun, 7 Jan 2018 18:30:34 -0700 Subject: Properly set docstring so it contains help message --- examples/argparse_example.py | 72 ++++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 36 deletions(-) (limited to 'examples/argparse_example.py') diff --git a/examples/argparse_example.py b/examples/argparse_example.py index 16de343c..c6a17435 100755 --- a/examples/argparse_example.py +++ b/examples/argparse_example.py @@ -1,14 +1,15 @@ #!/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 @@ -39,40 +40,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 - @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): - """Repeats what you tell me to.""" - 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(' '.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( - prog='sspeak', - description='Repeats what you tell me to' - ) + 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_sspeak(self, rawarg, args=None): + def do_speak(self, rawarg, args=None): """Repeats what you tell me to.""" words = [] for word in args.words: @@ -88,10 +63,13 @@ class CmdLineApp(Cmd): # 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( prog='tag', - description='create an html tag, the first argument is the tag, the rest is the contents' + description='create a html tag', ) argparser.add_argument('tag', nargs=1, help='tag') argparser.add_argument('content', nargs='+', help='content to surround with tag') @@ -102,6 +80,28 @@ class CmdLineApp(Cmd): # 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_deprecated_speak(self, arg, opts=None): + """Repeats what you tell me to.""" + 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(' '.join(words)) + self.stdout.write('\n') + # 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 -- cgit v1.2.1 From 6cb43c600f0b5d694201233ade6abd4f989d1730 Mon Sep 17 00:00:00 2001 From: kotfu Date: Sun, 7 Jan 2018 20:02:38 -0700 Subject: Set prog in argparser based on the name of the function --- examples/argparse_example.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'examples/argparse_example.py') diff --git a/examples/argparse_example.py b/examples/argparse_example.py index c6a17435..a387acc3 100755 --- a/examples/argparse_example.py +++ b/examples/argparse_example.py @@ -67,10 +67,7 @@ class CmdLineApp(Cmd): do_orate = do_speak # another synonym, but this one takes multi-line input - argparser = argparse.ArgumentParser( - prog='tag', - description='create a html tag', - ) + 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) -- cgit v1.2.1 From 5a28a87769e23578993c52cf9c5df1c9d9bb7f35 Mon Sep 17 00:00:00 2001 From: kotfu Date: Sun, 7 Jan 2018 20:18:55 -0700 Subject: Clean up variable names --- examples/argparse_example.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'examples/argparse_example.py') diff --git a/examples/argparse_example.py b/examples/argparse_example.py index a387acc3..4c5d6f59 100755 --- a/examples/argparse_example.py +++ b/examples/argparse_example.py @@ -47,7 +47,7 @@ class CmdLineApp(Cmd): 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, rawarg, args=None): + def do_speak(self, argv, args=None): """Repeats what you tell me to.""" words = [] for word in args.words: @@ -71,7 +71,7 @@ class CmdLineApp(Cmd): 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, cmdline, args=None): + def do_tag(self, argv, args=None): self.stdout.write('<{0}>{1}'.format(args.tag[0], ' '.join(args.content))) self.stdout.write('\n') # self.stdout.write is better than "print", because Cmd can be -- cgit v1.2.1 From 3607a39471eac8123e69b9416e6d77ce5d5b5ddf Mon Sep 17 00:00:00 2001 From: Jared Crapo Date: Wed, 10 Jan 2018 23:19:05 -0700 Subject: simplify output --- examples/argparse_example.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'examples/argparse_example.py') diff --git a/examples/argparse_example.py b/examples/argparse_example.py index 4c5d6f59..d784ccf5 100755 --- a/examples/argparse_example.py +++ b/examples/argparse_example.py @@ -41,13 +41,13 @@ class CmdLineApp(Cmd): # self.default_to_shell = True - argparser = argparse.ArgumentParser(prog='speak') + 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, argv, args=None): + def do_speak(self, cmdline, args=None): """Repeats what you tell me to.""" words = [] for word in args.words: @@ -58,10 +58,7 @@ class CmdLineApp(Cmd): 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 + self.poutput(' '.join(words)) do_say = do_speak # now "say" is a synonym for "speak" do_orate = do_speak # another synonym, but this one takes multi-line input -- cgit v1.2.1 From 19c0586b45204ac264038ce23da89858aefda46f Mon Sep 17 00:00:00 2001 From: kotfu Date: Fri, 12 Jan 2018 21:07:44 -0700 Subject: @with_argument_parser now passes an arglist instead of a string --- examples/argparse_example.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'examples/argparse_example.py') diff --git a/examples/argparse_example.py b/examples/argparse_example.py index d784ccf5..3574f549 100755 --- a/examples/argparse_example.py +++ b/examples/argparse_example.py @@ -47,7 +47,7 @@ class CmdLineApp(Cmd): 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): + def do_speak(self, arglist, args=None): """Repeats what you tell me to.""" words = [] for word in args.words: @@ -68,7 +68,7 @@ class CmdLineApp(Cmd): 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): + def do_tag(self, arglist, args=None): self.stdout.write('<{0}>{1}'.format(args.tag[0], ' '.join(args.content))) self.stdout.write('\n') # self.stdout.write is better than "print", because Cmd can be -- cgit v1.2.1 From c26b00853633c7df8cdee0ee49b3596154bb09c1 Mon Sep 17 00:00:00 2001 From: kotfu Date: Fri, 12 Jan 2018 22:28:29 -0700 Subject: new @with_argument_list decorator --- examples/argparse_example.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'examples/argparse_example.py') diff --git a/examples/argparse_example.py b/examples/argparse_example.py index 3574f549..40ea372c 100755 --- a/examples/argparse_example.py +++ b/examples/argparse_example.py @@ -14,7 +14,7 @@ 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, make_option, options, with_argument_parser, with_argument_list class CmdLineApp(Cmd): @@ -69,10 +69,20 @@ class CmdLineApp(Cmd): argparser.add_argument('content', nargs='+', help='content to surround with tag') @with_argument_parser(argparser) def do_tag(self, arglist, args=None): - self.stdout.write('<{0}>{1}'.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 + """create a html tag""" + self.poutput('<{0}>{1}'.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}'.format(tag, ' '.join(content))) + else: + self.perror("tagg requires at least 2 arguments") + # @options uses the python optparse module which has been deprecated # since 2011. Use @with_argument_parser instead, which utilizes the -- cgit v1.2.1 From 141d95194b30d959f6c21f4546100551c442b13d Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Mon, 15 Jan 2018 01:34:50 -0500 Subject: Made a couple cleanup changes 1) cmd2 no longer imports make_option from optparse - test files and examples now import this directly - this helps emphasize that this old optparse methodology of adding options to commands is deprecated 2) All argparsers have been given custom names instead of just "argparser" - this helps with readability and maintainability, especially with IDE renaming and such --- examples/argparse_example.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'examples/argparse_example.py') diff --git a/examples/argparse_example.py b/examples/argparse_example.py index 40ea372c..b203feef 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, with_argument_list +from cmd2 import Cmd, options, with_argument_parser, with_argument_list +from optparse import make_option class CmdLineApp(Cmd): @@ -40,13 +41,13 @@ 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) + @with_argument_parser(speak_parser) def do_speak(self, arglist, args=None): """Repeats what you tell me to.""" words = [] @@ -63,11 +64,11 @@ 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') - 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) + @with_argument_parser(tag_parser) def do_tag(self, arglist, args=None): """create a html tag""" self.poutput('<{0}>{1}'.format(args.tag[0], ' '.join(args.content))) @@ -107,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.') -- cgit v1.2.1 From eee2d621abfb3d6455570b540069a4853a68f8c6 Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Mon, 15 Jan 2018 12:11:12 -0500 Subject: Changed @with_argument_parser to only pass single argument to commands Also added another @with_argparser_and_list decorator that uses argparse.parse_known_args to pass two arguments to a command: both the argparse output and a list of unknown/unmatched args. --- examples/argparse_example.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'examples/argparse_example.py') diff --git a/examples/argparse_example.py b/examples/argparse_example.py index b203feef..ae45411c 100755 --- a/examples/argparse_example.py +++ b/examples/argparse_example.py @@ -48,7 +48,7 @@ class CmdLineApp(Cmd): speak_parser.add_argument('words', nargs='+', help='words to say') @with_argument_parser(speak_parser) - def do_speak(self, arglist, args=None): + def do_speak(self, args): """Repeats what you tell me to.""" words = [] for word in args.words: @@ -69,7 +69,7 @@ class CmdLineApp(Cmd): tag_parser.add_argument('content', nargs='+', help='content to surround with tag') @with_argument_parser(tag_parser) - def do_tag(self, arglist, args=None): + def do_tag(self, args): """create a html tag""" self.poutput('<{0}>{1}'.format(args.tag[0], ' '.join(args.content))) -- cgit v1.2.1 From b034f6d2de92a784853ddeef4e51b26148056691 Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Wed, 17 Jan 2018 13:48:13 -0500 Subject: Improved how new argparse-based decorators provide help Now "help command_name" and "command_name -h" provide exactly the same text. The function docstring for the "do_*" command sets and overrides the ArgumentParser "description" if the docstring is not empty. --- examples/argparse_example.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/argparse_example.py') diff --git a/examples/argparse_example.py b/examples/argparse_example.py index ae45411c..48adca52 100755 --- a/examples/argparse_example.py +++ b/examples/argparse_example.py @@ -64,7 +64,7 @@ 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 = argparse.ArgumentParser() tag_parser.add_argument('tag', nargs=1, help='tag') tag_parser.add_argument('content', nargs='+', help='content to surround with tag') -- cgit v1.2.1 From 03f15696076b04a2881562a0429a435e61ffd92c Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Wed, 17 Jan 2018 21:43:20 -0500 Subject: Simplified a few argparse examples and fixed some incorrect documentation I eliminated a few "narg=1" configurations so that a single str value is returned instead of a List[str]. I also reworded some documentation which was no longer correct after the last commit which made "history command" have the same help text as "command -h" when using one of the two argparse decorators. --- examples/argparse_example.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'examples/argparse_example.py') diff --git a/examples/argparse_example.py b/examples/argparse_example.py index 48adca52..9f6548de 100755 --- a/examples/argparse_example.py +++ b/examples/argparse_example.py @@ -65,13 +65,13 @@ class CmdLineApp(Cmd): do_orate = do_speak # another synonym, but this one takes multi-line input tag_parser = argparse.ArgumentParser() - tag_parser.add_argument('tag', nargs=1, help='tag') + tag_parser.add_argument('tag', 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}'.format(args.tag[0], ' '.join(args.content))) + self.poutput('<{0}>{1}'.format(args.tag, ' '.join(args.content))) @with_argument_list -- cgit v1.2.1 From c9f7c012bda012b4df7a8c5e853bd5d3e6d99b1b Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Sun, 21 Jan 2018 22:24:09 -0500 Subject: Renamed @with_argument_parser decorator to @with_argparser Also: - Reanamed foo and bar subcommand methods to base_foo and base_bar --- examples/argparse_example.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'examples/argparse_example.py') diff --git a/examples/argparse_example.py b/examples/argparse_example.py index 9f6548de..fbb2b1dc 100755 --- a/examples/argparse_example.py +++ b/examples/argparse_example.py @@ -14,7 +14,7 @@ verifying that the output produced matches the transcript. import argparse import sys -from cmd2 import Cmd, options, with_argument_parser, with_argument_list +from cmd2 import Cmd, options, with_argparser, with_argument_list from optparse import make_option @@ -47,7 +47,7 @@ class CmdLineApp(Cmd): speak_parser.add_argument('-r', '--repeat', type=int, help='output [n] times') speak_parser.add_argument('words', nargs='+', help='words to say') - @with_argument_parser(speak_parser) + @with_argparser(speak_parser) def do_speak(self, args): """Repeats what you tell me to.""" words = [] @@ -68,7 +68,7 @@ class CmdLineApp(Cmd): tag_parser.add_argument('tag', help='tag') tag_parser.add_argument('content', nargs='+', help='content to surround with tag') - @with_argument_parser(tag_parser) + @with_argparser(tag_parser) def do_tag(self, args): """create a html tag""" self.poutput('<{0}>{1}'.format(args.tag, ' '.join(args.content))) -- cgit v1.2.1