summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkotfu <kotfu@kotfu.net>2018-01-07 20:02:38 -0700
committerkotfu <kotfu@kotfu.net>2018-01-07 20:02:38 -0700
commit6cb43c600f0b5d694201233ade6abd4f989d1730 (patch)
treec9acbeaf507a762db0b05b32a1e824b479279bfe
parent8c58bb558adceb8ff32c7a3e1a88d2a13371dbfa (diff)
downloadcmd2-git-6cb43c600f0b5d694201233ade6abd4f989d1730.tar.gz
Set prog in argparser based on the name of the function
-rwxr-xr-xcmd2.py6
-rwxr-xr-xexamples/argparse_example.py5
-rw-r--r--tests/test_argparse.py15
3 files changed, 14 insertions, 12 deletions
diff --git a/cmd2.py b/cmd2.py
index c284a66f..f5c1dab0 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -260,13 +260,17 @@ def with_argument_parser(argparser):
opts = argparser.parse_args(lexed_arglist)
func(instance, arg, opts)
+ # argparser defaults the program name to sys.argv[0]
+ # we want it to be the name of our command
+ argparser.prog = func.__name__[3:]
+
+ # put the help message in the method docstring
funcdoc = func.__doc__
if funcdoc:
funcdoc += '\n'
else:
# if it's None, make it an empty string
funcdoc = ''
-
cmd_wrapper.__doc__ = '{}{}'.format(funcdoc, argparser.format_help())
return cmd_wrapper
return arg_decorator
diff --git a/examples/argparse_example.py b/examples/argparse_example.py
index c6a17435..a387acc3 100755
--- a/examples/argparse_example.py
+++ b/examples/argparse_example.py
@@ -67,10 +67,7 @@ class CmdLineApp(Cmd):
do_orate = do_speak # another synonym, but this one takes multi-line input
- argparser = argparse.ArgumentParser(
- prog='tag',
- description='create a html tag',
- )
+ argparser = argparse.ArgumentParser(description='create a html tag')
argparser.add_argument('tag', nargs=1, help='tag')
argparser.add_argument('content', nargs='+', help='content to surround with tag')
@with_argument_parser(argparser)
diff --git a/tests/test_argparse.py b/tests/test_argparse.py
index 41173c8b..e9dbc1b3 100644
--- a/tests/test_argparse.py
+++ b/tests/test_argparse.py
@@ -13,9 +13,7 @@ class ArgparseApp(cmd2.Cmd):
self.maxrepeats = 3
cmd2.Cmd.__init__(self)
- argparser = argparse.ArgumentParser(
- prog='say',
- )
+ argparser = argparse.ArgumentParser()
argparser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
argparser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
argparser.add_argument('-r', '--repeat', type=int, help='output [n] times')
@@ -37,10 +35,7 @@ class ArgparseApp(cmd2.Cmd):
self.stdout.write(' '.join(words))
self.stdout.write('\n')
- argparser = argparse.ArgumentParser(
- prog='tag',
- description='create a html tag'
- )
+ argparser = argparse.ArgumentParser(description='create a html tag')
argparser.add_argument('tag', nargs=1, help='tag')
argparser.add_argument('content', nargs='+', help='content to surround with tag')
@cmd2.with_argument_parser(argparser)
@@ -88,3 +83,9 @@ def test_argparse_help_docstring(argparse_app):
def test_argparse_help_description(argparse_app):
out = run_cmd(argparse_app, 'help tag')
assert out[2] == 'create a html tag'
+
+def test_argparse_prog(argparse_app):
+ out = run_cmd(argparse_app, 'help tag')
+ progname = out[0].split(' ')[1]
+ assert progname == 'tag'
+ \ No newline at end of file