@@ -21,14 +21,30 @@ class CommandSetBase(cmd2.CommandSet):
21
21
22
22
@cmd2 .with_default_category ('Fruits' )
23
23
class CommandSetA (CommandSetBase ):
24
+ def on_register (self , cmd ) -> None :
25
+ super ().on_register (cmd )
26
+ print ("in on_register now" )
27
+
28
+ def on_registered (self ) -> None :
29
+ super ().on_registered ()
30
+ print ("in on_registered now" )
31
+
32
+ def on_unregister (self ) -> None :
33
+ super ().on_unregister ()
34
+ print ("in on_unregister now" )
35
+
36
+ def on_unregistered (self ) -> None :
37
+ super ().on_unregistered ()
38
+ print ("in on_unregistered now" )
39
+
24
40
def do_apple (self , statement : cmd2 .Statement ):
25
41
self ._cmd .poutput ('Apple!' )
26
42
27
43
def do_banana (self , statement : cmd2 .Statement ):
28
44
"""Banana Command"""
29
45
self ._cmd .poutput ('Banana!!' )
30
46
31
- cranberry_parser = cmd2 .Cmd2ArgumentParser ('cranberry' )
47
+ cranberry_parser = cmd2 .Cmd2ArgumentParser ()
32
48
cranberry_parser .add_argument ('arg1' , choices = ['lemonade' , 'juice' , 'sauce' ])
33
49
34
50
@cmd2 .with_argparser (cranberry_parser , with_unknown_args = True )
@@ -53,7 +69,7 @@ def do_durian(self, args: List[str]):
53
69
def complete_durian (self , text : str , line : str , begidx : int , endidx : int ) -> List [str ]:
54
70
return utils .basic_complete (text , line , begidx , endidx , ['stinks' , 'smells' , 'disgusting' ])
55
71
56
- elderberry_parser = cmd2 .Cmd2ArgumentParser ('elderberry' )
72
+ elderberry_parser = cmd2 .Cmd2ArgumentParser ()
57
73
elderberry_parser .add_argument ('arg1' )
58
74
59
75
@cmd2 .with_category ('Alone' )
@@ -158,7 +174,7 @@ def test_custom_construct_commandsets():
158
174
assert command_set_2 not in matches
159
175
160
176
161
- def test_load_commands (command_sets_manual ):
177
+ def test_load_commands (command_sets_manual , capsys ):
162
178
163
179
# now install a command set and verify the commands are now present
164
180
cmd_set = CommandSetA ()
@@ -171,6 +187,11 @@ def test_load_commands(command_sets_manual):
171
187
assert command_sets_manual .find_commandsets (CommandSetA )[0 ] is cmd_set
172
188
assert command_sets_manual .find_commandset_for_command ('elderberry' ) is cmd_set
173
189
190
+ # Make sure registration callbacks ran
191
+ out , err = capsys .readouterr ()
192
+ assert "in on_register now" in out
193
+ assert "in on_registered now" in out
194
+
174
195
cmds_cats , cmds_doc , cmds_undoc , help_topics = command_sets_manual ._build_command_info ()
175
196
176
197
assert 'Alone' in cmds_cats
@@ -192,6 +213,11 @@ def test_load_commands(command_sets_manual):
192
213
assert 'Alone' not in cmds_cats
193
214
assert 'Fruits' not in cmds_cats
194
215
216
+ # Make sure unregistration callbacks ran
217
+ out , err = capsys .readouterr ()
218
+ assert "in on_unregister now" in out
219
+ assert "in on_unregistered now" in out
220
+
195
221
# uninstall a second time and verify no errors happen
196
222
command_sets_manual .unregister_command_set (cmd_set )
197
223
@@ -298,7 +324,7 @@ def __init__(self, dummy):
298
324
self ._dummy = dummy # prevents autoload
299
325
self ._cut_called = False
300
326
301
- cut_parser = cmd2 .Cmd2ArgumentParser ('cut' )
327
+ cut_parser = cmd2 .Cmd2ArgumentParser ()
302
328
cut_subparsers = cut_parser .add_subparsers (title = 'item' , help = 'item to cut' )
303
329
304
330
def namespace_provider (self ) -> argparse .Namespace :
@@ -319,8 +345,7 @@ def do_cut(self, ns: argparse.Namespace):
319
345
self ._cmd .pwarning ('This command does nothing without sub-parsers registered' )
320
346
self ._cmd .do_help ('cut' )
321
347
322
-
323
- stir_parser = cmd2 .Cmd2ArgumentParser ('stir' )
348
+ stir_parser = cmd2 .Cmd2ArgumentParser ()
324
349
stir_subparsers = stir_parser .add_subparsers (title = 'item' , help = 'what to stir' )
325
350
326
351
@cmd2 .with_argparser (stir_parser , ns_provider = namespace_provider )
@@ -592,7 +617,7 @@ class AppWithSubCommands(cmd2.Cmd):
592
617
def __init__ (self , * args , ** kwargs ):
593
618
super (AppWithSubCommands , self ).__init__ (* args , ** kwargs )
594
619
595
- cut_parser = cmd2 .Cmd2ArgumentParser ('cut' )
620
+ cut_parser = cmd2 .Cmd2ArgumentParser ()
596
621
cut_subparsers = cut_parser .add_subparsers (title = 'item' , help = 'item to cut' )
597
622
598
623
@cmd2 .with_argparser (cut_parser )
@@ -853,7 +878,7 @@ class BadSubcommandApp(cmd2.Cmd):
853
878
def __init__ (self , * args , ** kwargs ):
854
879
super (BadSubcommandApp , self ).__init__ (* args , ** kwargs )
855
880
856
- cut_parser = cmd2 .Cmd2ArgumentParser ('cut' )
881
+ cut_parser = cmd2 .Cmd2ArgumentParser ()
857
882
cut_subparsers = cut_parser .add_subparsers (title = 'item' , help = 'item to cut' )
858
883
859
884
@cmd2 .with_argparser (cut_parser )
0 commit comments