@@ -62,6 +62,26 @@ def do_elderberry(self, ns: argparse.Namespace):
62
62
self ._cmd .poutput ('Elderberry {}!!' .format (ns .arg1 ))
63
63
self ._cmd .last_result = {'arg1' : ns .arg1 }
64
64
65
+ # Test that CommandSet with as_subcommand_to decorator successfully loads
66
+ # during `cmd2.Cmd.__init__()`.
67
+ main_parser = cmd2 .Cmd2ArgumentParser (description = "Main Command" )
68
+ main_subparsers = main_parser .add_subparsers (dest = 'subcommand' , metavar = 'SUBCOMMAND' )
69
+ main_subparsers .required = True
70
+
71
+ @cmd2 .with_category ('Alone' )
72
+ @cmd2 .with_argparser (main_parser )
73
+ def do_main (self , args : argparse .Namespace ) -> None :
74
+ # Call handler for whatever subcommand was selected
75
+ handler = args .get_handler ()
76
+ handler (args )
77
+
78
+ # main -> sub
79
+ subcmd_parser = cmd2 .Cmd2ArgumentParser (add_help = False , description = "Sub Command" )
80
+
81
+ @cmd2 .as_subcommand_to ('main' , 'sub' , subcmd_parser , help = "sub command" )
82
+ def subcmd_func (self , args : argparse .Namespace ) -> None :
83
+ self ._cmd .poutput ("Subcommand Ran" )
84
+
65
85
66
86
@cmd2 .with_default_category ('Command Set B' )
67
87
class CommandSetB (CommandSetBase ):
@@ -87,6 +107,11 @@ def test_autoload_commands(command_sets_app):
87
107
88
108
assert 'Alone' in cmds_cats
89
109
assert 'elderberry' in cmds_cats ['Alone' ]
110
+ assert 'main' in cmds_cats ['Alone' ]
111
+
112
+ # Test subcommand was autoloaded
113
+ result = command_sets_app .app_cmd ('main sub' )
114
+ assert 'Subcommand Ran' in result .stdout
90
115
91
116
assert 'Also Alone' in cmds_cats
92
117
assert 'durian' in cmds_cats ['Also Alone' ]
@@ -150,6 +175,11 @@ def test_load_commands(command_sets_manual):
150
175
151
176
assert 'Alone' in cmds_cats
152
177
assert 'elderberry' in cmds_cats ['Alone' ]
178
+ assert 'main' in cmds_cats ['Alone' ]
179
+
180
+ # Test subcommand was loaded
181
+ result = command_sets_manual .app_cmd ('main sub' )
182
+ assert 'Subcommand Ran' in result .stdout
153
183
154
184
assert 'Fruits' in cmds_cats
155
185
assert 'cranberry' in cmds_cats ['Fruits' ]
@@ -172,6 +202,11 @@ def test_load_commands(command_sets_manual):
172
202
173
203
assert 'Alone' in cmds_cats
174
204
assert 'elderberry' in cmds_cats ['Alone' ]
205
+ assert 'main' in cmds_cats ['Alone' ]
206
+
207
+ # Test subcommand was loaded
208
+ result = command_sets_manual .app_cmd ('main sub' )
209
+ assert 'Subcommand Ran' in result .stdout
175
210
176
211
assert 'Fruits' in cmds_cats
177
212
assert 'cranberry' in cmds_cats ['Fruits' ]
0 commit comments