1
0
Fork 0

Merge pull request #12283 from Seldaek/fix_cmd_guessing

Fix command name parsing to take into account global input options
pull/12284/head
Jordi Boggiano 2025-01-20 14:28:13 +01:00 committed by GitHub
commit 149dc40dda
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 21 additions and 2 deletions

View File

@ -21,6 +21,7 @@ use LogicException;
use RuntimeException;
use Symfony\Component\Console\Application as BaseApplication;
use Symfony\Component\Console\Exception\CommandNotFoundException;
use Symfony\Component\Console\Exception\ExceptionInterface;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputDefinition;
@ -173,7 +174,7 @@ class Application extends BaseApplication
// determine command name to be executed without including plugin commands
$commandName = '';
if ($name = $this->getCommandName($input)) {
if ($name = $this->getCommandNameBeforeBinding($input)) {
try {
$commandName = $this->find($name)->getName();
} 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
$isProxyCommand = false;
if ($name = $this->getCommandName($input)) {
if ($name = $this->getCommandNameBeforeBinding($input)) {
try {
$command = $this->find($name);
$commandName = $command->getName();
@ -631,6 +632,24 @@ class Application extends BaseApplication
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
{
$branchAliasString = '';