From 8e52a0eef11709b75f1c6d6f1e3f79b98b904250 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Tue, 24 Sep 2024 00:43:07 -0400 Subject: [PATCH] Placed default_suggestion_message on its own line. --- cmd2/cmd2.py | 14 ++++++++------ docs/api/cmd.rst | 7 +++---- tests/test_cmd2.py | 2 +- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 28661b96..79fd2bf2 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -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: @@ -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 = '' @@ -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 diff --git a/docs/api/cmd.rst b/docs/api/cmd.rst index 4b8cdca6..4fbb8ccd 100644 --- a/docs/api/cmd.rst +++ b/docs/api/cmd.rst @@ -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 @@ -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``. diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 0c9c04e4..7d72be3d 100755 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -333,7 +333,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