Skip to content

Commit 8c58bb5

Browse files
committed
Properly set docstring so it contains help message
1 parent c25a2b7 commit 8c58bb5

File tree

3 files changed

+55
-39
lines changed

3 files changed

+55
-39
lines changed

cmd2.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,16 @@ def cmd_wrapper(instance, arg):
258258
temp_arglist.append(strip_quotes(arg))
259259
lexed_arglist = temp_arglist
260260
opts = argparser.parse_args(lexed_arglist)
261-
262261
func(instance, arg, opts)
262+
263+
funcdoc = func.__doc__
264+
if funcdoc:
265+
funcdoc += '\n'
266+
else:
267+
# if it's None, make it an empty string
268+
funcdoc = ''
269+
270+
cmd_wrapper.__doc__ = '{}{}'.format(funcdoc, argparser.format_help())
263271
return cmd_wrapper
264272
return arg_decorator
265273

examples/argparse_example.py

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#!/usr/bin/env python
22
# coding=utf-8
3-
"""A sample application for cmd2 showing how to use Argparse to process command line arguments for your application.
4-
It parses command line arguments looking for known arguments, but then still passes any unknown arguments onto cmd2
5-
to treat them as arguments at invocation.
3+
"""A sample application for cmd2 showing how to use argparse to
4+
process command line arguments for your application.
65
7-
Thanks to cmd2's built-in transcript testing capability, it also serves as a test suite for argparse_example.py when
8-
used with the exampleSession.txt transcript.
6+
Thanks to cmd2's built-in transcript testing capability, it also
7+
serves as a test suite for argparse_example.py when used with the
8+
exampleSession.txt transcript.
99
10-
Running `python argparse_example.py -t exampleSession.txt` will run all the commands in the transcript against
11-
argparse_example.py, verifying that the output produced matches the transcript.
10+
Running `python argparse_example.py -t exampleSession.txt` will run
11+
all the commands in the transcript against argparse_example.py,
12+
verifying that the output produced matches the transcript.
1213
"""
1314
import argparse
1415
import sys
@@ -39,40 +40,14 @@ def __init__(self, ip_addr=None, port=None, transcript_files=None):
3940
# Setting this true makes it run a shell command if a cmd2/cmd command doesn't exist
4041
# self.default_to_shell = True
4142

42-
@options([make_option('-p', '--piglatin', action="store_true", help="atinLay"),
43-
make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE"),
44-
make_option('-r', '--repeat', type="int", help="output [n] times")
45-
])
46-
def do_speak(self, arg, opts=None):
47-
"""Repeats what you tell me to."""
48-
words = []
49-
for word in arg:
50-
if opts.piglatin:
51-
word = '%s%say' % (word[1:], word[0])
52-
if opts.shout:
53-
arg = arg.upper()
54-
words.append(word)
55-
repetitions = opts.repeat or 1
56-
for i in range(min(repetitions, self.maxrepeats)):
57-
self.stdout.write(' '.join(words))
58-
self.stdout.write('\n')
59-
# self.stdout.write is better than "print", because Cmd can be
60-
# initialized with a non-standard output destination
6143

62-
do_say = do_speak # now "say" is a synonym for "speak"
63-
do_orate = do_speak # another synonym, but this one takes multi-line input
64-
65-
66-
argparser = argparse.ArgumentParser(
67-
prog='sspeak',
68-
description='Repeats what you tell me to'
69-
)
44+
argparser = argparse.ArgumentParser(prog='speak')
7045
argparser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
7146
argparser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
7247
argparser.add_argument('-r', '--repeat', type=int, help='output [n] times')
7348
argparser.add_argument('words', nargs='+', help='words to say')
7449
@with_argument_parser(argparser)
75-
def do_sspeak(self, rawarg, args=None):
50+
def do_speak(self, rawarg, args=None):
7651
"""Repeats what you tell me to."""
7752
words = []
7853
for word in args.words:
@@ -88,10 +63,13 @@ def do_sspeak(self, rawarg, args=None):
8863
# self.stdout.write is better than "print", because Cmd can be
8964
# initialized with a non-standard output destination
9065

66+
do_say = do_speak # now "say" is a synonym for "speak"
67+
do_orate = do_speak # another synonym, but this one takes multi-line input
68+
9169

9270
argparser = argparse.ArgumentParser(
9371
prog='tag',
94-
description='create an html tag, the first argument is the tag, the rest is the contents'
72+
description='create a html tag',
9573
)
9674
argparser.add_argument('tag', nargs=1, help='tag')
9775
argparser.add_argument('content', nargs='+', help='content to surround with tag')
@@ -102,6 +80,28 @@ def do_tag(self, cmdline, args=None):
10280
# self.stdout.write is better than "print", because Cmd can be
10381
# initialized with a non-standard output destination
10482

83+
# @options uses the python optparse module which has been deprecated
84+
# since 2011. Use @with_argument_parser instead, which utilizes the
85+
# python argparse module
86+
@options([make_option('-p', '--piglatin', action="store_true", help="atinLay"),
87+
make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE"),
88+
make_option('-r', '--repeat', type="int", help="output [n] times")
89+
])
90+
def do_deprecated_speak(self, arg, opts=None):
91+
"""Repeats what you tell me to."""
92+
words = []
93+
for word in arg:
94+
if opts.piglatin:
95+
word = '%s%say' % (word[1:], word[0])
96+
if opts.shout:
97+
arg = arg.upper()
98+
words.append(word)
99+
repetitions = opts.repeat or 1
100+
for i in range(min(repetitions, self.maxrepeats)):
101+
self.stdout.write(' '.join(words))
102+
self.stdout.write('\n')
103+
# self.stdout.write is better than "print", because Cmd can be
104+
# initialized with a non-standard output destination
105105

106106
if __name__ == '__main__':
107107
# You can do your custom Argparse parsing here to meet your application's needs

tests/test_argparse.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ def __init__(self):
1515

1616
argparser = argparse.ArgumentParser(
1717
prog='say',
18-
description='Repeats what you tell me to'
1918
)
2019
argparser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
2120
argparser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
2221
argparser.add_argument('-r', '--repeat', type=int, help='output [n] times')
2322
argparser.add_argument('words', nargs='+', help='words to say')
2423
@cmd2.with_argument_parser(argparser)
2524
def do_say(self, cmdline, args=None):
25+
"""Repeat what you tell me to."""
2626
words = []
2727
for word in args.words:
2828
if word is None:
@@ -39,7 +39,7 @@ def do_say(self, cmdline, args=None):
3939

4040
argparser = argparse.ArgumentParser(
4141
prog='tag',
42-
description='create an html tag, the first argument is the tag, the rest is the contents'
42+
description='create a html tag'
4343
)
4444
argparser.add_argument('tag', nargs=1, help='tag')
4545
argparser.add_argument('content', nargs='+', help='content to surround with tag')
@@ -80,3 +80,11 @@ def test_argparse_quoted_arguments_posix_multiple(argparse_app):
8080
argparse_app.POSIX = True
8181
out = run_cmd(argparse_app, 'tag strong this "should be" loud')
8282
assert out == ['<strong>this should be loud</strong>']
83+
84+
def test_argparse_help_docstring(argparse_app):
85+
out = run_cmd(argparse_app, 'help say')
86+
assert out[0] == 'Repeat what you tell me to.'
87+
88+
def test_argparse_help_description(argparse_app):
89+
out = run_cmd(argparse_app, 'help tag')
90+
assert out[2] == 'create a html tag'

0 commit comments

Comments
 (0)