Skip to content

Commit e60cc37

Browse files
committed
Improved argument processing docs
Improved the documentation related to how to use the argparse decorators. Also: - Fixed a comment in cmd2.py which referred to the old decorator before the rename - Fixed README.md which had the old decorator name in it prior to the rename
1 parent 2cdeb5c commit e60cc37

3 files changed

Lines changed: 55 additions & 28 deletions

File tree

README.md

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,15 @@ Instructions for implementing each feature follow.
108108

109109
- Parsing commands with `argparse`
110110

111-
```python
111+
```Python
112+
import argparse
113+
from cmd2 import with_argparser
114+
112115
argparser = argparse.ArgumentParser()
113116
argparser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
114117
argparser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
115118
argparser.add_argument('words', nargs='+', help='words to say')
116-
@with_argument_parser(argparser)
119+
@with_argparser(argparser)
117120
def do_speak(self, args):
118121
"""Repeats what you tell me to."""
119122
words = []
@@ -154,7 +157,7 @@ A sample application for cmd2.
154157
import random
155158
import argparse
156159

157-
from cmd2 import Cmd, with_argument_parser
160+
from cmd2 import Cmd, with_argparser
158161

159162

160163
class CmdLineApp(Cmd):
@@ -178,12 +181,12 @@ class CmdLineApp(Cmd):
178181
# Set use_ipython to True to enable the "ipy" command which embeds and interactive IPython shell
179182
Cmd.__init__(self, use_ipython=False)
180183

181-
argparser = argparse.ArgumentParser()
182-
argparser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
183-
argparser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
184-
argparser.add_argument('-r', '--repeat', type=int, help='output [n] times')
185-
argparser.add_argument('words', nargs='+', help='words to say')
186-
@with_argument_parser(argparser)
184+
speak_parser = argparse.ArgumentParser()
185+
speak_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
186+
speak_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
187+
speak_parser.add_argument('-r', '--repeat', type=int, help='output [n] times')
188+
speak_parser.add_argument('words', nargs='+', help='words to say')
189+
@with_argparser(speak_parser)
187190
def do_speak(self, args):
188191
"""Repeats what you tell me to."""
189192
words = []
@@ -201,10 +204,10 @@ class CmdLineApp(Cmd):
201204
do_say = do_speak # now "say" is a synonym for "speak"
202205
do_orate = do_speak # another synonym, but this one takes multi-line input
203206

204-
argparser = argparse.ArgumentParser()
205-
argparser.add_argument('-r', '--repeat', type=int, help='how many times to repeat')
206-
argparser.add_argument('words', nargs='+', help='words to say')
207-
@with_argument_parser(argparser)
207+
mumble_parser = argparse.ArgumentParser()
208+
mumble_parser.add_argument('-r', '--repeat', type=int, help='how many times to repeat')
209+
mumble_parser.add_argument('words', nargs='+', help='words to say')
210+
@with_argparser(mumble_parser)
208211
def do_mumble(self, args):
209212
"""Mumbles what you tell me to."""
210213
repetitions = args.repeat or 1

cmd2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@
122122

123123

124124
# The next 3 variables and associated setter functions effect how arguments are parsed for decorated commands
125-
# which use one of the decorators such as @with_argument_list or @with_argument_parser
125+
# which use one of the decorators such as @with_argument_list or @with_argparser
126126
# The defaults are sane and maximize ease of use for new applications based on cmd2.
127127
# To maximize backwards compatibility, we recommend setting USE_ARG_LIST to "False"
128128

docs/argument_processing.rst

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ 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_argparser`` decorator.
16+
These features are all provided by the ``@with_argparser`` decorator which is importable from ``cmd2``.
17+
18+
See the either the argprint_ or argparse_ example to learn more about how to use the various ``cmd2`` argument
19+
processing decorators in your ``cmd2`` applications.
20+
21+
.. _argprint: https://github.com/python-cmd2/cmd2/blob/master/examples/arg_print.py
22+
.. _argparse: https://github.com/python-cmd2/cmd2/blob/master/examples/argparse_example.py
1723

