Skip to content

Commit 3747fb8

Browse files
committed
Added ability to place the command name in a disabled command message. This is useful when disabling an entire category of commands.
1 parent 258ad43 commit 3747fb8

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

cmd2/cmd2.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ def __subclasshook__(cls, C):
134134
ALPHABETICAL_SORT_KEY = utils.norm_fold
135135
NATURAL_SORT_KEY = utils.natural_keys
136136

137+
# Used as the command name placeholder in disabled command messages.
138+
COMMAND_NAME = "<COMMAND_NAME>"
139+
137140

138141
def categorize(func: Union[Callable, Iterable], category: str) -> None:
139142
"""Categorize a function.
@@ -3681,7 +3684,8 @@ def disable_command(self, command: str, message_to_print: str) -> None:
36813684
help_function=getattr(self, help_func_name, None))
36823685

36833686
# Overwrite the command and help functions to print the message
3684-
new_func = functools.partial(self._report_disabled_command_usage, message_to_print=message_to_print)
3687+
new_func = functools.partial(self._report_disabled_command_usage,
3688+
message_to_print=message_to_print.replace(COMMAND_NAME, command))
36853689
setattr(self, self.cmd_func_name(command), new_func)
36863690
setattr(self, help_func_name, new_func)
36873691

tests/test_cmd2.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2280,3 +2280,11 @@ def test_disabled_command_not_in_history(disable_commands_app):
22802280
saved_len = len(disable_commands_app.history)
22812281
run_cmd(disable_commands_app, 'has_help_func')
22822282
assert saved_len == len(disable_commands_app.history)
2283+
2284+
def test_disabled_message_command_name(disable_commands_app, capsys):
2285+
message_to_print = '{} is currently disabled'.format(cmd2.cmd2.COMMAND_NAME)
2286+
disable_commands_app.disable_command('has_help_func', message_to_print)
2287+
2288+
run_cmd(disable_commands_app, 'has_help_func')
2289+
out, err = capsys.readouterr()
2290+
assert err.startswith('has_help_func is currently disabled')

0 commit comments

Comments
 (0)