Skip to content

Commit eb050d7

Browse files
committed
Merge branch 'master' into attributes
2 parents d2ebee4 + 177297a commit eb050d7

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
@@ -135,6 +135,9 @@ def __subclasshook__(cls, C):
135135
ALPHABETICAL_SORT_KEY = utils.norm_fold
136136
NATURAL_SORT_KEY = utils.natural_keys
137137

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

139142
def categorize(func: Union[Callable, Iterable], category: str) -> None:
140143
"""Categorize a function.
@@ -2063,7 +2066,8 @@ def default(self, statement: Statement) -> Optional[bool]:
20632066

20642067
return self.do_shell(statement.command_and_args)
20652068
else:
2066-
self.poutput('*** {} is not a recognized command, alias, or macro\n'.format(statement.command))
2069+
self.perror('*** {} is not a recognized command, alias, or macro'.format(statement.command),
2070+
err_color=Fore.RESET, traceback_war=False)
20672071

20682072
def pseudo_raw_input(self, prompt: str) -> str:
20692073
"""Began life as a copy of cmd's cmdloop; like raw_input but
@@ -3678,6 +3682,10 @@ def disable_command(self, command: str, message_to_print: str) -> None:
36783682
Disable a command and overwrite its functions
36793683
:param command: the command being disabled
36803684
:param message_to_print: what to print when this command is run or help is called on it while disabled
3685+
3686+
The variable COMMAND_NAME can be used as a placeholder for the name of the
3687+
command being disabled.
3688+
ex: message_to_print = "{} is currently disabled".format(COMMAND_NAME)
36813689
"""
36823690
import functools
36833691

@@ -3697,7 +3705,8 @@ def disable_command(self, command: str, message_to_print: str) -> None:
36973705
help_function=getattr(self, help_func_name, None))
36983706

36993707
# Overwrite the command and help functions to print the message
3700-
new_func = functools.partial(self._report_disabled_command_usage, message_to_print=message_to_print)
3708+
new_func = functools.partial(self._report_disabled_command_usage,
3709+
message_to_print=message_to_print.replace(COMMAND_NAME, command))
37013710
setattr(self, self.cmd_func_name(command), new_func)
37023711
setattr(self, help_func_name, new_func)
37033712

@@ -3707,6 +3716,10 @@ def disable_category(self, category: str, message_to_print: str) -> None:
37073716
:param category: the category to disable
37083717
:param message_to_print: what to print when anything in this category is run or help is called on it
37093718
while disabled
3719+
3720+
The variable COMMAND_NAME can be used as a placeholder for the name of the
3721+
command being disabled.
3722+
ex: message_to_print = "{} is currently disabled".format(COMMAND_NAME)
37103723
"""
37113724
all_commands = self.get_all_commands()
37123725

@@ -3723,7 +3736,7 @@ def _report_disabled_command_usage(self, *args, message_to_print: str, **kwargs)
37233736
:param message_to_print: the message reporting that the command is disabled
37243737
:param kwargs: not used
37253738
"""
3726-
self.poutput(message_to_print)
3739+
self.perror(message_to_print, err_color=Fore.RESET, traceback_war=False)
37273740

37283741
def cmdloop(self, intro: Optional[str] = None) -> None:
37293742
"""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):
@@ -2198,23 +2199,27 @@ def disable_commands_app():
21982199
return app
21992200

22002201

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

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

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

2213-
out = run_cmd(disable_commands_app, 'has_no_help_func')
2214-
assert out == [message_to_print]
2216+
run_cmd(disable_commands_app, 'has_no_help_func')
2217+
out, err = capsys.readouterr()
2218+
assert err.startswith(message_to_print)
22152219

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

22192224
visible_commands = disable_commands_app.get_visible_commands()
22202225
assert 'has_help_func' not in visible_commands
@@ -2274,3 +2279,11 @@ def test_disabled_command_not_in_history(disable_commands_app):
22742279
saved_len = len(disable_commands_app.history)
22752280
run_cmd(disable_commands_app, 'has_help_func')
22762281
assert saved_len == len(disable_commands_app.history)
2282+
2283+
def test_disabled_message_command_name(disable_commands_app, capsys):
2284+
message_to_print = '{} is currently disabled'.format(cmd2.cmd2.COMMAND_NAME)
2285+
disable_commands_app.disable_command('has_help_func', message_to_print)
2286+
2287+
run_cmd(disable_commands_app, 'has_help_func')
2288+
out, err = capsys.readouterr()
2289+
assert err.startswith('has_help_func is currently disabled')

0 commit comments

Comments
 (0)