Skip to content

[Console] Commands with an alias should not be recognized as ambiguous #25407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ public function findNamespace($namespace)
public function find($name)
{
$this->init();

$aliases = array();
$allCommands = array_keys($this->commands);
$expr = preg_replace_callback('{([^:]+|)}', function ($matches) { return preg_quote($matches[1]).'[^:]*'; }, $name);
$commands = preg_grep('{^'.$expr.'}', $allCommands);
Expand Down Expand Up @@ -526,15 +526,16 @@ public function find($name)
// filter out aliases for commands which are already on the list
if (count($commands) > 1) {
$commandList = $this->commands;
$commands = array_filter($commands, function ($nameOrAlias) use ($commandList, $commands) {
$commands = array_filter($commands, function ($nameOrAlias) use ($commandList, $commands, &$aliases) {
$commandName = $commandList[$nameOrAlias]->getName();
$aliases[$nameOrAlias] = $commandName;

return $commandName === $nameOrAlias || !in_array($commandName, $commands);
});
}

$exact = in_array($name, $commands, true);
if (count($commands) > 1 && !$exact) {
$exact = in_array($name, $commands, true) || isset($aliases[$name]);
if (!$exact && count($commands) > 1) {
$suggestions = $this->getAbbreviationSuggestions(array_values($commands));

throw new \InvalidArgumentException(sprintf('Command "%s" is ambiguous (%s).', $name, $suggestions));
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public static function setUpBeforeClass()
require_once self::$fixturesPath.'/BarBucCommand.php';
require_once self::$fixturesPath.'/FooSubnamespaced1Command.php';
require_once self::$fixturesPath.'/FooSubnamespaced2Command.php';
require_once self::$fixturesPath.'/TestTiti.php';
require_once self::$fixturesPath.'/TestToto.php';
}

protected function normalizeLineBreaks($text)
Expand Down Expand Up @@ -226,6 +228,14 @@ public function testFindAmbiguousNamespace()
$application->findNamespace('f');
}

public function testFindNonAmbiguous()
{
$application = new Application();
$application->add(new \TestTiti());
$application->add(new \TestToto());
$this->assertEquals('test-toto', $application->find('test')->getName());
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage There are no commands defined in the "bar" namespace.
Expand Down
21 changes: 21 additions & 0 deletions src/Symfony/Component/Console/Tests/Fixtures/TestTiti.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class TestTiti extends Command
{
protected function configure()
{
$this
->setName('test-titi')
->setDescription('The test:titi command')
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$output->write('test-titi');
}
}
22 changes: 22 additions & 0 deletions src/Symfony/Component/Console/Tests/Fixtures/TestToto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class TestToto extends Command
{
protected function configure()
{
$this
->setName('test-toto')
->setDescription('The test-toto command')
->setAliases(array('test'))
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$output->write('test-toto');
}
}