summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/test_argparse.py69
1 files changed, 59 insertions, 10 deletions
diff --git a/tests/test_argparse.py b/tests/test_argparse.py
index 9510e8f2..db6dea21 100644
--- a/tests/test_argparse.py
+++ b/tests/test_argparse.py
@@ -233,9 +233,14 @@ class SubcommandApp(cmd2.Cmd):
"""bar subcommand of base command"""
self.poutput('((%s))' % args.z)
+ def base_helpless(self, args):
+ """helpless subcommand of base command"""
+ self.poutput('((%s))' % args.z)
+
# create the top-level parser for the base command
base_parser = argparse.ArgumentParser()
- base_subparsers = base_parser.add_subparsers(title='subcommands', help='subcommand help')
+ base_subparsers = base_parser.add_subparsers(dest='subcommand', metavar='SUBCOMMAND')
+ base_subparsers.required = True
# create the parser for the "foo" subcommand
parser_foo = base_subparsers.add_parser('foo', help='foo help')
@@ -244,20 +249,24 @@ class SubcommandApp(cmd2.Cmd):
parser_foo.set_defaults(func=base_foo)
# create the parser for the "bar" subcommand
- parser_bar = base_subparsers.add_parser('bar', help='bar help')
+ parser_bar = base_subparsers.add_parser('bar', help='bar help', aliases=['bar_1', 'bar_2'])
+ parser_bar.add_argument('z', help='string')
+ parser_bar.set_defaults(func=base_bar)
+
+ # create the parser for the "helpless" subcommand
+ # This subcommand has aliases and no help text. It exists to prevent changes to _set_parser_prog() which
+ # use an approach which relies on action._choices_actions list. See comment in that function for more
+ # details.
+ parser_bar = base_subparsers.add_parser('helpless', aliases=['helpless_1', 'helpless_2'])
parser_bar.add_argument('z', help='string')
parser_bar.set_defaults(func=base_bar)
@cmd2.with_argparser(base_parser)
def do_base(self, args):
"""Base command help"""
- func = getattr(args, 'func', None)
- if func is not None:
- # Call whatever subcommand function was selected
- func(self, args)
- else:
- # No subcommand was provided, so call help
- self.do_help('base')
+ # Call whatever subcommand function was selected
+ func = getattr(args, 'func')
+ func(self, args)
@pytest.fixture
def subcommand_app():
@@ -277,7 +286,7 @@ def test_subcommand_bar(subcommand_app):
def test_subcommand_invalid(subcommand_app):
out, err = run_cmd(subcommand_app, 'base baz')
assert err[0].startswith('usage: base')
- assert err[1].startswith("base: error: invalid choice: 'baz'")
+ assert err[1].startswith("base: error: argument SUBCOMMAND: invalid choice: 'baz'")
def test_subcommand_base_help(subcommand_app):
out, err = run_cmd(subcommand_app, 'help base')
@@ -286,12 +295,52 @@ def test_subcommand_base_help(subcommand_app):
assert out[2] == 'Base command help'
def test_subcommand_help(subcommand_app):
+ # foo has no aliases
out, err = run_cmd(subcommand_app, 'help base foo')
assert out[0].startswith('usage: base foo')
assert out[1] == ''
assert out[2] == 'positional arguments:'
+ # bar has aliases (usage should never show alias name)
+ out, err = run_cmd(subcommand_app, 'help base bar')
+ assert out[0].startswith('usage: base bar')
+ assert out[1] == ''
+ assert out[2] == 'positional arguments:'
+
+ out, err = run_cmd(subcommand_app, 'help base bar_1')
+ assert out[0].startswith('usage: base bar')
+ assert out[1] == ''
+ assert out[2] == 'positional arguments:'
+
+ out, err = run_cmd(subcommand_app, 'help base bar_2')
+ assert out[0].startswith('usage: base bar')
+ assert out[1] == ''
+ assert out[2] == 'positional arguments:'
+
+ # helpless has aliases and no help text (usage should never show alias name)
+ out, err = run_cmd(subcommand_app, 'help base helpless')
+ assert out[0].startswith('usage: base helpless')
+ assert out[1] == ''
+ assert out[2] == 'positional arguments:'
+
+ out, err = run_cmd(subcommand_app, 'help base helpless_1')
+ assert out[0].startswith('usage: base helpless')
+ assert out[1] == ''
+ assert out[2] == 'positional arguments:'
+
+ out, err = run_cmd(subcommand_app, 'help base helpless_2')
+ assert out[0].startswith('usage: base helpless')
+ assert out[1] == ''
+ assert out[2] == 'positional arguments:'
def test_subcommand_invalid_help(subcommand_app):
out, err = run_cmd(subcommand_app, 'help base baz')
assert out[0].startswith('usage: base')
+
+def test_add_another_subcommand(subcommand_app):
+ """
+ This tests makes sure _set_parser_prog() sets _prog_prefix on every _SubParsersAction so that all future calls
+ to add_parser() write the correct prog value to the parser being added.
+ """
+ new_parser = subcommand_app.base_subparsers.add_parser('new_sub', help="stuff")
+ assert new_parser.prog == "base new_sub"