diff --git a/news/320.bugfix.md b/news/320.bugfix.md new file mode 100644 index 00000000..8f2615fc --- /dev/null +++ b/news/320.bugfix.md @@ -0,0 +1 @@ +Improved the error message for invalid subcommands in a command namespace. diff --git a/src/cleo/application.py b/src/cleo/application.py index b02e0d00..9bcf9445 100644 --- a/src/cleo/application.py +++ b/src/cleo/application.py @@ -595,6 +595,8 @@ def _get_command_name(self, io: IO) -> str | None: if self.has(candidate): return candidate + return candidates[-1] + return io.input.first_argument def extract_namespace(self, name: str, limit: int | None = None) -> str: diff --git a/tests/test_application.py b/tests/test_application.py index f08286a4..873b28a9 100644 --- a/tests/test_application.py +++ b/tests/test_application.py @@ -230,6 +230,18 @@ def test_set_catch_exceptions(app: Application, environ: dict[str, str]) -> None tester.execute("foo", decorated=False) +def test_invalid_subcommand_uses_full_command_name(app: Application) -> None: + app.auto_exits(False) + app.catch_exceptions(True) + app.add(FooCommand()) + + tester = ApplicationTester(app) + tester.execute("foo baz", decorated=False) + + assert tester.status_code == 1 + assert 'The command "foo baz" does not exist.' in tester.io.fetch_error() + + def test_auto_exit(app: Application) -> None: app.auto_exits(False) assert not app.is_auto_exit_enabled()