Skip to content

Commit 177297a

Browse files
authored
Merge pull request #646 from python-cmd2/disabling_tweaks
Disabling tweaks
2 parents 677e8bb + c014b1d commit 177297a

File tree

4 files changed

+50
-16
lines changed

4 files changed

+50
-16
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.9.12 (TBD, 2019)
2+
* Enhancements
3+
* Added ability to include command name placeholders in the message printed when trying to run a disabled command.
4+
* See docstring for ``disable_command()`` or ``disable_category()`` for more details.
5+
16
## 0.9.11 (March 13, 2019)
27
* Bug Fixes
38
* Fixed bug in how **history** command deals with multiline commands when output to a script

cmd2/cmd2.py

Lines changed: 16 additions & 3 deletions
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.
@@ -2054,7 +2057,8 @@ def default(self, statement: Statement) -> Optional[bool]:
20542057

20552058
return self.do_shell(statement.command_and_args)
20562059
else:
2057-
self.poutput('*** {} is not a recognized command, alias, or macro\n'.format(statement.command))
2060+
self.perror('*** {} is not a recognized command, alias, or macro'.format(statement.command),
2061+
err_color=Fore.RESET, traceback_war=False)
20582062

20592063
def pseudo_raw_input(self, prompt: str) -> str:
20602064
"""Began life as a copy of cmd's cmdloop; like raw_input but
@@ -3661,6 +3665,10 @@ def disable_command(self, command: str, message_to_print: str) -> None:
36613665
Disable a command and overwrite its functions
36623666
:param command: the command being disabled
36633667
:param message_to_print: what to print when this command is run or help is called on it while disabled
3668+
3669+
The variable COMMAND_NAME can be used as a placeholder for the name of the
3670+
command being disabled.
3671+
ex: message_to_print = "{} is currently disabled".format(COMMAND_NAME)
36643672
"""
36653673
import functools
36663674

@@ -3680,7 +3688,8 @@ def disable_command(self, command: str, message_to_print: str) -> None:
36803688
help_function=getattr(self, help_func_name, None))
36813689

36823690
# Overwrite the command and help functions to print the message
3683-
new_func = functools.partial(self._report_disabled_command_usage, message_to_print=message_to_print)
3691+
new_func = functools.partial(self._report_disabled_command_usage,
3692+
message_to_print=message_to_print.replace(COMMAND_NAME, command))
36843693
setattr(self, self.cmd_func_name(command), new_func)
36853694
setattr(self, help_func_name, new_func)
36863695

@@ -3690,6 +3699,10 @@ def disable_category(self, category: str, message_to_print: str) -> None:
36903699
:param category: the category to disable
36913700
:param message_to_print: what to print when anything in this category is run or help is called on it
36923701
while disabled
3702+
3703+
The variable COMMAND_NAME can be used as a placeholder for the name of the
3704+
command being disabled.
3705+
ex: message_to_print = "{} is currently disabled".format(COMMAND_NAME)
36933706
"""
36943707
all_commands = self.get_all_commands()
36953708

@@ -3706,7 +3719,7 @@ def _report_disabled_command_usage(self, *args, message_to_print: str, **kwargs)
37063719
:param message_to_print: the message reporting that the command is disabled
37073720
:param kwargs: not used
37083721
"""
3709-
self.poutput(message_to_print)
3722+
self.perror(message_to_print, err_color=Fore.RESET, traceback_war=False)
37103723

37113724
def cmdloop(self, intro: Optional[str] = None) -> None:
37123725
"""This is an outer wrapper around _cmdloop() which deals with extra features provided by cmd2.

examples/help_categories.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import argparse
88

99
import cmd2
10+
from cmd2.cmd2 import COMMAND_NAME
1011

1112

1213
class HelpCategories(cmd2.Cmd):
@@ -143,7 +144,9 @@ def do_version(self, _):
143144
@cmd2.with_category("Command Management")
144145
def do_disable_commands(self, _):
145146
"""Disable the Application Management commands"""
146-
self.disable_category(self.CMD_CAT_APP_MGMT, "Application Management is currently disabled")
147+
message_to_print = "{} is not available while {} commands are disabled".format(COMMAND_NAME,
148+
self.CMD_CAT_APP_MGMT)
149+
self.disable_category(self.CMD_CAT_APP_MGMT, message_to_print)
147150
self.poutput("The Application Management commands have been disabled")
148151

149152
@cmd2.with_category("Command Management")

tests/test_cmd2.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,10 @@ def test_pyscript_requires_an_argument(base_app, capsys):
292292
assert "the following arguments are required: script_path" in err
293293

294294

295-
def test_base_error(base_app):
296-
out = run_cmd(base_app, 'meow')
297-
assert "is not a recognized command" in out[0]
295+
def test_base_error(base_app, capsys):
296+
run_cmd(base_app, 'meow')
297+
out, err = capsys.readouterr()
298+
assert "is not a recognized command" in err
298299

299300

300301
def test_base_load(base_app, request):
@@ -2199,23 +2200,27 @@ def disable_commands_app():
21992200
return app
22002201

22012202

2202-
def test_disable_and_enable_category(disable_commands_app):
2203+
def test_disable_and_enable_category(disable_commands_app, capsys):
22032204
# Disable the category
22042205
message_to_print = 'These commands are currently disabled'
22052206
disable_commands_app.disable_category(disable_commands_app.category_name, message_to_print)
22062207

22072208
# Make sure all the commands and help on those commands displays the message
2208-
out = run_cmd(disable_commands_app, 'has_help_func')
2209-
assert out == [message_to_print]
2209+
run_cmd(disable_commands_app, 'has_help_func')
2210+
out, err = capsys.readouterr()
2211+
assert err.startswith(message_to_print)
22102212

2211-
out = run_cmd(disable_commands_app, 'help has_help_func')
2212-
assert out == [message_to_print]
2213+
run_cmd(disable_commands_app, 'help has_help_func')
2214+
out, err = capsys.readouterr()
2215+
assert err.startswith(message_to_print)
22132216

2214-
out = run_cmd(disable_commands_app, 'has_no_help_func')
2215-
assert out == [message_to_print]
2217+
run_cmd(disable_commands_app, 'has_no_help_func')
2218+
out, err = capsys.readouterr()
2219+
assert err.startswith(message_to_print)
22162220

2217-
out = run_cmd(disable_commands_app, 'help has_no_help_func')
2218-
assert out == [message_to_print]
2221+
run_cmd(disable_commands_app, 'help has_no_help_func')
2222+
out, err = capsys.readouterr()
2223+
assert err.startswith(message_to_print)
22192224

22202225
visible_commands = disable_commands_app.get_visible_commands()
22212226
assert 'has_help_func' not in visible_commands
@@ -2275,3 +2280,11 @@ def test_disabled_command_not_in_history(disable_commands_app):
22752280
saved_len = len(disable_commands_app.history)
22762281
run_cmd(disable_commands_app, 'has_help_func')
22772282
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)