@@ -4,7 +4,7 @@ Modular Commands
4
4
Overview
5
5
--------
6
6
7
- Cmd2 also enables developers to modularize their command definitions into Command Sets. Command sets represent
7
+ Cmd2 also enables developers to modularize their command definitions into `` CommandSet `` objects. CommandSets represent
8
8
a logical grouping of commands within an cmd2 application. By default, all CommandSets will be discovered and loaded
9
9
automatically when the cmd2.Cmd class is instantiated with this mixin. This also enables the developer to
10
10
dynamically add/remove commands from the cmd2 application. This could be useful for loadable plugins that
@@ -21,10 +21,14 @@ Features
21
21
* Dynamically Loadable/Unloadable Commands - Command functions and CommandSets can both be loaded and unloaded
22
22
dynamically during application execution. This can enable features such as dynamically loaded modules that
23
23
add additional commands.
24
+ * Events handlers - Four event handlers are provided in ``CommandSet `` class for custom initialization
25
+ and cleanup steps. See :ref: `features/modular_commands:Event Handlers `.
24
26
* Subcommand Injection - Subcommands can be defined separately from the base command. This allows for a more
25
27
action-centric instead of object-centric command system while still organizing your code and handlers around the
26
28
objects being managed.
27
29
30
+ See API documentation for :attr: `cmd2.command_definition.CommandSet `
31
+
28
32
See the examples for more details: https://github.com/python-cmd2/cmd2/tree/master/plugins/command_sets/examples
29
33
30
34
@@ -207,6 +211,30 @@ You may need to disable command auto-loading if you need dynamically load comman
207
211
app.cmdloop()
208
212
209
213
214
+ Event Handlers
215
+ --------------
216
+ The following functions are called at different points in the ``CommandSet `` life cycle.
217
+
218
+ ``on_register(self, cmd) -> None `` - Called by cmd2.Cmd as the first step to
219
+ registering a CommandSet. The commands defined in this class have not be
220
+ added to the CLI object at this point. Subclasses can override this to
221
+ perform any initialization requiring access to the Cmd object
222
+ (e.g. configure commands and their parsers based on CLI state data).
223
+
224
+ ``on_registered(self) -> None `` - Called by cmd2.Cmd after a CommandSet is
225
+ registered and all its commands have been added to the CLI. Subclasses can
226
+ override this to perform custom steps related to the newly added commands
227
+ (e.g. setting them to a disabled state).
228
+
229
+ ``on_unregister(self) -> None `` - Called by ``cmd2.Cmd `` as the first step to
230
+ unregistering a CommandSet. Subclasses can override this to perform any cleanup
231
+ steps which require their commands being registered in the CLI.
232
+
233
+ ``on_unregistered(self) -> None `` - Called by ``cmd2.Cmd `` after a CommandSet
234
+ has been unregistered and all its commands removed from the CLI. Subclasses can
235
+ override this to perform remaining cleanup steps.
236
+
237
+
210
238
Injecting Subcommands
211
239
----------------------
212
240
0 commit comments