Skip to content

Commit c9f7c01

Browse files
committed
Renamed @with_argument_parser decorator to @with_argparser
Also: - Reanamed foo and bar subcommand methods to base_foo and base_bar
1 parent 711d638 commit c9f7c01

File tree

9 files changed

+39
-39
lines changed

9 files changed

+39
-39
lines changed

cmd2.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ def cmd_wrapper(instance, cmdline):
308308
return arg_decorator
309309

310310

311-
def with_argument_parser(argparser, subcommand_names=None):
311+
def with_argparser(argparser, subcommand_names=None):
312312
"""A decorator to alter a cmd2 method to populate its ``args`` argument by parsing arguments
313313
with the given instance of argparse.ArgumentParser.
314314
@@ -1463,7 +1463,7 @@ def show(self, args, parameter):
14631463
set_parser.add_argument('-l', '--long', action='store_true', help='describe function of parameter')
14641464
set_parser.add_argument('settable', nargs='*', help='[param_name] [value]')
14651465

1466-
@with_argument_parser(set_parser)
1466+
@with_argparser(set_parser)
14671467
def do_set(self, args):
14681468
"""Sets a settable parameter or shows current settings of parameters.
14691469
@@ -1825,7 +1825,7 @@ def do_ipy(self, arg):
18251825
/regex/ items matching regular expression"""
18261826
history_parser.add_argument('arg', nargs='?', help=_history_arg_help)
18271827

1828-
@with_argument_parser(history_parser)
1828+
@with_argparser(history_parser)
18291829
def do_history(self, args):
18301830
"""View, run, edit, and save previously entered commands."""
18311831
# If an argument was supplied, then retrieve partial contents of the history

docs/argument_processing.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ Argument Processing
1313
4. Adds the usage message from the argument parser to your command.
1414
5. Checks if the ``-h/--help`` option is present, and if so, display the help message for the command
1515

16-
These features are all provided by the ``@with_argument_parser`` decorator.
16+
These features are all provided by the ``@with_argparser`` decorator.
1717

1818
Using the argument parser decorator
1919
===================================
2020

2121
For each command in the ``cmd2`` subclass which requires argument parsing,
2222
create an instance of ``argparse.ArgumentParser()`` which can parse the
2323
input appropriately for the command. Then decorate the command method with
24-
the ``@with_argument_parser`` decorator, passing the argument parser as the
24+
the ``@with_argparser`` decorator, passing the argument parser as the
2525
first parameter to the decorator. This changes the second argumen to the command method, which will contain the results
2626
of ``ArgumentParser.parse_args()``.
2727

@@ -33,7 +33,7 @@ Here's what it looks like::
3333
argparser.add_argument('-r', '--repeat', type=int, help='output [n] times')
3434
argparser.add_argument('word', nargs='?', help='word to say')
3535

36-
@with_argument_parser(argparser)
36+
@with_argparser(argparser)
3737
def do_speak(self, opts)
3838
"""Repeats what you tell me to."""
3939
arg = opts.word
@@ -47,7 +47,7 @@ Here's what it looks like::
4747

4848
.. note::
4949

50-
The ``@with_argument_parser`` decorator sets the ``prog`` variable in
50+
The ``@with_argparser`` decorator sets the ``prog`` variable in
5151
the argument parser based on the name of the method it is decorating.
5252
This will override anything you specify in ``prog`` variable when
5353
creating the argument parser.
@@ -57,14 +57,14 @@ Help Messages
5757
=============
5858

5959
By default, cmd2 uses the docstring of the command method when a user asks
60-
for help on the command. When you use the ``@with_argument_parser``
60+
for help on the command. When you use the ``@with_argparser``
6161
decorator, the docstring for the ``do_*`` method is used to set the description for the ``argparse.ArgumentParser`` is
6262
With this code::
6363

6464
argparser = argparse.ArgumentParser()
6565
argparser.add_argument('tag', help='tag')
6666
argparser.add_argument('content', nargs='+', help='content to surround with tag')
67-
@with_argument_parser(argparser)
67+
@with_argparser(argparser)
6868
def do_tag(self, args):
6969
"""create a html tag"""
7070
self.stdout.write('<{0}>{1}</{0}>'.format(args.tag, ' '.join(args.content)))
@@ -92,7 +92,7 @@ docstring on your method empty::
9292
argparser = argparse.ArgumentParser(description='create an html tag')
9393
argparser.add_argument('tag', help='tag')
9494
argparser.add_argument('content', nargs='+', help='content to surround with tag')
95-
@with_argument_parser(argparser)
95+
@with_argparser(argparser)
9696
def do_tag(self, args):
9797
self.stdout.write('<{0}>{1}</{0}>'.format(args.tag, ' '.join(args.content)))
9898
self.stdout.write('\n')
@@ -121,7 +121,7 @@ To add additional text to the end of the generated help message, use the ``epilo
121121
)
122122
argparser.add_argument('tag', help='tag')
123123
argparser.add_argument('content', nargs='+', help='content to surround with tag')
124-
@with_argument_parser(argparser)
124+
@with_argparser(argparser)
125125
def do_tag(self, args):
126126
self.stdout.write('<{0}>{1}</{0}>'.format(args.tag, ' '.join(args.content)))
127127
self.stdout.write('\n')
@@ -190,7 +190,7 @@ Here's what it looks like::
190190

191191
Sub-commands
192192
============
193-
Sub-commands are supported for commands using either the ``@with_argument_parser`` or
193+
Sub-commands are supported for commands using either the ``@with_argparser`` or
194194
``@with_argparser_and_unknown_args`` decorator. The syntax for supporting them is based on argparse sub-parsers.
195195

196196
See the subcommands_ example to learn more about how to use sub-commands in your ``cmd2`` application.

examples/arg_print.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import cmd2
1515
import pyparsing
1616

17-
from cmd2 import with_argument_list, with_argument_parser, with_argparser_and_unknown_args
17+
from cmd2 import with_argument_list, with_argparser, with_argparser_and_unknown_args
1818

1919

2020
class ArgumentAndOptionPrinter(cmd2.Cmd):
@@ -47,7 +47,7 @@ def do_lprint(self, arglist):
4747
oprint_parser.add_argument('-r', '--repeat', type=int, help='output [n] times')
4848
oprint_parser.add_argument('words', nargs='+', help='words to print')
4949

50-
@with_argument_parser(oprint_parser)
50+
@with_argparser(oprint_parser)
5151
def do_oprint(self, args):
5252
"""Print the options and argument list this options command was called with."""
5353
print('oprint was called with the following\n\toptions: {!r}'.format(args))

examples/argparse_example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import argparse
1515
import sys
1616

17-
from cmd2 import Cmd, options, with_argument_parser, with_argument_list
17+
from cmd2 import Cmd, options, with_argparser, with_argument_list
1818
from optparse import make_option
1919

2020

@@ -47,7 +47,7 @@ def __init__(self, ip_addr=None, port=None, transcript_files=None):
4747
speak_parser.add_argument('-r', '--repeat', type=int, help='output [n] times')
4848
speak_parser.add_argument('words', nargs='+', help='words to say')
4949

50-
@with_argument_parser(speak_parser)
50+
@with_argparser(speak_parser)
5151
def do_speak(self, args):
5252
"""Repeats what you tell me to."""
5353
words = []
@@ -68,7 +68,7 @@ def do_speak(self, args):
6868
tag_parser.add_argument('tag', help='tag')
6969
tag_parser.add_argument('content', nargs='+', help='content to surround with tag')
7070

71-
@with_argument_parser(tag_parser)
71+
@with_argparser(tag_parser)
7272
def do_tag(self, args):
7373
"""create a html tag"""
7474
self.poutput('<{0}>{1}</{0}>'.format(args.tag, ' '.join(args.content)))

examples/example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import random
1515
import argparse
1616

17-
from cmd2 import Cmd, with_argument_parser
17+
from cmd2 import Cmd, with_argparser
1818

1919

2020
class CmdLineApp(Cmd):
@@ -44,7 +44,7 @@ def __init__(self):
4444
speak_parser.add_argument('-r', '--repeat', type=int, help='output [n] times')
4545
speak_parser.add_argument('words', nargs='+', help='words to say')
4646

47-
@with_argument_parser(speak_parser)
47+
@with_argparser(speak_parser)
4848
def do_speak(self, args):
4949
"""Repeats what you tell me to."""
5050
words = []
@@ -66,7 +66,7 @@ def do_speak(self, args):
6666
mumble_parser.add_argument('-r', '--repeat', type=int, help='how many times to repeat')
6767
mumble_parser.add_argument('words', nargs='+', help='words to say')
6868

69-
@with_argument_parser(mumble_parser)
69+
@with_argparser(mumble_parser)
7070
def do_mumble(self, args):
7171
"""Mumbles what you tell me to."""
7272
repetitions = args.repeat or 1

examples/pirate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
It demonstrates many features of cmd2.
88
"""
99
import argparse
10-
from cmd2 import Cmd, with_argument_parser
10+
from cmd2 import Cmd, with_argparser
1111

