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/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/example.py')
-rwxr-xr-x | examples/example.py | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/examples/example.py b/examples/example.py index d7d4e21c..4ba0d29a 100755 --- a/examples/example.py +++ b/examples/example.py @@ -38,13 +38,14 @@ 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) - def do_speak(self, cmdline, opts=None): + 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, args): """Repeats what you tell me to.""" words = [] for word in args.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) - def do_mumble(self, cmdline, args=None): + 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, args): """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() |