From 5a9e2aa9c03b17df0a3c265dc0d03a7f8d9cb26d Mon Sep 17 00:00:00 2001 From: AleksZyro <178569539+AleksZyro@users.noreply.github.com> Date: Thu, 6 Aug 2026 00:54:24 +0200 Subject: [PATCH] Improve invalid subcommand errors --- news/320.bugfix.md | 1 + src/cleo/application.py | 2 ++ tests/test_application.py | 12 ++++++++++++ 3 files changed, 15 insertions(+) create mode 100644 news/320.bugfix.md 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()