diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2020-09-01 16:49:34 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2020-09-01 16:49:34 -0400 |
commit | 77a36b1b20b32ddca608421b8831938ada46c06c (patch) | |
tree | ac9655841762a693ee4df431782a2ddb12793c19 | |
parent | 36b0b75265942fe375545beb7d2d8a2c5f6f63c4 (diff) | |
download | cmd2-git-77a36b1b20b32ddca608421b8831938ada46c06c.tar.gz |
Added on_registered() callback to CommandSet
-rw-r--r-- | CHANGELOG.md | 5 | ||||
-rw-r--r-- | cmd2/cmd2.py | 1 | ||||
-rw-r--r-- | cmd2/command_definition.py | 11 |
3 files changed, 15 insertions, 2 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c65bb8a..a22bfdf8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 1.3.9 (September 01, 2020) +* Enhancements + * Added `on_registered()` callback to `CommandSet` class. This is called by `cmd2.Cmd` after a + `CommandSet` is registered and all its commands have been added to the CLI. + ## 1.3.8 (August 28, 2020) * Bug Fixes * Fixed issue where subcommand added with `@as_subcommand_to` decorator did not display help diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index d768085a..8fd3d243 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -503,6 +503,7 @@ class Cmd(cmd.Cmd): self._installed_command_sets.append(cmdset) self._register_subcommands(cmdset) + cmdset.on_registered() except Exception: for attrib in installed_attributes: delattr(self, attrib) diff --git a/cmd2/command_definition.py b/cmd2/command_definition.py index 27a044bc..3c663054 100644 --- a/cmd2/command_definition.py +++ b/cmd2/command_definition.py @@ -53,7 +53,7 @@ class CommandSet(object): def __init__(self): self._cmd = None # type: Optional[cmd2.Cmd] - def on_register(self, cmd): + def on_register(self, cmd) -> None: """ Called by cmd2.Cmd when a CommandSet is registered. Subclasses can override this to perform an initialization requiring access to the Cmd object. @@ -66,7 +66,14 @@ class CommandSet(object): else: raise CommandSetRegistrationError('This CommandSet has already been registered') - def on_unregister(self): + def on_registered(self) -> None: + """ + Called by cmd2.Cmd after a CommandSet is registered and all its commands have been added + to the CLI. Subclasses can override this to perform custom steps. + """ + pass + + def on_unregister(self) -> None: """ Called by ``cmd2.Cmd`` when a CommandSet is unregistered and removed. |