From f76f43793ea728a57b2e51b0ae245d3abf866512 Mon Sep 17 00:00:00 2001 From: Jared Crapo Date: Wed, 10 Jan 2018 23:18:38 -0700 Subject: switch from optparse to argparse --- examples/example.py | 51 ++++++++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 25 deletions(-) (limited to 'examples/example.py') diff --git a/examples/example.py b/examples/example.py index 889e62c0..d7d4e21c 100755 --- a/examples/example.py +++ b/examples/example.py @@ -12,8 +12,9 @@ the transcript. """ import random +import argparse -from cmd2 import Cmd, make_option, options, set_use_arg_list +from cmd2 import Cmd, with_argument_parser class CmdLineApp(Cmd): @@ -37,41 +38,41 @@ class CmdLineApp(Cmd): # Set use_ipython to True to enable the "ipy" command which embeds and interactive IPython shell Cmd.__init__(self, use_ipython=False) - # For option commands, pass a single argument string instead of a list of argument strings to the do_* methods - set_use_arg_list(False) - - opts = [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")] - - @options(opts, arg_desc='(text to say)') - def do_speak(self, arg, opts=None): + 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, 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() - repetitions = opts.repeat or 1 + 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.poutput(arg) - # recommend using the poutput function instead of - # self.stdout.write or "print", because Cmd allows the user - # to redirect output + # .poutput handles newlines, and accommodates output redirection too + 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 - @options([ make_option('-r', '--repeat', type="int", help="output [n] times") ]) - def do_mumble(self, arg, opts=None): + argparser = argparse.ArgumentParser() + argparser.add_argument('-r', '--repeat', type=int, help='how many times to repeat') + argparser.add_argument('words', nargs='+', help='words to say') + @with_argument_parser(argparser) + def do_mumble(self, cmdline, args=None): """Mumbles what you tell me to.""" - repetitions = opts.repeat or 1 - arg = arg.split() + repetitions = args.repeat or 1 for i in range(min(repetitions, self.maxrepeats)): output = [] if (random.random() < .33): output.append(random.choice(self.MUMBLE_FIRST)) - for word in arg: + for word in args.words: if (random.random() < .40): output.append(random.choice(self.MUMBLES)) output.append(word) -- 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/example.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) (limited to 'examples/example.py') diff --git a/examples/example.py b/examples/example.py index d7d4e21c..7c78edc3 100755 --- a/examples/example.py +++ b/examples/example.py @@ -38,12 +38,13 @@ class CmdLineApp(Cmd): # Set use_ipython to True to enable the "ipy" command which embeds and interactive IPython shell Cmd.__init__(self, use_ipython=False) - 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) + 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') + + @with_argument_parser(speak_parser) def do_speak(self, cmdline, opts=None): """Repeats what you tell me to.""" words = [] @@ -61,25 +62,27 @@ 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 - argparser = argparse.ArgumentParser() - argparser.add_argument('-r', '--repeat', type=int, help='how many times to repeat') - argparser.add_argument('words', nargs='+', help='words to say') - @with_argument_parser(argparser) + mumble_parser = argparse.ArgumentParser() + mumble_parser.add_argument('-r', '--repeat', type=int, help='how many times to repeat') + mumble_parser.add_argument('words', nargs='+', help='words to say') + + @with_argument_parser(mumble_parser) def do_mumble(self, cmdline, args=None): """Mumbles what you tell me to.""" repetitions = args.repeat or 1 for i in range(min(repetitions, self.maxrepeats)): output = [] - if (random.random() < .33): + if random.random() < .33: output.append(random.choice(self.MUMBLE_FIRST)) for word in args.words: - if (random.random() < .40): + if random.random() < .40: output.append(random.choice(self.MUMBLES)) output.append(word) - if (random.random() < .25): + if random.random() < .25: output.append(random.choice(self.MUMBLE_LAST)) self.poutput(' '.join(output)) + if __name__ == '__main__': c = CmdLineApp() c.cmdloop() -- 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/example.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'examples/example.py') diff --git a/examples/example.py b/examples/example.py index 7c78edc3..4ba0d29a 100755 --- a/examples/example.py +++ b/examples/example.py @@ -45,7 +45,7 @@ class CmdLineApp(Cmd): speak_parser.add_argument('words', nargs='+', help='words to say') @with_argument_parser(speak_parser) - def do_speak(self, cmdline, opts=None): + def do_speak(self, args): """Repeats what you tell me to.""" words = [] for word in args.words: @@ -67,7 +67,7 @@ class CmdLineApp(Cmd): mumble_parser.add_argument('words', nargs='+', help='words to say') @with_argument_parser(mumble_parser) - def do_mumble(self, cmdline, args=None): + def do_mumble(self, args): """Mumbles what you tell me to.""" repetitions = args.repeat or 1 for i in range(min(repetitions, self.maxrepeats)): -- 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/example.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'examples/example.py') diff --git a/examples/example.py b/examples/example.py index 4ba0d29a..c66f0e60 100755 --- a/examples/example.py +++ b/examples/example.py @@ -14,7 +14,7 @@ the transcript. import random import argparse -from cmd2 import Cmd, with_argument_parser +from cmd2 import Cmd, with_argparser class CmdLineApp(Cmd): @@ -44,7 +44,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 = [] @@ -66,7 +66,7 @@ class CmdLineApp(Cmd): mumble_parser.add_argument('-r', '--repeat', type=int, help='how many times to repeat') mumble_parser.add_argument('words', nargs='+', help='words to say') - @with_argument_parser(mumble_parser) + @with_argparser(mumble_parser) def do_mumble(self, args): """Mumbles what you tell me to.""" repetitions = args.repeat or 1 -- cgit v1.2.1