@@ -13,15 +13,15 @@ Argument Processing
13
13
4. Adds the usage message from the argument parser to your command.
14
14
5. Checks if the ``-h/--help `` option is present, and if so, display the help message for the command
15
15
16
- These features are all provided by the ``@with_argument_parser `` decorator.
16
+ These features are all provided by the ``@with_argparser `` decorator.
17
17
18
18
Using the argument parser decorator
19
19
===================================
20
20
21
21
For each command in the ``cmd2 `` subclass which requires argument parsing,
22
22
create an instance of ``argparse.ArgumentParser() `` which can parse the
23
23
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
25
25
first parameter to the decorator. This changes the second argumen to the command method, which will contain the results
26
26
of ``ArgumentParser.parse_args() ``.
27
27
@@ -33,7 +33,7 @@ Here's what it looks like::
33
33
argparser.add_argument('-r', '--repeat', type=int, help='output [n] times')
34
34
argparser.add_argument('word', nargs='?', help='word to say')
35
35
36
- @with_argument_parser (argparser)
36
+ @with_argparser (argparser)
37
37
def do_speak(self, opts)
38
38
"""Repeats what you tell me to."""
39
39
arg = opts.word
@@ -47,7 +47,7 @@ Here's what it looks like::
47
47
48
48
.. note ::
49
49
50
- The ``@with_argument_parser `` decorator sets the ``prog `` variable in
50
+ The ``@with_argparser `` decorator sets the ``prog `` variable in
51
51
the argument parser based on the name of the method it is decorating.
52
52
This will override anything you specify in ``prog `` variable when
53
53
creating the argument parser.
@@ -57,14 +57,14 @@ Help Messages
57
57
=============
58
58
59
59
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 ``
61
61
decorator, the docstring for the ``do_* `` method is used to set the description for the ``argparse.ArgumentParser `` is
62
62
With this code::
63
63
64
64
argparser = argparse.ArgumentParser()
65
65
argparser.add_argument('tag', help='tag')
66
66
argparser.add_argument('content', nargs='+', help='content to surround with tag')
67
- @with_argument_parser (argparser)
67
+ @with_argparser (argparser)
68
68
def do_tag(self, args):
69
69
"""create a html tag"""
70
70
self.stdout.write('<{0}>{1}</{0}>'.format(args.tag, ' '.join(args.content)))
@@ -92,7 +92,7 @@ docstring on your method empty::
92
92
argparser = argparse.ArgumentParser(description='create an html tag')
93
93
argparser.add_argument('tag', help='tag')
94
94
argparser.add_argument('content', nargs='+', help='content to surround with tag')
95
- @with_argument_parser (argparser)
95
+ @with_argparser (argparser)
96
96
def do_tag(self, args):
97
97
self.stdout.write('<{0}>{1}</{0}>'.format(args.tag, ' '.join(args.content)))
98
98
self.stdout.write('\n')
@@ -121,7 +121,7 @@ To add additional text to the end of the generated help message, use the ``epilo
121
121
)
122
122
argparser.add_argument('tag', help='tag')
123
123
argparser.add_argument('content', nargs='+', help='content to surround with tag')
124
- @with_argument_parser (argparser)
124
+ @with_argparser (argparser)
125
125
def do_tag(self, args):
126
126
self.stdout.write('<{0}>{1}</{0}>'.format(args.tag, ' '.join(args.content)))
127
127
self.stdout.write('\n')
@@ -190,7 +190,7 @@ Here's what it looks like::
190
190
191
191
Sub-commands
192
192
============
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
194
194
``@with_argparser_and_unknown_args `` decorator. The syntax for supporting them is based on argparse sub-parsers.
195
195
196
196
See the subcommands _ example to learn more about how to use sub-commands in your ``cmd2 `` application.
0 commit comments