1212

1313
class Pirate(Cmd):
@@ -78,7 +78,7 @@ def do_sing(self, arg):
7878
yo_parser.add_argument('-c', '--commas', action='store_true', help='Intersperse commas')
7979
yo_parser.add_argument('beverage', help='beverage to drink with the chant')
8080

81-
@with_argument_parser(yo_parser)
81+
@with_argparser(yo_parser)
8282
def do_yo(self, args):
8383
"""Compose a yo-ho-ho type chant with flexible options."""
8484
chant = ['yo'] + ['ho'] * args.ho

examples/subcommands.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import argparse
1010

1111
import cmd2
12-
from cmd2 import with_argument_parser
12+
from cmd2 import with_argparser
1313

1414

1515
class SubcommandsExample(cmd2.Cmd):
@@ -19,11 +19,11 @@ def __init__(self):
1919
cmd2.Cmd.__init__(self)
2020

2121
# sub-command functions for the base command
22-
def foo(self, args):
22+
def base_foo(self, args):
2323
"""foo subcommand of base command"""
2424
self.poutput(args.x * args.y)
2525

26-
def bar(self, args):
26+
def base_bar(self, args):
2727
"""bar sucommand of base command"""
2828
self.poutput('((%s))' % args.z)
2929

@@ -35,17 +35,17 @@ def bar(self, args):
3535
parser_foo = base_subparsers.add_parser('foo', help='foo help')
3636
parser_foo.add_argument('-x', type=int, default=1, help='integer')
3737
parser_foo.add_argument('y', type=float, help='float')
38-
parser_foo.set_defaults(func=foo)
38+
parser_foo.set_defaults(func=base_foo)
3939

