8000 Improve UX on not found namespace/command · symfony/symfony@76ecf4d · GitHub
[go: up one dir, main page]

Skip to content

Commit 76ecf4d

Browse files
committed
Improve UX on not found namespace/command
1 parent 51bc35c commit 76ecf4d

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ public function findNamespace($namespace)
503503

504504
$exact = in_array($namespace, $namespaces, true);
505505
if (count($namespaces) > 1 && !$exact) {
506-
throw new CommandNotFoundException(sprintf('The namespace "%s" is ambiguous (%s).', $namespace, $this->getAbbreviationSuggestions(array_values($namespaces))), array_values($namespaces));
506+
throw new CommandNotFoundException(sprintf("The namespace \"%s\" is ambiguous.\nDid you mean one of these?\n%s", $namespace, $this->getAbbreviationSuggestions(array_values($namespaces))), array_values($namespaces));
507507
}
508508

509509
return $exact ? $namespace : reset($namespaces);
@@ -559,9 +559,20 @@ public function find($name)
559559

560560
$exact = in_array($name, $commands, true);
561561
if (count($commands) > 1 && !$exact) {
562-
$suggestions = $this->getAbbreviationSuggestions(array_values($commands));
562+
$usableWidth = ($this->getTerminalWidth() ?: 60) - 10;
563+
$abbrevs = array_values($commands);
564+
$maxLen = 0;
565+
foreach ($abbrevs as $abbrev) {
566+
$maxLen = max(strlen($abbrev), $maxLen);
567+
}
568+
$abbrevs = array_map(function ($cmd) use ($commandList, $usableWidth, $maxLen) {
569+
$abbrev = str_pad($cmd, $maxLen, ' ').' '.$commandList[$cmd]->getDescription();
570+
571+
return strlen($abbrev) > $usableWidth ? substr($abbrev, 0, $usableWidth - 3).'...' : $abbrev;
572+
}, array_values($commands));
573+
$suggestions = $this->getAbbreviationSuggestions($abbrevs);
563574

564-
throw new CommandNotFoundException(sprintf('Command "%s" is ambiguous (%s).', $name, $suggestions), array_values($commands));
575+
throw new CommandNotFoundException(sprintf("Command \"%s\" is ambiguous.\nDid you mean one of these?\n%s", $name, $suggestions), array_values($commands));
565576
}
566577

567578
return $this->get($exact ? $name : reset($commands));
@@ -944,7 +955,7 @@ protected function getDefaultHelperSet()
944955
*/
945956
private function getAbbreviationSuggestions($abbrevs)
946957
{
947-
return sprintf('%s, %s%s', $abbrevs[0], $abbrevs[1], count($abbrevs) > 2 ? sprintf(' and %d more', count($abbrevs) - 2) : '');
958+
return ' '.implode("\n ", $abbrevs);
948959
}
949960

950961
/**

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,15 @@ public function testFindNamespaceWithSubnamespaces()
211211
$this->assertEquals('foo', $application->findNamespace('foo'), '->findNamespace() returns commands even if the commands are only contained in subnamespaces');
212212
}
213213

214-
/**
215-
* @expectedException \Symfony\Component\Console\Exception\CommandNotFoundException
216-
* @expectedExceptionMessage The namespace "f" is ambiguous (foo, foo1).
217-
*/
218214
public function testFindAmbiguousNamespace()
219215
{
220216
$application = new Application();
221217
$application->add(new \BarBucCommand());
222218
$application->add(new \FooCommand());
223219
$application->add(new \Foo2Command());
220+
221+
$expectedMsg = "The namespace \"f\" is ambiguous.\nDid you mean one of these?\n foo\n foo1";
222+
$this->setExpectedException('Symfony\Component\Console\Exception\CommandNotFoundException', $expectedMsg);
224223
$application->findNamespace('f');
225224
}
226225

@@ -279,8 +278,20 @@ public function provideAmbiguousAbbreviations()
279278
{
280279
return array(
281280
array('f', 'Command "f" is not defined.'),
282-
array('a', 'Command "a" is ambiguous (afoobar, afoobar1 and 1 more).'),
283-
array('foo:b', 'Command "foo:b" is ambiguous (foo:bar, foo:bar1 and 1 more).'),
281+
array(
282+
'a',
283+
"Command \"a\" is ambiguous.\nDid you mean one of these?\n".
284+
" afoobar The foo:bar command\n".
285+
" afoobar1 The foo:bar1 command\n".
286+
" afoobar2 The foo1:bar command",
287+
),
288+
array(
289+
'foo:b',
290+
"Command \"foo:b\" is ambiguous.\nDid you mean one of these?\n".
291+
" foo:bar The foo:bar command\n".
292+
" foo:bar1 The foo:bar1 command\n".
293+
" foo1:bar The foo1:bar command",
294+
),
284295
);
285296
}
286297

0 commit comments

Comments
 (0)
0