Skip to content

Commit 6cb43c6

Browse files
committed
Set prog in argparser based on the name of the function
1 parent 8c58bb5 commit 6cb43c6

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

cmd2.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,17 @@ def cmd_wrapper(instance, arg):
260260
opts = argparser.parse_args(lexed_arglist)
261261
func(instance, arg, opts)
262262

263+
# argparser defaults the program name to sys.argv[0]
264+
# we want it to be the name of our command
265+
argparser.prog = func.__name__[3:]
266+
267+
# put the help message in the method docstring
263268
funcdoc = func.__doc__
264269
if funcdoc:
265270
funcdoc += '\n'
266271
else:
267272
# if it's None, make it an empty string
268273
funcdoc = ''
269-
270274
cmd_wrapper.__doc__ = '{}{}'.format(funcdoc, argparser.format_help())
271275
return cmd_wrapper
272276
return arg_decorator

examples/argparse_example.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,7 @@ def do_speak(self, rawarg, args=None):
6767
do_orate = do_speak # another synonym, but this one takes multi-line input
6868

6969

70-
argparser = argparse.ArgumentParser(
71-
prog='tag',
72-
description='create a html tag',
73-
)
70+
argparser = argparse.ArgumentParser(description='create a html tag')
7471
argparser.add_argument('tag', nargs=1, help='tag')
7572
argparser.add_argument('content', nargs='+', help='content to surround with tag')
7673
@with_argument_parser(argparser)

tests/test_argparse.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ def __init__(self):
1313
self.maxrepeats = 3
1414
cmd2.Cmd.__init__(self)
1515

16-
argparser = argparse.ArgumentParser(
17-
prog='say',
18-
)
16+
argparser = argparse.ArgumentParser()
1917
argparser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
2018
argparser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
2119
argparser.add_argument('-r', '--repeat', type=int, help='output [n] times')
@@ -37,10 +35,7 @@ def do_say(self, cmdline, args=None):
3735
self.stdout.write(' '.join(words))
3836
self.stdout.write('\n')
3937

40-
argparser = argparse.ArgumentParser(
41-
prog='tag',
42-
description='create a html tag'
43-
)
38+
argparser = argparse.ArgumentParser(description='create a html tag')
4439
argparser.add_argument('tag', nargs=1, help='tag')
4540
argparser.add_argument('content', nargs='+', help='content to surround with tag')
4641
@cmd2.with_argument_parser(argparser)
@@ -88,3 +83,9 @@ def test_argparse_help_docstring(argparse_app):
8883
def test_argparse_help_description(argparse_app):
8984
out = run_cmd(argparse_app, 'help tag')
9085
assert out[2] == 'create a html tag'
86+
87+
def test_argparse_prog(argparse_app):
88+
out = run_cmd(argparse_app, 'help tag')
89+
progname = out[0].split(' ')[1]
90+
assert progname == 'tag'
91+

0 commit comments

Comments
 (0)