Skip to content

Commit adcc870

Browse files
committed
Merge branch 'master' into attributes
2 parents 6b388ed + 8e92f9d commit adcc870

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22
* Enhancements
33
* Added ability to include command name placeholders in the message printed when trying to run a disabled command.
44
* See docstring for ``disable_command()`` or ``disable_category()`` for more details.
5+
* Added instance attributes to customize error messages without having to override methods. Theses messages can
6+
also be colored.
7+
* `help_error` - the error that prints when no help information can be found
8+
* `default_error` - the error that prints when a non-existent command is run
9+
* Potentially breaking changes
10+
* The following commands now write to stderr instead of stdout when printing an error. This will make catching
11+
errors easier in pyscript.
12+
* ``do_help()`` - when no help information can be found
13+
* ``default()`` - in all cases since this is called when an invalid command name is run
14+
* ``_report_disabled_command_usage()`` - in all cases since this is called when a disabled command is run
15+
* Removed *** from beginning of error messages printed by `do_help()` and `default()`.
516

617
## 0.9.11 (March 13, 2019)
718
* Bug Fixes

cmd2/cmd2.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,12 @@ def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, persistent
419419
# Used to keep track of whether a continuation prompt is being displayed
420420
self.at_continuation_prompt = False
421421

422+
# The error that prints when no help information can be found
423+
self.help_error = "No help on {}"
424+
425+
# The error that prints when a non-existent command is run
426+
self.default_error = "{} is not a recognized command, alias, or macro"
427+
422428
# If this string is non-empty, then this warning message will print if a broken pipe error occurs while printing
423429
self.broken_pipe_warning = ''
424430

@@ -2065,8 +2071,8 @@ def default(self, statement: Statement) -> Optional[bool]:
20652071

20662072
return self.do_shell(statement.command_and_args)
20672073
else:
2068-
self.perror('*** {} is not a recognized command, alias, or macro'.format(statement.command),
2069-
err_color=Fore.RESET, traceback_war=False)
2074+
err_msg = self.default_error.format(statement.command)
2075+
self.decolorized_write(sys.stderr, "{}\n".format(err_msg))
20702076

20712077
def pseudo_raw_input(self, prompt: str) -> str:
20722078
"""Began life as a copy of cmd's cmdloop; like raw_input but
@@ -2595,12 +2601,21 @@ def do_help(self, args: argparse.Namespace) -> None:
25952601
else:
25962602
# Getting help for a specific command
25972603
func = self.cmd_func(args.command)
2604+
help_func = getattr(self, HELP_FUNC_PREFIX + args.command, None)
2605+
2606+
# If the command function uses argparse, then use argparse's help
25982607
if func and hasattr(func, 'argparser'):
25992608
completer = AutoCompleter(getattr(func, 'argparser'), self)
26002609
tokens = [args.command] + args.subcommand
26012610
self.poutput(completer.format_help(tokens))
2611+
2612+
# If there is no help information then print an error
2613+
elif help_func is None and (func is None or not func.__doc__):
2614+
err_msg = self.help_error.format(args.command)
2615+
self.decolorized_write(sys.stderr, "{}\n".format(err_msg))
2616+
2617+
# Otherwise delegate to cmd base class do_help()
26022618
else:
2603-
# No special behavior needed, delegate to cmd base class do_help()
26042619
super().do_help(args.command)
26052620

26062621
def _help_menu(self, verbose: bool = False) -> None:
@@ -3735,7 +3750,7 @@ def _report_disabled_command_usage(self, *args, message_to_print: str, **kwargs)
37353750
:param message_to_print: the message reporting that the command is disabled
37363751
:param kwargs: not used
37373752
"""
3738-
self.perror(message_to_print, err_color=Fore.RESET, traceback_war=False)
3753+
self.decolorized_write(sys.stderr, "{}\n".format(message_to_print))
37393754

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

tests/test_cmd2.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -952,10 +952,10 @@ def test_custom_help_menu(help_app):
952952
""")
953953
assert out == expected
954954

955-
def test_help_undocumented(help_app):
956-
out = run_cmd(help_app, 'help undoc')
957-
expected = normalize('*** No help on undoc')
958-
assert out == expected
955+
def test_help_undocumented(help_app, capsys):
956+
run_cmd(help_app, 'help undoc')
957+
out, err = capsys.readouterr()
958+
assert err.startswith("No help on undoc")
959959

960960
def test_help_overridden_method(help_app):
961961
out = run_cmd(help_app, 'help edit')
@@ -1786,8 +1786,9 @@ def test_macro_create_with_escaped_args(base_app, capsys):
17861786
assert out == normalize("Macro 'fake' created")
17871787

17881788
# Run the macro
1789-
out = run_cmd(base_app, 'fake')
1790-
assert 'No help on {1}' in out[0]
1789+
run_cmd(base_app, 'fake')
1790+
out, err = capsys.readouterr()
1791+
assert err.startswith('No help on {1}')
17911792

17921793
def test_macro_usage_with_missing_args(base_app, capsys):
17931794
# Create the macro

0 commit comments

Comments
 (0)