Debian Codegraph @

tests/test_argparse_subcommands.py

raw symbols SCIP
1 """reproduces test_argparse.py except with SubCommands"""
2
3 import pytest
4
5 import cmd2
6
7 from .conftest import (
8 WithCommandSets,
9 run_cmd,
10 )
11
12
13 class SubcommandSet(cmd2.CommandSet):
14 """Example cmd2 application where we a base command which has a couple subcommands."""
15
16 def __init__(self, dummy) -> None:
17 super().__init__()
18
19 # subcommand functions for the base command
20 def base_foo(self, args) -> None:
21 """Foo subcommand of base command"""
22 self._cmd.poutput(args.x * args.y)
23
24 def base_bar(self, args) -> None:
25 """Bar subcommand of base command"""
26 self._cmd.poutput(f"(({args.z}))")
27
28 def base_helpless(self, args) -> None:
29 """Helpless subcommand of base command"""
30 self._cmd.poutput(f"(({args.z}))")
31
32 # create the top-level parser for the base command
33 base_parser = cmd2.Cmd2ArgumentParser()
34 base_subparsers = base_parser.add_subparsers(dest="subcommand", metavar="SUBCOMMAND", required=True)
35
36 # create the parser for the "foo" subcommand
37 parser_foo = base_subparsers.add_parser("foo", help="foo help")
38 parser_foo.add_argument("-x", type=int, default=1, help="integer")
39 parser_foo.add_argument("y", type=float, help="float")
40 parser_foo.set_defaults(func=base_foo)
41
42 # create the parser for the "bar" subcommand
43 parser_bar = base_subparsers.add_parser("bar", help="bar help", aliases=["bar_1", "bar_2"])
44 parser_bar.add_argument("z", help="string")
45 parser_bar.set_defaults(func=base_bar)
46
47 # create the parser for the "helpless" subcommand
48 # This subcommand has aliases and no help text.
49 parser_helpless = base_subparsers.add_parser("helpless", aliases=["helpless_1", "helpless_2"])
50 parser_helpless.add_argument("z", help="string")
51 parser_helpless.set_defaults(func=base_helpless)
52
53 @cmd2.with_argparser(base_parser)
54 def do_base(self, args) -> None:
55 """Base command help"""
56 # Call whatever subcommand function was selected
57 func = args.func
58 func(self, args)
59
60
61 @pytest.fixture
62 def subcommand_app():
63 return WithCommandSets(auto_load_commands=False, command_sets=[SubcommandSet(1)])
64
65
66 def test_subcommand_foo(subcommand_app) -> None:
67 out, _err = run_cmd(subcommand_app, "base foo -x2 5.0")
68 assert out == ["10.0"]
69
70
71 def test_subcommand_bar(subcommand_app) -> None:
72 out, _err = run_cmd(subcommand_app, "base bar baz")
73 assert out == ["((baz))"]
74
75
76 def test_subcommand_invalid(subcommand_app) -> None:
77 _out, err = run_cmd(subcommand_app, "base baz")
78 assert err[0].startswith("Usage: base")
79 assert err[1].startswith("Error: argument SUBCOMMAND: invalid choice: 'baz'")
80
81
82 def test_subcommand_base_help(subcommand_app) -> None:
83 out, _err = run_cmd(subcommand_app, "help base")
84 assert out[0].startswith("Usage: base")
85 assert out[1] == ""
86 assert out[2] == "Base command help"
87
88
89 def test_subcommand_help(subcommand_app) -> None:
90 # foo has no aliases
91 out, _err = run_cmd(subcommand_app, "help base foo")
92 assert out[0].startswith("Usage: base foo")
93 assert out[1] == ""
94 assert out[2] == "Positional Arguments:"
95
96 # bar has aliases (usage should never show alias name)
97 out, _err = run_cmd(subcommand_app, "help base bar")
98 assert out[0].startswith("Usage: base bar")
99 assert out[1] == ""
100 assert out[2] == "Positional Arguments:"
101
102 out, _err = run_cmd(subcommand_app, "help base bar_1")
103 assert out[0].startswith("Usage: base bar")
104 assert out[1] == ""
105 assert out[2] == "Positional Arguments:"
106
107 out, _err = run_cmd(subcommand_app, "help base bar_2")
108 assert out[0].startswith("Usage: base bar")
109 assert out[1] == ""
110 assert out[2] == "Positional Arguments:"
111
112 # helpless has aliases and no help text (usage should never show alias name)
113 out, _err = run_cmd(subcommand_app, "help base helpless")
114 assert out[0].startswith("Usage: base helpless")
115 assert out[1] == ""
116 assert out[2] == "Positional Arguments:"
117
118 out, _err = run_cmd(subcommand_app, "help base helpless_1")
119 assert out[0].startswith("Usage: base helpless")
120 assert out[1] == ""
121 assert out[2] == "Positional Arguments:"
122
123 out, _err = run_cmd(subcommand_app, "help base helpless_2")
124 assert out[0].startswith("Usage: base helpless")
125 assert out[1] == ""
126 assert out[2] == "Positional Arguments:"
127
128
129 def test_subcommand_invalid_help(subcommand_app) -> None:
130 out, _err = run_cmd(subcommand_app, "help base baz")
131 assert out[0].startswith("Usage: base")