Skip to content

Commit 4bd9c79

Browse files
author
Robin Chalas
committed
bug #25407 [Console] Commands with an alias should not be recognized as ambiguous (Simperfit)
This PR was merged into the 2.7 branch. Discussion ---------- [Console] Commands with an alias should not be recognized as ambiguous | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no | Deprecations? | no <!-- don't forget to update UPGRADE-*.md files --> | Tests pass? | no | Fixed tickets | no <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | When having two commands with the same starting name and an alias equal to the starting name, we should not have an ambiguous error because we are setting the alias. Commits ------- 863f632 [Console] Commands with an alias should not be recognized as ambiguous
2 parents f6fc785 + 863f632 commit 4bd9c79

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ public function findNamespace($namespace)
498498
public function find($name)
499499
{
500500
$this->init();
501-
501+
$aliases = array();
502502
$allCommands = array_keys($this->commands);
503503
$expr = preg_replace_callback('{([^:]+|)}', function ($matches) { return preg_quote($matches[1]).'[^:]*'; }, $name);
504504
$commands = preg_grep('{^'.$expr.'}', $allCommands);
@@ -526,15 +526,16 @@ public function find($name)
526526
// filter out aliases for commands which are already on the list
527527
if (count($commands) > 1) {
528528
$commandList = $this->commands;
529-
$commands = array_filter($commands, function ($nameOrAlias) use ($commandList, $commands) {
529+
$commands = array_filter($commands, function ($nameOrAlias) use ($commandList, $commands, &$aliases) {
530530
$commandName = $commandList[$nameOrAlias]->getName();
531+
$aliases[$nameOrAlias] = $commandName;
531532

532533
return $commandName === $nameOrAlias || !in_array($commandName, $commands);
533534
});
534535
}
535536

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

540541
throw new \InvalidArgumentException(sprintf('Command "%s" is ambiguous (%s).', $name, $suggestions));

src/Symfony/Component/Console/Tests/ApplicationTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public static function setUpBeforeClass()
4949
require_once self::$fixturesPath.'/BarBucCommand.php';
5050
require_once self::$fixturesPath.'/FooSubnamespaced1Command.php';
5151
require_once self::$fixturesPath.'/FooSubnamespaced2Command.php';
52+
require_once self::$fixturesPath.'/TestTiti.php';
53+
require_once self::$fixturesPath.'/TestToto.php';
5254
}
5355

5456
protected function normalizeLineBreaks($text)
@@ -226,6 +228,14 @@ public function testFindAmbiguousNamespace()
226228
$application->findNamespace('f');
227229
}
228230

231+
public function testFindNonAmbiguous()
232+
{
233+
$application = new Application();
234+
$application->add(new \TestTiti());
235+
$application->add(new \TestToto());
236+
$this->assertEquals('test-toto', $application->find('test')->getName());
237+
}
238+
229239
/**
230240
* @expectedException \InvalidArgumentException
231241
* @expectedExceptionMessage There are no commands defined in the "bar" namespace.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Command\Command;
4+
use Symfony\Component\Console\Input\InputInterface;
5+
use Symfony\Component\Console\Output\OutputInterface;
6+
7+
class TestTiti extends Command
8+
{
9+
protected function configure()
10+
{
11+
$this
12+
->setName('test-titi')
13+
->setDescription('The test:titi command')
14+
;
15+
}
16+
17+
protected function execute(InputInterface $input, OutputInterface $output)
18+
{
19+
$output->write('test-titi');
20+
}
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Command\Command;
4+
use Symfony\Component\Console\Input\InputInterface;
5+
use Symfony\Component\Console\Output\OutputInterface;
6+
7+
class TestToto extends Command
8+
{
9+
protected function configure()
10+
{
11+
$this
12+
->setName('test-toto')
13+
->setDescription('The test-toto command')
14+
->setAliases(array('test'))
15+
;
16+
}
17+
18+
protected function execute(InputInterface $input, OutputInterface $output)
19+
{
20+
$output->write('test-toto');
21+
}
22+
}

0 commit comments

Comments
 (0)