summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2019-03-14 18:12:15 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2019-03-14 18:12:15 -0400
commitd2147757107c22de4b425d750137dd703cdcc1b5 (patch)
tree842970aad887263b76c414acf00eb0f9005ce23a
parent177297ae557383215f3dae698ff43e1cf9daecca (diff)
downloadcmd2-git-d2147757107c22de4b425d750137dd703cdcc1b5.tar.gz
Help command now writes to stderr when there is no help information
-rw-r--r--cmd2/cmd2.py16
-rw-r--r--tests/test_cmd2.py13
2 files changed, 19 insertions, 10 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 6b085a98..869b7b04 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -2057,8 +2057,8 @@ class Cmd(cmd.Cmd):
return self.do_shell(statement.command_and_args)
else:
- self.perror('*** {} is not a recognized command, alias, or macro'.format(statement.command),
- err_color=Fore.RESET, traceback_war=False)
+ self.perror('{} is not a recognized command, alias, or macro'.format(statement.command),
+ traceback_war=False, err_color=Fore.RESET)
def pseudo_raw_input(self, prompt: str) -> str:
"""Began life as a copy of cmd's cmdloop; like raw_input but
@@ -2586,12 +2586,20 @@ class Cmd(cmd.Cmd):
else:
# Getting help for a specific command
func = self.cmd_func(args.command)
+ help_func = getattr(self, HELP_FUNC_PREFIX + args.command, None)
+
+ # If the command function uses argparse, then use argparse's help
if func and hasattr(func, 'argparser'):
completer = AutoCompleter(getattr(func, 'argparser'), self)
tokens = [args.command] + args.subcommand
self.poutput(completer.format_help(tokens))
+
+ # If there is no help information then print an error
+ elif help_func is None and (func is None or not func.__doc__):
+ self.perror("No help on {}".format(args.command), traceback_war=False, err_color=Fore.RESET)
+
+ # Otherwise delegate to cmd base class do_help()
else:
- # No special behavior needed, delegate to cmd base class do_help()
super().do_help(args.command)
def _help_menu(self, verbose: bool = False) -> None:
@@ -3719,7 +3727,7 @@ class Cmd(cmd.Cmd):
:param message_to_print: the message reporting that the command is disabled
:param kwargs: not used
"""
- self.perror(message_to_print, err_color=Fore.RESET, traceback_war=False)
+ self.perror(message_to_print, traceback_war=False, err_color=Fore.RESET)
def cmdloop(self, intro: Optional[str] = None) -> None:
"""This is an outer wrapper around _cmdloop() which deals with extra features provided by cmd2.
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index 44ca89e0..6733be19 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -952,10 +952,10 @@ undoc
""")
assert out == expected
-def test_help_undocumented(help_app):
- out = run_cmd(help_app, 'help undoc')
- expected = normalize('*** No help on undoc')
- assert out == expected
+def test_help_undocumented(help_app, capsys):
+ run_cmd(help_app, 'help undoc')
+ out, err = capsys.readouterr()
+ assert err.startswith("No help on undoc")
def test_help_overridden_method(help_app):
out = run_cmd(help_app, 'help edit')
@@ -1787,8 +1787,9 @@ def test_macro_create_with_escaped_args(base_app, capsys):
assert out == normalize("Macro 'fake' created")
# Run the macro
- out = run_cmd(base_app, 'fake')
- assert 'No help on {1}' in out[0]
+ run_cmd(base_app, 'fake')
+ out, err = capsys.readouterr()
+ assert err.startswith('No help on {1}')
def test_macro_usage_with_missing_args(base_app, capsys):
# Create the macro