Skip to content

Commit 03f1569

Browse files
committed
Simplified a few argparse examples and fixed some incorrect documentation
I eliminated a few "narg=1" configurations so that a single str value is returned instead of a List[str]. I also reworded some documentation which was no longer correct after the last commit which made "history command" have the same help text as "command -h" when using one of the two argparse decorators.
1 parent b034f6d commit 03f1569

File tree

4 files changed

+18
-16
lines changed

4 files changed

+18
-16
lines changed

docs/argument_processing.rst

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,26 @@ Help Messages
5858

5959
By default, cmd2 uses the docstring of the command method when a user asks
6060
for help on the command. When you use the ``@with_argument_parser``
61-
decorator, the formatted help from the ``argparse.ArgumentParser`` is
62-
appended to the docstring for the method of that command. With this code::
61+
decorator, the docstring for the ``do_*`` method is used to set the description for the ``argparse.ArgumentParser`` is
62+
With this code::
6363

6464
argparser = argparse.ArgumentParser()
65-
argparser.add_argument('tag', nargs=1, help='tag')
65+
argparser.add_argument('tag', help='tag')
6666
argparser.add_argument('content', nargs='+', help='content to surround with tag')
6767
@with_argument_parser(argparser)
6868
def do_tag(self, args):
6969
"""create a html tag"""
70-
self.stdout.write('<{0}>{1}</{0}>'.format(args.tag[0], ' '.join(args.content)))
70+
self.stdout.write('<{0}>{1}</{0}>'.format(args.tag, ' '.join(args.content)))
7171
self.stdout.write('\n')
7272

7373
The ``help tag`` command displays:
7474

7575
.. code-block:: none
7676
77-
create a html tag
7877
usage: tag [-h] tag content [content ...]
7978
79+
create a html tag
80+
8081
positional arguments:
8182
tag tag
8283
content content to surround with tag
@@ -85,14 +86,15 @@ The ``help tag`` command displays:
8586
-h, --help show this help message and exit
8687
8788
88-
If you would prefer the short description of your command to come after the usage message, leave the docstring on your method empty, but supply a ``description`` variable to the argument parser::
89+
If you would prefer you can set the ``description`` while instantiating the ``argparse.ArgumentParser`` and leave the
90+
docstring on your method empty::
8991

9092
argparser = argparse.ArgumentParser(description='create an html tag')
91-
argparser.add_argument('tag', nargs=1, help='tag')
93+
argparser.add_argument('tag', help='tag')
9294
argparser.add_argument('content', nargs='+', help='content to surround with tag')
9395
@with_argument_parser(argparser)
9496
def do_tag(self, args):
95-
self.stdout.write('<{0}>{1}</{0}>'.format(args.tag[0], ' '.join(args.content)))
97+
self.stdout.write('<{0}>{1}</{0}>'.format(args.tag, ' '.join(args.content)))
9698
self.stdout.write('\n')
9799

98100
Now when the user enters ``help tag`` they see:
@@ -117,11 +119,11 @@ To add additional text to the end of the generated help message, use the ``epilo
117119
description='create an html tag',
118120
epilog='This command can not generate tags with no content, like <br/>.'
119121
)
120-
argparser.add_argument('tag', nargs=1, help='tag')
122+
argparser.add_argument('tag', help='tag')
121123
argparser.add_argument('content', nargs='+', help='content to surround with tag')
122124
@with_argument_parser(argparser)
123125
def do_tag(self, args):
124-
self.stdout.write('<{0}>{1}</{0}>'.format(args.tag[0], ' '.join(args.content)))
126+
self.stdout.write('<{0}>{1}</{0}>'.format(args.tag, ' '.join(args.content)))
125127
self.stdout.write('\n')
126128

127129
Which yields:

examples/argparse_example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ def do_speak(self, args):
6565
do_orate = do_speak # another synonym, but this one takes multi-line input
6666

6767
tag_parser = argparse.ArgumentParser()
68-
tag_parser.add_argument('tag', nargs=1, help='tag')
68+
tag_parser.add_argument('tag', help='tag')
6969
tag_parser.add_argument('content', nargs='+', help='content to surround with tag')
7070

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

7676

7777
@with_argument_list

examples/pirate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,15 @@ def do_sing(self, arg):
7676
yo_parser = argparse.ArgumentParser()
7777
yo_parser.add_argument('--ho', type=int, default=2, help="How often to chant 'ho'")
7878
yo_parser.add_argument('-c', '--commas', action='store_true', help='Intersperse commas')
79-
yo_parser.add_argument('beverage', nargs=1, help='beverage to drink with the chant')
79+
yo_parser.add_argument('beverage', help='beverage to drink with the chant')
8080

8181
@with_argument_parser(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
8585
separator = ', ' if args.commas else ' '
8686
chant = separator.join(chant)
87-
print('{0} and a bottle of {1}'.format(chant, args.beverage[0]))
87+
print('{0} and a bottle of {1}'.format(chant, args.beverage))
8888

8989

9090
if __name__ == '__main__':

tests/test_argparse.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ def do_say(self, args):
3737
self.stdout.write('\n')
3838

3939
tag_parser = argparse.ArgumentParser(description='create a html tag')
40-
tag_parser.add_argument('tag', nargs=1, help='tag')
40+
tag_parser.add_argument('tag', help='tag')
4141
tag_parser.add_argument('content', nargs='+', help='content to surround with tag')
4242

4343
@cmd2.with_argument_parser(tag_parser)
4444
def do_tag(self, args):
45-
self.stdout.write('<{0}>{1}</{0}>'.format(args.tag[0], ' '.join(args.content)))
45+
self.stdout.write('<{0}>{1}</{0}>'.format(args.tag, ' '.join(args.content)))
4646
self.stdout.write('\n')
4747

4848
@cmd2.with_argument_list

0 commit comments

Comments
 (0)