Skip to content

Commit a1afa7c

Browse files
committed
Added function to report that a disabled command has been run.
Not adding disabled commands to the history when run.
1 parent c5872ac commit a1afa7c

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

cmd2/cmd2.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,8 +1987,9 @@ def onecmd(self, statement: Union[Statement, str]) -> bool:
19871987
else:
19881988
func = self.cmd_func(statement.command)
19891989
if func:
1990-
# Since we have a valid command store it in the history
1991-
if statement.command not in self.exclude_from_history:
1990+
# Check to see if this command should be stored in history
1991+
if statement.command not in self.exclude_from_history \
1992+
and statement.command not in self.disabled_commands:
19921993
self.history.append(statement)
19931994

19941995
stop = func(statement)
@@ -3676,8 +3677,9 @@ def disable_command(self, command: str, message_to_print: str) -> None:
36763677
self.disabled_commands[command] = dc
36773678

36783679
# Overwrite the command and help functions to print the message
3679-
setattr(self, self.cmd_func_name(command), functools.partial(self.poutput, message_to_print + '\n'))
3680-
setattr(self, help_func_name, functools.partial(self.poutput, message_to_print + '\n'))
3680+
new_func = functools.partial(self._report_disabled_command_usage, message_to_print=message_to_print)
3681+
setattr(self, self.cmd_func_name(command), new_func)
3682+
setattr(self, help_func_name, new_func)
36813683

36823684
def disable_category(self, category: str, message_to_print: str) -> None:
36833685
"""
@@ -3696,6 +3698,16 @@ def disable_category(self, category: str, message_to_print: str) -> None:
36963698
if cmd_category is not None and cmd_category == category:
36973699
self.disable_command(cmd_name, message_to_print)
36983700

3701+
# noinspection PyUnusedLocal
3702+
def _report_disabled_command_usage(self, *args, message_to_print: str, **kwargs) -> None:
3703+
"""
3704+
Report when a disabled command has been run or had help called on it
3705+
:param args: not used
3706+
:param message_to_print: the message reporting that the command is disabled
3707+
:param kwargs: not used
3708+
"""
3709+
self.poutput(message_to_print)
3710+
36993711
def cmdloop(self, intro: Optional[str] = None) -> None:
37003712
"""This is an outer wrapper around _cmdloop() which deals with extra features provided by cmd2.
37013713

tests/test_cmd2.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2267,3 +2267,11 @@ def test_disable_command_twice(disable_commands_app):
22672267
disable_commands_app.disable_command('has_help_func', message_to_print)
22682268
new_len = len(disable_commands_app.disabled_commands)
22692269
assert saved_len == new_len
2270+
2271+
def test_disabled_command_not_in_history(disable_commands_app):
2272+
message_to_print = 'These commands are currently disabled'
2273+
disable_commands_app.disable_command('has_help_func', message_to_print)
2274+
2275+
saved_len = len(disable_commands_app.history)
2276+
run_cmd(disable_commands_app, 'has_help_func')
2277+
assert saved_len == len(disable_commands_app.history)

0 commit comments

Comments
 (0)