1
0
Fork 0

Fix command name parsing to take into account global input options

Fixes #12259
Closes #12275
pull/12283/head
Jordi Boggiano 2025-01-20 11:34:10 +01:00
parent f5e7a8390d
commit b1b8d49d36
No known key found for this signature in database
1 changed files with 21 additions and 2 deletions

View File

@ -21,6 +21,7 @@ use LogicException;
use RuntimeException; use RuntimeException;
use Symfony\Component\Console\Application as BaseApplication; use Symfony\Component\Console\Application as BaseApplication;
use Symfony\Component\Console\Exception\CommandNotFoundException; use Symfony\Component\Console\Exception\CommandNotFoundException;
use Symfony\Component\Console\Exception\ExceptionInterface;
use Symfony\Component\Console\Helper\HelperSet; use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\QuestionHelper; use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputDefinition; use Symfony\Component\Console\Input\InputDefinition;
@ -173,7 +174,7 @@ class Application extends BaseApplication
// determine command name to be executed without including plugin commands // determine command name to be executed without including plugin commands
$commandName = ''; $commandName = '';
if ($name = $this->getCommandName($input)) { if ($name = $this->getCommandNameBeforeBinding($input)) {
try { try {
$commandName = $this->find($name)->getName(); $commandName = $this->find($name)->getName();
} catch (CommandNotFoundException $e) { } catch (CommandNotFoundException $e) {
@ -311,7 +312,7 @@ class Application extends BaseApplication
// determine command name to be executed incl plugin commands, and check if it's a proxy command // determine command name to be executed incl plugin commands, and check if it's a proxy command
$isProxyCommand = false; $isProxyCommand = false;
if ($name = $this->getCommandName($input)) { if ($name = $this->getCommandNameBeforeBinding($input)) {
try { try {
$command = $this->find($name); $command = $this->find($name);
$commandName = $command->getName(); $commandName = $command->getName();
@ -631,6 +632,24 @@ class Application extends BaseApplication
return $commands; return $commands;
} }
/**
* This ensures we can find the correct command name even if a global input option is present before it
*
* e.g. "composer -d foo bar" should detect bar as the command name, and not foo
*/
private function getCommandNameBeforeBinding(InputInterface $input): ?string
{
$input = clone $input;
try {
// Makes ArgvInput::getFirstArgument() able to distinguish an option from an argument.
$input->bind($this->getDefinition());
} catch (ExceptionInterface $e) {
// Errors must be ignored, full binding/validation happens later when the command is known.
}
return $input->getFirstArgument();
}
public function getLongVersion(): string public function getLongVersion(): string
{ {
$branchAliasString = ''; $branchAliasString = '';