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