summaryrefslogtreecommitdiff
path: root/cmd2
diff options
context:
space:
mode:
authorEric Lin <anselor@gmail.com>2020-06-13 12:30:33 -0400
committeranselor <anselor@gmail.com>2020-08-04 13:38:08 -0400
commitc88de7dfcfed716e81d06775b6e7929e4e01428c (patch)
treee8d2abb125ff2921f6de4607059fd7335dd70992 /cmd2
parente32cccc4e599c924c3fd5f8376f7efd085f88019 (diff)
downloadcmd2-git-c88de7dfcfed716e81d06775b6e7929e4e01428c.tar.gz
add ability to remove commands and commandsets
Issue #943
Diffstat (limited to 'cmd2')
-rw-r--r--cmd2/cmd2.py50
-rw-r--r--cmd2/command_definition.py9
2 files changed, 54 insertions, 5 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index edf2a643..ef273d15 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -244,8 +244,8 @@ class Cmd(cmd.Cmd):
shortcuts=shortcuts)
# Load modular commands
- self._installed_functions: List[str] = []
- self._installed_command_sets: List[CommandSet] = []
+ self._installed_functions = [] # type: List[str]
+ self._installed_command_sets = [] # type: List[CommandSet]
if command_sets:
for command_set in command_sets:
self.install_command_set(command_set)
@@ -469,7 +469,34 @@ class Cmd(cmd.Cmd):
delattr(self, attrib)
raise
- def install_command_function(self, cmd_name: str, cmd_func: Callable, cmd_completer: Callable, cmd_help: Callable):
+ def uninstall_command_set(self, cmdset: CommandSet):
+ """
+ Uninstalls an CommandSet and unloads all associated commands
+ :param cmdset: CommandSet to uninstall
+ """
+ if cmdset in self._installed_command_sets:
+ methods = inspect.getmembers(
+ cmdset,
+ predicate=lambda meth: inspect.ismethod(meth) and meth.__name__.startswith(COMMAND_FUNC_PREFIX))
+
+ for method in methods:
+ cmd_name = method[0][len(COMMAND_FUNC_PREFIX):]
+
+ delattr(self, COMMAND_FUNC_PREFIX + cmd_name)
+
+ if hasattr(self, COMPLETER_FUNC_PREFIX + cmd_name):
+ delattr(self, COMPLETER_FUNC_PREFIX + cmd_name)
+ if hasattr(self, HELP_FUNC_PREFIX + cmd_name):
+ delattr(self, HELP_FUNC_PREFIX + cmd_name)
+
+ cmdset.on_unregister(self)
+ self._installed_command_sets.remove(cmdset)
+
+ def install_command_function(self,
+ cmd_name: str,
+ cmd_func: Callable,
+ cmd_completer: Optional[Callable],
+ cmd_help: Optional[Callable]):
"""
Installs a command by passing in functions for the command, completion, and help
@@ -483,7 +510,8 @@ class Cmd(cmd.Cmd):
if not valid:
raise ValueError("Invalid command name {!r}: {}".format(cmd_name, errmsg))
- assert getattr(self, COMMAND_FUNC_PREFIX + cmd_name, None) is None, 'Duplicate command function registered: ' + cmd_name
+ assert getattr(self, COMMAND_FUNC_PREFIX + cmd_name, None) is None,\
+ 'Duplicate command function registered: ' + cmd_name
setattr(self, COMMAND_FUNC_PREFIX + cmd_name, types.MethodType(cmd_func, self))
self._installed_functions.append(cmd_name)
if cmd_completer is not None:
@@ -495,6 +523,20 @@ class Cmd(cmd.Cmd):
'Duplicate command help registered: ' + HELP_FUNC_PREFIX + cmd_name
setattr(self, HELP_FUNC_PREFIX + cmd_name, types.MethodType(cmd_help, self))
+ def uninstall_command(self, cmd_name: str):
+ """
+ Uninstall an installed command and any associated completer or help functions
+ :param cmd_name: Command to uninstall
+ """
+ if cmd_name in self._installed_functions:
+ delattr(self, COMMAND_FUNC_PREFIX + cmd_name)
+
+ if hasattr(self, COMPLETER_FUNC_PREFIX + cmd_name):
+ delattr(self, COMPLETER_FUNC_PREFIX + cmd_name)
+ if hasattr(self, HELP_FUNC_PREFIX + cmd_name):
+ delattr(self, HELP_FUNC_PREFIX + cmd_name)
+ self._installed_functions.remove(cmd_name)
+
def add_settable(self, settable: Settable) -> None:
"""
Convenience method to add a settable parameter to ``self.settables``
diff --git a/cmd2/command_definition.py b/cmd2/command_definition.py
index a235525d..115cef64 100644
--- a/cmd2/command_definition.py
+++ b/cmd2/command_definition.py
@@ -87,6 +87,7 @@ def register_command(cmd_func: Callable[['Cmd', Union['Statement', 'argparse.Nam
break
_UNBOUND_COMMANDS.append((cmd_name, cmd_func, cmd_completer, cmd_help))
+ return cmd_func
def with_default_category(category: str):
@@ -132,6 +133,12 @@ class CommandSet(object):
to perform an initialization requiring access to the Cmd object.
:param cmd: The cmd2 main application
- :return: None
"""
self._cmd = cmd
+
+ def on_unregister(self, cmd: 'Cmd'):
+ """
+ Called by ``cmd2.Cmd`` when a CommandSet is unregistered and removed.
+ :param cmd:
+ """
+ self._cmd = None