summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2019-03-10 13:44:39 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2019-03-10 13:44:39 -0400
commita1afa7cc454c476280dda3201e8ed21d4fce9dfd (patch)
tree2fdb4fa7337bb94ce5ab59ad815572017261f74a
parentc5872ac9ffa9047087340fe340b8a88bf2a902d6 (diff)
downloadcmd2-git-a1afa7cc454c476280dda3201e8ed21d4fce9dfd.tar.gz
Added function to report that a disabled command has been run.
Not adding disabled commands to the history when run.
-rw-r--r--cmd2/cmd2.py20
-rw-r--r--tests/test_cmd2.py8
2 files changed, 24 insertions, 4 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index c9661a1a..957ebffd 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -1987,8 +1987,9 @@ class Cmd(cmd.Cmd):
else:
func = self.cmd_func(statement.command)
if func:
- # Since we have a valid command store it in the history
- if statement.command not in self.exclude_from_history:
+ # Check to see if this command should be stored in history
+ if statement.command not in self.exclude_from_history \
+ and statement.command not in self.disabled_commands:
self.history.append(statement)
stop = func(statement)
@@ -3676,8 +3677,9 @@ class Cmd(cmd.Cmd):
self.disabled_commands[command] = dc
# Overwrite the command and help functions to print the message
- setattr(self, self.cmd_func_name(command), functools.partial(self.poutput, message_to_print + '\n'))
- setattr(self, help_func_name, functools.partial(self.poutput, message_to_print + '\n'))
+ new_func = functools.partial(self._report_disabled_command_usage, message_to_print=message_to_print)
+ setattr(self, self.cmd_func_name(command), new_func)
+ setattr(self, help_func_name, new_func)
def disable_category(self, category: str, message_to_print: str) -> None:
"""
@@ -3696,6 +3698,16 @@ class Cmd(cmd.Cmd):
if cmd_category is not None and cmd_category == category:
self.disable_command(cmd_name, message_to_print)
+ # noinspection PyUnusedLocal
+ def _report_disabled_command_usage(self, *args, message_to_print: str, **kwargs) -> None:
+ """
+ Report when a disabled command has been run or had help called on it
+ :param args: not used
+ :param message_to_print: the message reporting that the command is disabled
+ :param kwargs: not used
+ """
+ self.poutput(message_to_print)
+
def cmdloop(self, intro: Optional[str] = None) -> None:
"""This is an outer wrapper around _cmdloop() which deals with extra features provided by cmd2.
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index 5e9e4ce6..b3942203 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -2267,3 +2267,11 @@ def test_disable_command_twice(disable_commands_app):
disable_commands_app.disable_command('has_help_func', message_to_print)
new_len = len(disable_commands_app.disabled_commands)
assert saved_len == new_len
+
+def test_disabled_command_not_in_history(disable_commands_app):
+ message_to_print = 'These commands are currently disabled'
+ disable_commands_app.disable_command('has_help_func', message_to_print)
+
+ saved_len = len(disable_commands_app.history)
+ run_cmd(disable_commands_app, 'has_help_func')
+ assert saved_len == len(disable_commands_app.history)