summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2019-03-16 17:39:38 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2019-03-16 17:39:38 -0400
commita36f1fff6bd058225dd0eaf63f45f3dfa34d338c (patch)
treede6fd37065cc48a8182951977c24114d2c6000c4
parentc88453a10d386fabd209abb0a0465f1af7da3303 (diff)
downloadcmd2-git-a36f1fff6bd058225dd0eaf63f45f3dfa34d338c.tar.gz
Added help_error and default_error
-rw-r--r--CHANGELOG.md4
-rw-r--r--cmd2/cmd2.py14
2 files changed, 13 insertions, 5 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4825f5f5..6d21b33c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,10 @@
* Enhancements
* Added ability to include command name placeholders in the message printed when trying to run a disabled command.
* See docstring for ``disable_command()`` or ``disable_category()`` for more details.
+ * Added instance attributes to customize error messages without having to override methods. Theses messages can
+ also be colored.
+ * `help_error` - the error that prints when no help information can be found
+ * `default_error` - the error that prints when a non-existent command is run
* Potentially breaking changes
* The following commands now write to stderr instead of stdout when printing an error. This will make catching
errors easier in pyscript.
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index e09af4ce..e41d947d 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -338,9 +338,6 @@ class Cmd(cmd.Cmd):
'quiet': "Don't print nonessential feedback",
'timing': 'Report execution times'}
- # Override cmd's nohelp
- nohelp = "No help on {}"
-
def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, persistent_history_file: str = '',
persistent_history_length: int = 1000, startup_script: Optional[str] = None, use_ipython: bool = False,
transcript_files: Optional[List[str]] = None) -> None:
@@ -431,6 +428,12 @@ class Cmd(cmd.Cmd):
# Used to keep track of whether a continuation prompt is being displayed
self.at_continuation_prompt = False
+ # The error that prints when no help information can be found
+ self.help_error = "No help on {}"
+
+ # The error that prints when a non-existent command is run
+ self.default_error = "{} is not a recognized command, alias, or macro"
+
# If this string is non-empty, then this warning message will print if a broken pipe error occurs while printing
self.broken_pipe_warning = ''
@@ -2060,7 +2063,8 @@ class Cmd(cmd.Cmd):
return self.do_shell(statement.command_and_args)
else:
- sys.stderr.write('{} is not a recognized command, alias, or macro\n'.format(statement.command))
+ err_msg = self.default_error.format(statement.command)
+ self.decolorized_write(sys.stderr, "{}\n".format(err_msg))
def pseudo_raw_input(self, prompt: str) -> str:
"""Began life as a copy of cmd's cmdloop; like raw_input but
@@ -2598,7 +2602,7 @@ class Cmd(cmd.Cmd):
# If there is no help information then print an error
elif help_func is None and (func is None or not func.__doc__):
- err_msg = Cmd.nohelp.format(args.command)
+ err_msg = self.help_error.format(args.command)
self.decolorized_write(sys.stderr, "{}\n".format(err_msg))
# Otherwise delegate to cmd base class do_help()