4040
# create the parser for the "bar" sub-command
4141
parser_bar = base_subparsers.add_parser('bar', help='bar help')
4242
parser_bar.add_argument('z', help='string')
43-
parser_bar.set_defaults(func=bar)
43+
parser_bar.set_defaults(func=base_bar)
4444

4545
# Create a list of subcommand names, which is used to enable tab-completion of sub-commands
4646
subcommands = ['foo', 'bar']
4747

48-
@with_argument_parser(base_parser, subcommands)
48+
@with_argparser(base_parser, subcommands)
4949
def do_base(self, args):
5050
"""Base command help"""
5151
try:

tests/test_argparse.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def __init__(self):
2020
say_parser.add_argument('-r', '--repeat', type=int, help='output [n] times')
2121
say_parser.add_argument('words', nargs='+', help='words to say')
2222

23-
@cmd2.with_argument_parser(say_parser)
23+
@cmd2.with_argparser(say_parser)
2424
def do_say(self, args):
2525
"""Repeat what you tell me to."""
2626
words = []
@@ -41,7 +41,7 @@ def do_say(self, args):
4141
tag_parser.add_argument('tag', help='tag')
4242
tag_parser.add_argument('content', nargs='+', help='content to surround with tag')
4343

44-
@cmd2.with_argument_parser(tag_parser)
44+
@cmd2.with_argparser(tag_parser)
4545
def do_tag(self, args):
4646
self.stdout.write('<{0}>{1}</{0}>'.format(args.tag, ' '.join(args.content)))
4747
self.stdout.write('\n')
@@ -178,11 +178,11 @@ def __init__(self):
178178
cmd2.Cmd.__init__(self)
179179

180180
# sub-command functions for the base command
181-
def foo(self, args):
181+
def base_foo(self, args):
182182
"""foo subcommand of base command"""
183183
self.poutput(args.x * args.y)
184184

185-
def bar(self, args):
185+
def base_bar(self, args):
186186
"""bar sucommand of base command"""
187187
self.poutput('((%s))' % args.z)
188188

@@ -194,12 +194,12 @@ def bar(self, args):
194194
parser_foo = base_subparsers.add_parser('foo', help='foo help')
195195
parser_foo.add_argument('-x', type=int, default=1, help='integer')
196196
parser_foo.add_argument('y', type=float, help='float')
197-
parser_foo.set_defaults(func=foo)
197+
parser_foo.set_defaults(func=base_foo)
198198

199199
# create the parser for the "bar" sub-command
200200
parser_bar = base_subparsers.add_parser('bar', help='bar help')
201201
parser_bar.add_argument('z', help='string')
202-
parser_bar.set_defaults(func=bar)
202+
parser_bar.set_defaults(func=base_bar)
203203

204204
# Create a list of subcommand names, which is used to enable tab-completion of sub-commands
205205
subcommands = ['foo', 'bar']

tests/test_completion.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -429,11 +429,11 @@ def __init__(self):
429429
cmd2.Cmd.__init__(self)
430430

431431
# sub-command functions for the base command
432-
def foo(self, args):
432+
def base_foo(self, args):
433433
"""foo subcommand of base command"""
434434
self.poutput(args.x * args.y)
435435

436-
def bar(self, args):
436+
def base_bar(self, args):
437437
"""bar sucommand of base command"""
438438
self.poutput('((%s))' % args.z)
439439

@@ -445,17 +445,17 @@ def bar(self, args):
445445
parser_foo = base_subparsers.add_parser('foo', help='foo help')
446446
parser_foo.add_argument('-x', type=int, default=1, help='integer')
447447
parser_foo.add_argument('y', type=float, help='float')
448-
parser_foo.set_defaults(func=foo)
448+
parser_foo.set_defaults(func=base_foo)
449449

450450
# create the parser for the "bar" sub-command
451451
parser_bar = base_subparsers.add_parser('bar', help='bar help')
452452
parser_bar.add_argument('z', help='string')
453-
parser_bar.set_defaults(func=bar)
453+
parser_bar.set_defaults(func=base_bar)
454454

455455
# Create a list of subcommand names, which is used to enable tab-completion of sub-commands
456456
subcommands = ['foo', 'bar']
457457

458-
@cmd2.with_argument_parser(base_parser, subcommands)
458+
@cmd2.with_argparser(base_parser, subcommands)
459459
def do_base(self, args):
460460
"""Base command help"""
461461
try:

0 commit comments

Comments
 (0)