@@ -21,14 +21,30 @@ class CommandSetBase(cmd2.CommandSet):
2121
2222@cmd2 .with_default_category ('Fruits' )
2323class 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+
2440 def do_apple (self , statement : cmd2 .Statement ):
2541 self ._cmd .poutput ('Apple!' )
2642
2743 def do_banana (self , statement : cmd2 .Statement ):
2844 """Banana Command"""
2945 self ._cmd .poutput ('Banana!!' )
3046
31- cranberry_parser = cmd2 .Cmd2ArgumentParser ('cranberry' )
47+ cranberry_parser = cmd2 .Cmd2ArgumentParser ()
3248 cranberry_parser .add_argument ('arg1' , choices = ['lemonade' , 'juice' , 'sauce' ])
3349
3450 @cmd2 .with_argparser (cranberry_parser , with_unknown_args = True )
@@ -53,7 +69,7 @@ def do_durian(self, args: List[str]):
5369 def complete_durian (self , text : str , line : str , begidx : int , endidx : int ) -> List [str ]:
5470 return utils .basic_complete (text , line , begidx , endidx , ['stinks' , 'smells' , 'disgusting' ])
5571
56- elderberry_parser = cmd2 .Cmd2ArgumentParser ('elderberry' )
72+ elderberry_parser = cmd2 .Cmd2ArgumentParser ()
5773 elderberry_parser .add_argument ('arg1' )
5874
5975 @cmd2 .with_category ('Alone' )
@@ -158,7 +174,7 @@ def test_custom_construct_commandsets():
158174 assert command_set_2 not in matches
159175
160176
161- def test_load_commands (command_sets_manual ):
177+ def test_load_commands (command_sets_manual , capsys ):
162178
163179 # now install a command set and verify the commands are now present
164180 cmd_set = CommandSetA ()
@@ -171,6 +187,11 @@ def test_load_commands(command_sets_manual):
171187 assert command_sets_manual .find_commandsets (CommandSetA )[0 ] is cmd_set
172188 assert command_sets_manual .find_commandset_for_command ('elderberry' ) is cmd_set
173189
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+
174195 cmds_cats , cmds_doc , cmds_undoc , help_topics = command_sets_manual ._build_command_info ()
175196
176197 assert 'Alone' in cmds_cats
@@ -192,6 +213,11 @@ def test_load_commands(command_sets_manual):
192213 assert 'Alone' not in cmds_cats
193214 assert 'Fruits' not in cmds_cats
194215
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+
195221 # uninstall a second time and verify no errors happen
196222 command_sets_manual .unregister_command_set (cmd_set )
197223
@@ -298,7 +324,7 @@ def __init__(self, dummy):
298324 self ._dummy = dummy # prevents autoload
299325 self ._cut_called = False
300326
301- cut_parser = cmd2 .Cmd2ArgumentParser ('cut' )
327+ cut_parser = cmd2 .Cmd2ArgumentParser ()
302328 cut_subparsers = cut_parser .add_subparsers (title = 'item' , help = 'item to cut' )
303329
304330 def namespace_provider (self ) -> argparse .Namespace :
@@ -319,8 +345,7 @@ def do_cut(self, ns: argparse.Namespace):
319345 self ._cmd .pwarning ('This command does nothing without sub-parsers registered' )
320346 self ._cmd .do_help ('cut' )
321347
322-
323- stir_parser = cmd2 .Cmd2ArgumentParser ('stir' )
348+ stir_parser = cmd2 .Cmd2ArgumentParser ()
324349 stir_subparsers = stir_parser .add_subparsers (title = 'item' , help = 'what to stir' )
325350
326351 @cmd2 .with_argparser (stir_parser , ns_provider = namespace_provider )
@@ -592,7 +617,7 @@ class AppWithSubCommands(cmd2.Cmd):
592617 def __init__ (self , * args , ** kwargs ):
593618 super (AppWithSubCommands , self ).__init__ (* args , ** kwargs )
594619
595- cut_parser = cmd2 .Cmd2ArgumentParser ('cut' )
620+ cut_parser = cmd2 .Cmd2ArgumentParser ()
596621 cut_subparsers = cut_parser .add_subparsers (title = 'item' , help = 'item to cut' )
597622
598623 @cmd2 .with_argparser (cut_parser )
@@ -853,7 +878,7 @@ class BadSubcommandApp(cmd2.Cmd):
853878 def __init__ (self , * args , ** kwargs ):
854879 super (BadSubcommandApp , self ).__init__ (* args , ** kwargs )
855880
856- cut_parser = cmd2 .Cmd2ArgumentParser ('cut' )
881+ cut_parser = cmd2 .Cmd2ArgumentParser ()
857882 cut_subparsers = cut_parser .add_subparsers (title = 'item' , help = 'item to cut' )
858883
859884 @cmd2 .with_argparser (cut_parser )
0 commit comments