diff options
Diffstat (limited to 'examples/arg_print.py')
-rwxr-xr-x | examples/arg_print.py | 38 |
1 files changed, 30 insertions, 8 deletions
diff --git a/examples/arg_print.py b/examples/arg_print.py index 849cf386..8b02bc51 100755 --- a/examples/arg_print.py +++ b/examples/arg_print.py @@ -9,9 +9,12 @@ and argument parsing is intended to work. It also serves as an example of how to create command aliases (shortcuts). """ -import pyparsing +import argparse + import cmd2 -from cmd2 import options, make_option +import pyparsing + +from cmd2 import with_argument_list, with_argparser, with_argparser_and_unknown_args class ArgumentAndOptionPrinter(cmd2.Cmd): @@ -22,7 +25,7 @@ class ArgumentAndOptionPrinter(cmd2.Cmd): # self.commentGrammars = pyparsing.Or([pyparsing.cStyleComment]) # Create command aliases which are shorter - self.shortcuts.update({'ap': 'aprint', 'op': 'oprint'}) + self.shortcuts.update({'$': 'aprint', '%': 'oprint'}) # Make sure to call this super class __init__ *after* setting commentGrammars and/or updating shortcuts cmd2.Cmd.__init__(self) @@ -33,12 +36,31 @@ class ArgumentAndOptionPrinter(cmd2.Cmd): """Print the argument string this basic command is called with.""" print('aprint was called with argument: {!r}'.format(arg)) - @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")], arg_desc='positional_arg_string') - def do_oprint(self, arg, opts=None): + @with_argument_list + def do_lprint(self, arglist): + """Print the argument list this basic command is called with.""" + print('lprint was called with the following list of arguments: {!r}'.format(arglist)) + + oprint_parser = argparse.ArgumentParser() + oprint_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay') + oprint_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE') + oprint_parser.add_argument('-r', '--repeat', type=int, help='output [n] times') + oprint_parser.add_argument('words', nargs='+', help='words to print') + + @with_argparser(oprint_parser) + def do_oprint(self, args): + """Print the options and argument list this options command was called with.""" + print('oprint was called with the following\n\toptions: {!r}'.format(args)) + + pprint_parser = argparse.ArgumentParser() + pprint_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay') + pprint_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE') + pprint_parser.add_argument('-r', '--repeat', type=int, help='output [n] times') + @with_argparser_and_unknown_args(pprint_parser) + def do_pprint(self, args, unknown): """Print the options and argument list this options command was called with.""" - print('oprint was called with the following\n\toptions: {!r}\n\targuments: {!r}'.format(opts, arg)) + print('oprint was called with the following\n\toptions: {!r}\n\targuments: {}'.format(args, unknown)) + if __name__ == '__main__': |