diff options
Diffstat (limited to 'examples/example.py')
-rwxr-xr-x | examples/example.py | 58 |
1 files changed, 31 insertions, 27 deletions
diff --git a/examples/example.py b/examples/example.py index 889e62c0..c66f0e60 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_argparser class CmdLineApp(Cmd): @@ -37,48 +38,51 @@ 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) + 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') - 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): + @with_argparser(speak_parser) + def do_speak(self, args): """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): + 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_argparser(mumble_parser) + def do_mumble(self, args): """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): + if random.random() < .33: output.append(random.choice(self.MUMBLE_FIRST)) - for word in arg: - if (random.random() < .40): + for word in args.words: + 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() |