Skip to content

Commit a36f1ff

Browse files
committed
Added help_error and default_error
1 parent c88453a commit a36f1ff

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
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
59
* Potentially breaking changes
610
* The following commands now write to stderr instead of stdout when printing an error. This will make catching
711
errors easier in pyscript.

cmd2/cmd2.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,6 @@ class Cmd(cmd.Cmd):
338338
'quiet': "Don't print nonessential feedback",
339339
'timing': 'Report execution times'}
340340

341-
# Override cmd's nohelp
342-
nohelp = "No help on {}"
343-
344341
def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, persistent_history_file: str = '',
345342
persistent_history_length: int = 1000, startup_script: Optional[str] = None, use_ipython: bool = False,
346343
transcript_files: Optional[List[str]] = None) -> None:
@@ -431,6 +428,12 @@ def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, persistent
431428
# Used to keep track of whether a continuation prompt is being displayed
432429
self.at_continuation_prompt = False
433430

431+
# The error that prints when no help information can be found
432+
self.help_error = "No help on {}"
433+
434+
# The error that prints when a non-existent command is run
435+
self.default_error = "{} is not a recognized command, alias, or macro"
436+
434437
# If this string is non-empty, then this warning message will print if a broken pipe error occurs while printing
435438
self.broken_pipe_warning = ''
436439

@@ -2060,7 +2063,8 @@ def default(self, statement: Statement) -> Optional[bool]:
20602063

20612064
return self.do_shell(statement.command_and_args)
20622065
else:
2063-
sys.stderr.write('{} is not a recognized command, alias, or macro\n'.format(statement.command))
2066+
err_msg = self.default_error.format(statement.command)
2067+
self.decolorized_write(sys.stderr, "{}\n".format(err_msg))
20642068

20652069
def pseudo_raw_input(self, prompt: str) -> str:
20662070
"""Began life as a copy of cmd's cmdloop; like raw_input but
@@ -2598,7 +2602,7 @@ def do_help(self, args: argparse.Namespace) -> None:
25982602

25992603
# If there is no help information then print an error
26002604
elif help_func is None and (func is None or not func.__doc__):
2601-
err_msg = Cmd.nohelp.format(args.command)
2605+
err_msg = self.help_error.format(args.command)
26022606
self.decolorized_write(sys.stderr, "{}\n".format(err_msg))
26032607

26042608
# Otherwise delegate to cmd base class do_help()

0 commit comments

Comments
 (0)