Skip to content

Commit

Permalink
Placed default_suggestion_message on its own line. (#1326)
Browse files Browse the repository at this point in the history
  • Loading branch information
kmvanbrunt authored Sep 26, 2024
1 parent 522ce2e commit 3d7a1e7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
1 change: 0 additions & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ pytest-mock = "*"
sphinx = "*"
sphinx-autobuild = "*"
sphinx-rtd-theme = "*"
tableformatter="*"
twine = ">=1.11"

[pipenv]
Expand Down
14 changes: 8 additions & 6 deletions cmd2/cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ def __init__(
instantiate and register all commands. If False, CommandSets
must be manually installed with `register_command_set`.
:param allow_clipboard: If False, cmd2 will disable clipboard interactions
:param suggest_similar_command: If ``True``, ``cmd2`` will attempt to suggest the most
similar command when the user types a command that does
not exist. Default: ``False``.
"""
# Check if py or ipy need to be disabled in this instance
if not include_py:
Expand Down Expand Up @@ -404,7 +407,7 @@ def __init__(
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"
self.default_error = "{} is not a recognized command, alias, or macro."

# If non-empty, this string will be displayed if a broken pipe error occurs
self.broken_pipe_warning = ''
Expand Down Expand Up @@ -3092,11 +3095,10 @@ def default(self, statement: Statement) -> Optional[bool]: # type: ignore[overr
return self.do_shell(statement.command_and_args)
else:
err_msg = self.default_error.format(statement.command)
if self.suggest_similar_command:
suggested_command = self._suggest_similar_command(statement.command)
if suggested_command:
err_msg = err_msg + ' ' + self.default_suggestion_message.format(suggested_command)
# Set apply_style to False so default_error's style is not overridden
if self.suggest_similar_command and (suggested_command := self._suggest_similar_command(statement.command)):
err_msg += f"\n{self.default_suggestion_message.format(suggested_command)}"

# Set apply_style to False so styles for default_error and default_suggestion_message are not overridden
self.perror(err_msg, apply_style=False)
return None

Expand Down
7 changes: 3 additions & 4 deletions docs/api/cmd.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ cmd2.Cmd
.. attribute:: default_error

The error message displayed when a non-existent command is run.
Default: ``{} is not a recognized command, alias, or macro``
Default: ``{} is not a recognized command, alias, or macro.``

.. attribute:: help_error

Expand Down Expand Up @@ -75,6 +75,5 @@ cmd2.Cmd

.. attribute:: suggest_similar_command

If ``True``, ``cmd2`` will suggest the most similar command when the user
types a command that does not exist.
Default: ``False``.
If ``True``, ``cmd2`` will attempt to suggest the most similar command
when the user types a command that does not exist. Default: ``False``.
5 changes: 4 additions & 1 deletion tests/test_cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
plugin,
utils,
)
from cmd2.rl_utils import (
readline, # This ensures gnureadline is used in macOS tests
)

from .conftest import (
HELP_HISTORY,
Expand Down Expand Up @@ -333,7 +336,7 @@ def test_base_error_suggest_command(base_app):
old_suggest_similar_command = base_app.suggest_similar_command
base_app.suggest_similar_command = True
out, err = run_cmd(base_app, 'historic')
assert "history" in err[0]
assert "history" in err[1]
finally:
base_app.suggest_similar_command = old_suggest_similar_command

Expand Down

0 comments on commit 3d7a1e7

Please sign in to comment.