1824
Using the argument parser decorator
1925
===================================
@@ -27,6 +33,9 @@ of ``ArgumentParser.parse_args()``.
2733

2834
Here's what it looks like::
2935

36+
import argparse
37+
from cmd2 import with_argparser
38+
3039
argparser = argparse.ArgumentParser()
3140
argparser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
3241
argparser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
@@ -61,6 +70,9 @@ for help on the command. When you use the ``@with_argparser``
6170
decorator, the docstring for the ``do_*`` method is used to set the description for the ``argparse.ArgumentParser`` is
6271
With this code::
6372

73+
import argparse
74+
from cmd2 import with_argparser
75+
6476
argparser = argparse.ArgumentParser()
6577
argparser.add_argument('tag', help='tag')
6678
argparser.add_argument('content', nargs='+', help='content to surround with tag')
@@ -89,6 +101,9 @@ The ``help tag`` command displays:
89101
If you would prefer you can set the ``description`` while instantiating the ``argparse.ArgumentParser`` and leave the
90102
docstring on your method empty::
91103

104+
import argparse
105+
from cmd2 import with_argparser
106+
92107
argparser = argparse.ArgumentParser(description='create an html tag')
93108
argparser.add_argument('tag', help='tag')
94109
argparser.add_argument('content', nargs='+', help='content to surround with tag')
@@ -115,10 +130,11 @@ Now when the user enters ``help tag`` they see:
115130
116131
To add additional text to the end of the generated help message, use the ``epilog`` variable::
117132

118-
argparser = argparse.ArgumentParser(
119-
description='create an html tag',
120-
epilog='This command can not generate tags with no content, like <br/>.'
121-
)
133+
import argparse
134+
from cmd2 import with_argparser
135+
136+
argparser = argparse.ArgumentParser(description='create an html tag',
137+
epilog='This command can not generate tags with no content, like <br/>.')
122138
argparser.add_argument('tag', help='tag')
123139
argparser.add_argument('content', nargs='+', help='content to surround with tag')
124140
@with_argparser(argparser)
@@ -150,17 +166,19 @@ Receiving an argument list
150166
The default behavior of ``cmd2`` is to pass the user input directly to your
151167
``do_*`` methods as a string. If you don't want to use the full argument parser support outlined above, you can still have ``cmd2`` apply shell parsing rules to the user input and pass you a list of arguments instead of a string. Apply the ``@with_argument_list`` decorator to those methods that should receive an argument list instead of a string::
152168

153-
class CmdLineApp(cmd2.Cmd):
154-
""" Example cmd2 application. """
169+
from cmd2 import with_argument_list
170+
171+
class CmdLineApp(cmd2.Cmd):
172+
""" Example cmd2 application. """
155173

156-
def do_say(self, cmdline):
157-
# cmdline contains a string
158-
pass
174+
def do_say(self, cmdline):
175+
# cmdline contains a string
176+
pass
159177

160-
@with_argument_list
161-
def do_speak(self, arglist):
162-
# arglist contains a list of arguments
163-
pass
178+
@with_argument_list
179+
def do_speak(self, arglist):
180+
# arglist contains a list of arguments
181+
pass
164182

165183

166184
Using the argument parser decorator and also receiving a a list of unknown positional arguments
@@ -170,6 +188,9 @@ decorate the command method with the ``@with_argparser_and_unknown_args`` decora
170188

171189
Here's what it looks like::
172190

191+
import argparse
192+
from cmd2 import with_argparser_and_unknown_args
193+
173194
dir_parser = argparse.ArgumentParser()
174195
dir_parser.add_argument('-l', '--long', action='store_true', help="display in long format with one item per line")
175196

@@ -209,6 +230,9 @@ recommends using ``argparse`` instead.
209230

210231
Here's an example::
211232

233+
from optparse import make_option
234+
from cmd2 import options
235+
212236
opts = [make_option('-p', '--piglatin', action="store_true", help="atinLay"),
213237
make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE"),
214238
make_option('-r', '--repeat', type="int", help="output [n] times")]

0 commit comments

Comments
 (0)