Skip to content

[4.x] Support abbreviated command names and suggest alternatives - #35

Open
Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-find-command
Open

Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-find-command

Conversation

@Hackwar

@Hackwar Hackwar commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Summary of Changes

Application only looked up exact command names: ne for the single command neg failed with The command "ne" does not exist., and nothing suggested the right name, although findNamespace() already accepts abbreviated namespaces and the class otherwise mirrors Symfony's Application, which does both.

This adds Application::findCommand(string $name): AbstractCommand, a port of Symfony's Application::find():

  • an exact name or alias is returned as before (including hidden commands);
  • otherwise every part of the name may be abbreviated as long as the result is unique (c:c → cache:clear, li → list), with a case-insensitive fallback; aliases of a command that already matches are not counted twice;
  • an ambiguous abbreviation throws CommandNotFoundException "The command "x" is ambiguous." listing the matches with their descriptions, an unknown name keeps the existing message The command "x" does not exist. and appends "Did you mean this?" alternatives (Levenshtein, as in Symfony); both pass the names to CommandNotFoundException::getAlternatives();
  • an unknown namespace in the name throws NamespaceNotFoundException from findNamespace();
  • hidden commands are neither expanded from an abbreviation nor suggested.

doExecute() now resolves the command from the input with findCommand(). getCommand() and hasCommand() keep their exact lookup, so code calling them is unaffected. The --help handling still goes through getCommand() once the name is resolved, so li --help shows the help of list.

Related to #32 (which removes the extra error block a few lines further down in doExecute(); the two merge cleanly) and #33 (adds a finally to the same try block).

Testing Instructions

New tests in tests/ApplicationTest.php (testFindCommand*, testFindAmbiguousCommand, testFindUnknownCommand*, testTheApplicationIsExecutedWithAnAbbreviatedCommand) cover exact names, abbreviations per namespace part, the case-insensitive fallback, alias de-duplication, the command loader, ambiguity, alternatives, unknown namespaces and hidden commands. On 4.x-dev they fail (the method does not exist, and running li reports an unknown command).

Manually: register cache:clear and run bin/console c:c, or run bin/console lisst and see "Did you mean this? list".

Documentation Changes Required

docs/overview.md: new "Finding commands" section under "Console Application".

🤖 Generated with Claude Code

The application only accepted exact command names, although it mirrors
Symfony's Application, which accepts unique abbreviations (c:c for
cache:clear) and lists similar names for an unknown command. Add
Application::findCommand(), ported from Symfony's find(), and use it to
resolve the command given on the command line. getCommand() and
hasCommand() keep their exact lookup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Hackwar

Hackwar commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

Approved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant