diff options
-rwxr-xr-x | cmd2.py | 6 | ||||
-rw-r--r-- | docs/argument_processing.rst | 18 | ||||
-rwxr-xr-x | examples/arg_print.py | 4 | ||||
-rwxr-xr-x | examples/argparse_example.py | 6 | ||||
-rwxr-xr-x | examples/example.py | 6 | ||||
-rwxr-xr-x | examples/pirate.py | 4 | ||||
-rwxr-xr-x | examples/subcommands.py | 12 | ||||
-rw-r--r-- | tests/test_argparse.py | 12 | ||||
-rw-r--r-- | tests/test_completion.py | 10 |
9 files changed, 39 insertions, 39 deletions
@@ -308,7 +308,7 @@ def with_argparser_and_unknown_args(argparser, subcommand_names=None): return arg_decorator -def with_argument_parser(argparser, subcommand_names=None): +def with_argparser(argparser, subcommand_names=None): """A decorator to alter a cmd2 method to populate its ``args`` argument by parsing arguments with the given instance of argparse.ArgumentParser. @@ -1463,7 +1463,7 @@ class Cmd(cmd.Cmd): set_parser.add_argument('-l', '--long', action='store_true', help='describe function of parameter') set_parser.add_argument('settable', nargs='*', help='[param_name] [value]') - @with_argument_parser(set_parser) + @with_argparser(set_parser) def do_set(self, args): """Sets a settable parameter or shows current settings of parameters. @@ -1825,7 +1825,7 @@ a..b, a:b, a:, ..b items by indices (inclusive) /regex/ items matching regular expression""" history_parser.add_argument('arg', nargs='?', help=_history_arg_help) - @with_argument_parser(history_parser) + @with_argparser(history_parser) def do_history(self, args): """View, run, edit, and save previously entered commands.""" # If an argument was supplied, then retrieve partial contents of the history diff --git a/docs/argument_processing.rst b/docs/argument_processing.rst index 98d622bc..2a433bc7 100644 --- a/docs/argument_processing.rst +++ b/docs/argument_processing.rst @@ -13,7 +13,7 @@ Argument Processing 4. Adds the usage message from the argument parser to your command. 5. Checks if the ``-h/--help`` option is present, and if so, display the help message for the command -These features are all provided by the ``@with_argument_parser`` decorator. +These features are all provided by the ``@with_argparser`` decorator. Using the argument parser decorator =================================== @@ -21,7 +21,7 @@ Using the argument parser decorator For each command in the ``cmd2`` subclass which requires argument parsing, create an instance of ``argparse.ArgumentParser()`` which can parse the input appropriately for the command. Then decorate the command method with -the ``@with_argument_parser`` decorator, passing the argument parser as the +the ``@with_argparser`` decorator, passing the argument parser as the first parameter to the decorator. This changes the second argumen to the command method, which will contain the results of ``ArgumentParser.parse_args()``. @@ -33,7 +33,7 @@ Here's what it looks like:: argparser.add_argument('-r', '--repeat', type=int, help='output [n] times') argparser.add_argument('word', nargs='?', help='word to say') - @with_argument_parser(argparser) + @with_argparser(argparser) def do_speak(self, opts) """Repeats what you tell me to.""" arg = opts.word @@ -47,7 +47,7 @@ Here's what it looks like:: .. note:: - The ``@with_argument_parser`` decorator sets the ``prog`` variable in + The ``@with_argparser`` decorator sets the ``prog`` variable in the argument parser based on the name of the method it is decorating. This will override anything you specify in ``prog`` variable when creating the argument parser. @@ -57,14 +57,14 @@ Help Messages ============= By default, cmd2 uses the docstring of the command method when a user asks -for help on the command. When you use the ``@with_argument_parser`` +for help on the command. When you use the ``@with_argparser`` decorator, the docstring for the ``do_*`` method is used to set the description for the ``argparse.ArgumentParser`` is With this code:: argparser = argparse.ArgumentParser() argparser.add_argument('tag', help='tag') argparser.add_argument('content', nargs='+', help='content to surround with tag') - @with_argument_parser(argparser) + @with_argparser(argparser) def do_tag(self, args): """create a html tag""" self.stdout.write('<{0}>{1}</{0}>'.format(args.tag, ' '.join(args.content))) @@ -92,7 +92,7 @@ docstring on your method empty:: argparser = argparse.ArgumentParser(description='create an html tag') argparser.add_argument('tag', help='tag') argparser.add_argument('content', nargs='+', help='content to surround with tag') - @with_argument_parser(argparser) + @with_argparser(argparser) def do_tag(self, args): self.stdout.write('<{0}>{1}</{0}>'.format(args.tag, ' '.join(args.content))) self.stdout.write('\n') @@ -121,7 +121,7 @@ To add additional text to the end of the generated help message, use the ``epilo ) argparser.add_argument('tag', help='tag') argparser.add_argument('content', nargs='+', help='content to surround with tag') - @with_argument_parser(argparser) + @with_argparser(argparser) def do_tag(self, args): self.stdout.write('<{0}>{1}</{0}>'.format(args.tag, ' '.join(args.content))) self.stdout.write('\n') @@ -190,7 +190,7 @@ Here's what it looks like:: Sub-commands ============ -Sub-commands are supported for commands using either the ``@with_argument_parser`` or +Sub-commands are supported for commands using either the ``@with_argparser`` or ``@with_argparser_and_unknown_args`` decorator. The syntax for supporting them is based on argparse sub-parsers. See the subcommands_ example to learn more about how to use sub-commands in your ``cmd2`` application. diff --git a/examples/arg_print.py b/examples/arg_print.py index 1b18cdf0..8b02bc51 100755 --- a/examples/arg_print.py +++ b/examples/arg_print.py @@ -14,7 +14,7 @@ import argparse import cmd2 import pyparsing -from cmd2 import with_argument_list, with_argument_parser, with_argparser_and_unknown_args +from cmd2 import with_argument_list, with_argparser, with_argparser_and_unknown_args class ArgumentAndOptionPrinter(cmd2.Cmd): @@ -47,7 +47,7 @@ class ArgumentAndOptionPrinter(cmd2.Cmd): oprint_parser.add_argument('-r', '--repeat', type=int, help='output [n] times') oprint_parser.add_argument('words', nargs='+', help='words to print') - @with_argument_parser(oprint_parser) + @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)) 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}</{0}>'.format(args.tag, ' '.join(args.content))) 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 diff --git a/examples/pirate.py b/examples/pirate.py index 06fdb61b..cfe545d6 100755 --- a/examples/pirate.py +++ b/examples/pirate.py @@ -7,7 +7,7 @@ presented as part of her PyCon 2010 talk. It demonstrates many features of cmd2. """ import argparse -from cmd2 import Cmd, with_argument_parser +from cmd2 import Cmd, with_argparser class Pirate(Cmd): @@ -78,7 +78,7 @@ class Pirate(Cmd): yo_parser.add_argument('-c', '--commas', action='store_true', help='Intersperse commas') yo_parser.add_argument('beverage', help='beverage to drink with the chant') - @with_argument_parser(yo_parser) + @with_argparser(yo_parser) def do_yo(self, args): """Compose a yo-ho-ho type chant with flexible options.""" chant = ['yo'] + ['ho'] * args.ho diff --git a/examples/subcommands.py b/examples/subcommands.py index 67b06e18..e77abc61 100755 --- a/examples/subcommands.py +++ b/examples/subcommands.py @@ -9,7 +9,7 @@ and provides separate contextual help. import argparse import cmd2 -from cmd2 import with_argument_parser +from cmd2 import with_argparser class SubcommandsExample(cmd2.Cmd): @@ -19,11 +19,11 @@ class SubcommandsExample(cmd2.Cmd): cmd2.Cmd.__init__(self) # sub-command functions for the base command - def foo(self, args): + def base_foo(self, args): """foo subcommand of base command""" self.poutput(args.x * args.y) - def bar(self, args): + def base_bar(self, args): """bar sucommand of base command""" self.poutput('((%s))' % args.z) @@ -35,17 +35,17 @@ class SubcommandsExample(cmd2.Cmd): parser_foo = base_subparsers.add_parser('foo', help='foo help') parser_foo.add_argument('-x', type=int, default=1, help='integer') parser_foo.add_argument('y', type=float, help='float') - parser_foo.set_defaults(func=foo) + parser_foo.set_defaults(func=base_foo) # create the parser for the "bar" sub-command parser_bar = base_subparsers.add_parser('bar', help='bar help') parser_bar.add_argument('z', help='string') - parser_bar.set_defaults(func=bar) + parser_bar.set_defaults(func=base_bar) # Create a list of subcommand names, which is used to enable tab-completion of sub-commands subcommands = ['foo', 'bar'] - @with_argument_parser(base_parser, subcommands) + @with_argparser(base_parser, subcommands) def do_base(self, args): """Base command help""" try: diff --git a/tests/test_argparse.py b/tests/test_argparse.py index bb494d49..ecaa1049 100644 --- a/tests/test_argparse.py +++ b/tests/test_argparse.py @@ -20,7 +20,7 @@ class ArgparseApp(cmd2.Cmd): say_parser.add_argument('-r', '--repeat', type=int, help='output [n] times') say_parser.add_argument('words', nargs='+', help='words to say') - @cmd2.with_argument_parser(say_parser) + @cmd2.with_argparser(say_parser) def do_say(self, args): """Repeat what you tell me to.""" words = [] @@ -41,7 +41,7 @@ class ArgparseApp(cmd2.Cmd): tag_parser.add_argument('tag', help='tag') tag_parser.add_argument('content', nargs='+', help='content to surround with tag') - @cmd2.with_argument_parser(tag_parser) + @cmd2.with_argparser(tag_parser) def do_tag(self, args): self.stdout.write('<{0}>{1}</{0}>'.format(args.tag, ' '.join(args.content))) self.stdout.write('\n') @@ -178,11 +178,11 @@ class SubcommandApp(cmd2.Cmd): cmd2.Cmd.__init__(self) # sub-command functions for the base command - def foo(self, args): + def base_foo(self, args): """foo subcommand of base command""" self.poutput(args.x * args.y) - def bar(self, args): + def base_bar(self, args): """bar sucommand of base command""" self.poutput('((%s))' % args.z) @@ -194,12 +194,12 @@ class SubcommandApp(cmd2.Cmd): parser_foo = base_subparsers.add_parser('foo', help='foo help') parser_foo.add_argument('-x', type=int, default=1, help='integer') parser_foo.add_argument('y', type=float, help='float') - parser_foo.set_defaults(func=foo) + parser_foo.set_defaults(func=base_foo) # create the parser for the "bar" sub-command parser_bar = base_subparsers.add_parser('bar', help='bar help') parser_bar.add_argument('z', help='string') - parser_bar.set_defaults(func=bar) + parser_bar.set_defaults(func=base_bar) # Create a list of subcommand names, which is used to enable tab-completion of sub-commands subcommands = ['foo', 'bar'] diff --git a/tests/test_completion.py b/tests/test_completion.py index f0efae62..05774df9 100644 --- a/tests/test_completion.py +++ b/tests/test_completion.py @@ -429,11 +429,11 @@ class SubcommandsExample(cmd2.Cmd): cmd2.Cmd.__init__(self) # sub-command functions for the base command - def foo(self, args): + def base_foo(self, args): """foo subcommand of base command""" self.poutput(args.x * args.y) - def bar(self, args): + def base_bar(self, args): """bar sucommand of base command""" self.poutput('((%s))' % args.z) @@ -445,17 +445,17 @@ class SubcommandsExample(cmd2.Cmd): parser_foo = base_subparsers.add_parser('foo', help='foo help') parser_foo.add_argument('-x', type=int, default=1, help='integer') parser_foo.add_argument('y', type=float, help='float') - parser_foo.set_defaults(func=foo) + parser_foo.set_defaults(func=base_foo) # create the parser for the "bar" sub-command parser_bar = base_subparsers.add_parser('bar', help='bar help') parser_bar.add_argument('z', help='string') - parser_bar.set_defaults(func=bar) + parser_bar.set_defaults(func=base_bar) # Create a list of subcommand names, which is used to enable tab-completion of sub-commands subcommands = ['foo', 'bar'] - @cmd2.with_argument_parser(base_parser, subcommands) + @cmd2.with_argparser(base_parser, subcommands) def do_base(self, args): """Base command help""" try: |