Skip to content

Commit 4b41ce7

Browse files
kmvanbruntanselor
authored andcommitted
Updated all examples to use Cmd2ArgumentParser instead of argparse.ArgumentParser.
This is best practice for consistency of appearance between built-in and custom commands.
1 parent e5a7707 commit 4b41ce7

15 files changed

+36
-51
lines changed

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,9 @@ Instructions for implementing each feature follow.
130130
- Optionally, `cmd2.with_argparser(.., with_unknown_args=True)` can be used to pass all unknown arguments as a list
131131

132132
```Python
133-
import argparse
134-
from cmd2 import with_argparser
133+
from cmd2 import Cmd2ArgumentParser, with_argparser
135134

136-
argparser = argparse.ArgumentParser()
135+
argparser = Cmd2ArgumentParser()
137136
argparser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
138137
argparser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
139138
argparser.add_argument('words', nargs='+', help='words to say')
@@ -232,7 +231,6 @@ Example cmd2 application (**examples/example.py**):
232231
"""
233232
A sample application for cmd2.
234233
"""
235-
import argparse
236234
import random
237235
import sys
238236
import cmd2
@@ -256,7 +254,7 @@ class CmdLineApp(cmd2.Cmd):
256254
# Make maxrepeats settable at runtime
257255
self.add_settable(cmd2.Settable('maxrepeats', int, 'max repetitions for speak command'))
258256

259-
speak_parser = argparse.ArgumentParser()
257+
speak_parser = cmd2.Cmd2ArgumentParser()
260258
speak_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
261259
speak_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
262260
speak_parser.add_argument('-r', '--repeat', type=int, help='output [n] times')
@@ -280,7 +278,7 @@ class CmdLineApp(cmd2.Cmd):
280278
do_say = do_speak # now "say" is a synonym for "speak"
281279
do_orate = do_speak # another synonym, but this one takes multi-line input
282280

283-
mumble_parser = argparse.ArgumentParser()
281+
mumble_parser = cmd2.Cmd2ArgumentParser()
284282
mumble_parser.add_argument('-r', '--repeat', type=int, help='how many times to repeat')
285283
mumble_parser.add_argument('words', nargs='+', help='words to say')
286284

cmd2/decorators.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ def with_argparser(
288288
289289
:Example:
290290
291-
>>> parser = argparse.ArgumentParser()
291+
>>> parser = cmd2.Cmd2ArgumentParser()
292292
>>> parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
293293
>>> parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
294294
>>> parser.add_argument('-r', '--repeat', type=int, help='output [n] times')
@@ -302,7 +302,7 @@ def with_argparser(
302302
303303
:Example with unknown args:
304304
305-
>>> parser = argparse.ArgumentParser()
305+
>>> parser = cmd2.Cmd2ArgumentParser()
306306
>>> parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
307307
>>> parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
308308
>>> parser.add_argument('-r', '--repeat', type=int, help='output [n] times')

docs/examples/first_app.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ can shout and talk piglatin. We will also use some built in methods for
9696
this code to ``first_app.py``, so that the ``speak_parser`` attribute and the
9797
``do_speak()`` method are part of the ``CmdLineApp()`` class::
9898

99-
speak_parser = argparse.ArgumentParser()
99+
speak_parser = cmd2.Cmd2ArgumentParser()
100100
speak_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
101101
speak_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
102102
speak_parser.add_argument('-r', '--repeat', type=int, help='output [n] times')

docs/features/argument_processing.rst

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,9 @@ method, which will contain the results of ``ArgumentParser.parse_args()``.
5555

5656
Here's what it looks like::
5757

58-
import argparse
59-
from cmd2 import with_argparser
58+
from cmd2 import Cmd2ArgumentParser, with_argparser
6059

61-
argparser = argparse.ArgumentParser()
60+
argparser = Cmd2ArgumentParser()
6261
argparser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
6362
argparser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
6463
argparser.add_argument('-r', '--repeat', type=int, help='output [n] times')
@@ -106,10 +105,9 @@ docstring for the ``do_*`` method is used to set the description for the
106105

107106
With this code::
108107

109-
import argparse
110-
from cmd2 import with_argparser
108+
from cmd2 import Cmd2ArgumentParser, with_argparser
111109

112-
argparser = argparse.ArgumentParser()
110+
argparser = Cmd2ArgumentParser()
113111
argparser.add_argument('tag', help='tag')
114112
argparser.add_argument('content', nargs='+', help='content to surround with tag')
115113
@with_argparser(argparser)
@@ -137,10 +135,9 @@ the ``help tag`` command displays:
137135
If you would prefer you can set the ``description`` while instantiating the
138136
``argparse.ArgumentParser`` and leave the docstring on your method empty::
139137

140-
import argparse
141-
from cmd2 import with_argparser
138+
from cmd2 import Cmd2ArgumentParser, with_argparser
142139

143-
argparser = argparse.ArgumentParser(description='create an html tag')
140+
argparser = Cmd2ArgumentParser(description='create an html tag')
144141
argparser.add_argument('tag', help='tag')
145142
argparser.add_argument('content', nargs='+', help='content to surround with tag')
146143
@with_argparser(argparser)
@@ -166,11 +163,10 @@ Now when the user enters ``help tag`` they see:
166163
167164
To add additional text to the end of the generated help message, use the ``epilog`` variable::
168165

169-
import argparse
170-
from cmd2 import with_argparser
166+
from cmd2 import Cmd2ArgumentParser, with_argparser
171167

172-
argparser = argparse.ArgumentParser(description='create an html tag',
173-
epilog='This command cannot generate tags with no content, like <br/>.')
168+
argparser = Cmd2ArgumentParser(description='create an html tag',
169+
epilog='This command cannot generate tags with no content, like <br/>.')
174170
argparser.add_argument('tag', help='tag')
175171
argparser.add_argument('content', nargs='+', help='content to surround with tag')
176172
@with_argparser(argparser)
@@ -265,10 +261,9 @@ strings, then decorate the command method with the
265261

266262
Here's what it looks like::
267263

268-
import argparse
269-
from cmd2 import with_argparser
264+
from cmd2 import Cmd2ArgumentParser, with_argparser
270265

271-
dir_parser = argparse.ArgumentParser()
266+
dir_parser = Cmd2ArgumentParser()
272267
dir_parser.add_argument('-l', '--long', action='store_true', help="display in long format with one item per line")
273268

274269
@with_argparser(dir_parser, with_unknown_args=True)

examples/arg_decorators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def do_fsize(self, args: argparse.Namespace) -> None:
4242
self.poutput('{} {}'.format(size, args.unit))
4343

4444
# do_pow parser
45-
pow_parser = argparse.ArgumentParser()
45+
pow_parser = cmd2.Cmd2ArgumentParser()
4646
pow_parser.add_argument('base', type=int)
4747
pow_parser.add_argument('exponent', type=int, choices=range(-5, 6))
4848

examples/arg_print.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
1010
It also serves as an example of how to create shortcuts.
1111
"""
12-
import argparse
1312

1413
import cmd2
1514

@@ -40,7 +39,7 @@ def do_rprint(self, arglist):
4039
"""Print the argument list this basic command is called with (with quotes preserved)."""
4140
self.poutput('rprint was called with the following list of arguments: {!r}'.format(arglist))
4241

43-
oprint_parser = argparse.ArgumentParser()
42+
oprint_parser = cmd2.Cmd2ArgumentParser()
4443
oprint_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
4544
oprint_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
4645
oprint_parser.add_argument('-r', '--repeat', type=int, help='output [n] times')
@@ -51,7 +50,7 @@ def do_oprint(self, args):
5150
"""Print the options and argument list this options command was called with."""
5251
self.poutput('oprint was called with the following\n\toptions: {!r}'.format(args))
5352

54-
pprint_parser = argparse.ArgumentParser()
53+
pprint_parser = cmd2.Cmd2ArgumentParser()
5554
pprint_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
5655
pprint_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
5756
pprint_parser.add_argument('-r', '--repeat', type=int, help='output [n] times')

examples/cmd_as_argument.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def __init__(self):
3838
# Make maxrepeats settable at runtime
3939
self.add_settable(cmd2.Settable('maxrepeats', int, 'max repetitions for speak command', self))
4040

41-
speak_parser = argparse.ArgumentParser()
41+
speak_parser = cmd2.Cmd2ArgumentParser()
4242
speak_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
4343
speak_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
4444
speak_parser.add_argument('-r', '--repeat', type=int, help='output [n] times')
@@ -62,7 +62,7 @@ def do_speak(self, args):
6262
do_say = do_speak # now "say" is a synonym for "speak"
6363
do_orate = do_speak # another synonym, but this one takes multi-line input
6464

65-
mumble_parser = argparse.ArgumentParser()
65+
mumble_parser = cmd2.Cmd2ArgumentParser()
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

@@ -86,7 +86,7 @@ def do_mumble(self, args):
8686
def main(argv=None):
8787
"""Run when invoked from the operating system shell"""
8888

89-
parser = argparse.ArgumentParser(description='Commands as arguments')
89+
parser = cmd2.Cmd2ArgumentParser(description='Commands as arguments')
9090
command_help = 'optional command to run, if no command given, enter an interactive shell'
9191
parser.add_argument('command', nargs='?', help=command_help)
9292
arg_help = 'optional arguments for command'

examples/colors.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
poutput(), pfeedback(), and ppaged() never strip ANSI style sequences,
2424
regardless of the output destination
2525
"""
26-
import argparse
2726
from typing import (
2827
Any,
2928
)
@@ -54,7 +53,7 @@ def __init__(self):
5453
# Should ANSI color output be allowed
5554
self.allow_style = ansi.STYLE_TERMINAL
5655

57-
speak_parser = argparse.ArgumentParser()
56+
speak_parser = cmd2.Cmd2ArgumentParser()
5857
speak_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
5958
speak_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
6059
speak_parser.add_argument('-r', '--repeat', type=int, help='output [n] times')

examples/decorator_example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def __init__(self, ip_addr=None, port=None, transcript_files=None):
3737
# Setting this true makes it run a shell command if a cmd2/cmd command doesn't exist
3838
# self.default_to_shell = True
3939

40-
speak_parser = argparse.ArgumentParser()
40+
speak_parser = cmd2.Cmd2ArgumentParser()
4141
speak_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
4242
speak_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
4343
speak_parser.add_argument('-r', '--repeat', type=int, help='output [n] times')
@@ -60,7 +60,7 @@ def do_speak(self, args: argparse.Namespace):
6060
do_say = do_speak # now "say" is a synonym for "speak"
6161
do_orate = do_speak # another synonym, but this one takes multi-line input
6262

63-
tag_parser = argparse.ArgumentParser()
63+
tag_parser = cmd2.Cmd2ArgumentParser()
6464
tag_parser.add_argument('tag', help='tag')
6565
tag_parser.add_argument('content', nargs='+', help='content to surround with tag')
6666

@@ -89,7 +89,7 @@ def do_tagg(self, arglist: List[str]):
8989
import sys
9090

9191
# You can do your custom Argparse parsing here to meet your application's needs
92-
parser = argparse.ArgumentParser(description='Process the arguments however you like.')
92+
parser = cmd2.Cmd2ArgumentParser(description='Process the arguments however you like.')
9393

9494
# Add a few arguments which aren't really used, but just to get the gist
9595
parser.add_argument('-p', '--port', type=int, help='TCP port')

examples/example.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
the transcript against example.py, verifying that the output produced matches
1111
the transcript.
1212
"""
13-
import argparse
1413
import random
1514

1615
import cmd2
@@ -34,7 +33,7 @@ def __init__(self):
3433
self.maxrepeats = 3
3534
self.add_settable(cmd2.Settable('maxrepeats', int, 'max repetitions for speak command', self))
3635

37-
speak_parser = argparse.ArgumentParser()
36+
speak_parser = cmd2.Cmd2ArgumentParser()
3837
speak_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
3938
speak_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
4039
speak_parser.add_argument('-r', '--repeat', type=int, help='output [n] times')
@@ -58,7 +57,7 @@ def do_speak(self, args):
5857
do_say = do_speak # now "say" is a synonym for "speak"
5958
do_orate = do_speak # another synonym, but this one takes multi-line input
6059

61-
mumble_parser = argparse.ArgumentParser()
60+
mumble_parser = cmd2.Cmd2ArgumentParser()
6261
mumble_parser.add_argument('-r', '--repeat', type=int, help='how many times to repeat')
6362
mumble_parser.add_argument('words', nargs='+', help='words to say')
6463

examples/first_app.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
* Multiline Commands
1313
* History
1414
"""
15-
import argparse
1615

1716
import cmd2
1817

@@ -29,7 +28,7 @@ def __init__(self):
2928
self.maxrepeats = 3
3029
self.add_settable(cmd2.Settable('maxrepeats', int, 'max repetitions for speak command', self))
3130

32-
speak_parser = argparse.ArgumentParser()
31+
speak_parser = cmd2.Cmd2ArgumentParser()
3332
speak_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
3433
speak_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
3534
speak_parser.add_argument('-r', '--repeat', type=int, help='output [n] times')

examples/pirate.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
77
It demonstrates many features of cmd2.
88
"""
9-
import argparse
109

1110
import cmd2
1211
import cmd2.ansi
@@ -75,7 +74,7 @@ def do_sing(self, arg):
7574
"""Sing a colorful song."""
7675
self.poutput(cmd2.ansi.style(arg, fg=self.songcolor))
7776

78-
yo_parser = argparse.ArgumentParser()
77+
yo_parser = cmd2.Cmd2ArgumentParser()
7978
yo_parser.add_argument('--ho', type=int, default=2, help="How often to chant 'ho'")
8079
yo_parser.add_argument('-c', '--commas', action='store_true', help='Intersperse commas')
8180
yo_parser.add_argument('beverage', help='beverage to drink with the chant')

examples/plumbum_colors.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
2626
WARNING: This example requires the plumbum package, which isn't normally required by cmd2.
2727
"""
28-
import argparse
2928

3029
from plumbum.colors import (
3130
bg,
@@ -88,7 +87,7 @@ def __init__(self):
8887
# Should ANSI color output be allowed
8988
self.allow_style = ansi.STYLE_TERMINAL
9089

91-
speak_parser = argparse.ArgumentParser()
90+
speak_parser = cmd2.Cmd2ArgumentParser()
9291
speak_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
9392
speak_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
9493
speak_parser.add_argument('-r', '--repeat', type=int, help='output [n] times')

examples/python_scripting.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
This application and the "examples/scripts/conditional.py" script serve as an
2121
example for one way in which this can be done.
2222
"""
23-
import argparse
2423
import os
2524

2625
import cmd2
@@ -95,7 +94,7 @@ def complete_cd(self, text, line, begidx, endidx):
9594
# Tab complete only directories
9695
return self.path_complete(text, line, begidx, endidx, path_filter=os.path.isdir)
9796

98-
dir_parser = argparse.ArgumentParser()
97+
dir_parser = cmd2.Cmd2ArgumentParser()
9998
dir_parser.add_argument('-l', '--long', action='store_true', help="display in long format with one item per line")
10099

101100
@cmd2.with_argparser(dir_parser, with_unknown_args=True)

examples/subcommands.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
This example shows an easy way for a single command to have many subcommands, each of which takes different arguments
77
and provides separate contextual help.
88
"""
9-
import argparse
109

1110
import cmd2
1211

1312
sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football', 'Space Ball']
1413

1514
# create the top-level parser for the base command
16-
base_parser = argparse.ArgumentParser()
15+
base_parser = cmd2.Cmd2ArgumentParser()
1716
base_subparsers = base_parser.add_subparsers(title='subcommands', help='subcommand help')
1817

1918
# create the parser for the "foo" subcommand
@@ -39,7 +38,7 @@
3938

4039
# create the top-level parser for the alternate command
4140
# The alternate command doesn't provide its own help flag
42-
base2_parser = argparse.ArgumentParser(add_help=False)
41+
base2_parser = cmd2.Cmd2ArgumentParser(add_help=False)
4342
base2_subparsers = base2_parser.add_subparsers(title='subcommands', help='subcommand help')
4443

4544
# create the parser for the "foo" subcommand

0 commit comments

Comments
 (0)