-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Console] Add option to automatically run suggested command if there is only 1 alternative #25732
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
Changes from 5 commits
5bdcdda
d93ca23
f42a2bb
e4deda7
1b9f9eb
f8a816d
bf46ffe
d7dc6c7
4de52d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
|
||
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface; | ||
use Symfony\Component\Console\Exception\ExceptionInterface; | ||
use Symfony\Component\Console\Exception\NamespaceNotFoundException; | ||
use Symfony\Component\Console\Formatter\OutputFormatter; | ||
use Symfony\Component\Console\Helper\DebugFormatterHelper; | ||
use Symfony\Component\Console\Helper\Helper; | ||
|
@@ -39,6 +40,7 @@ | |
use Symfony\Component\Console\Event\ConsoleTerminateEvent; | ||
use Symfony\Component\Console\Exception\CommandNotFoundException; | ||
use Symfony\Component\Console\Exception\LogicException; | ||
use Symfony\Component\Console\Question\ConfirmationQuestion; | ||
use Symfony\Component\Debug\ErrorHandler; | ||
use Symfony\Component\Debug\Exception\FatalThrowableError; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
|
@@ -223,18 +225,36 @@ public function doRun(InputInterface $input, OutputInterface $output) | |
// the command name MUST be the first element of the input | ||
$command = $this->find($name); | ||
} catch (\Throwable $e) { | ||
if (null !== $this->dispatcher) { | ||
$event = new ConsoleErrorEvent($input, $output, $e); | ||
$this->dispatcher->dispatch(ConsoleEvents::ERROR, $event); | ||
if (!($e instanceof CommandNotFoundException && !$e instanceof NamespaceNotFoundException) || 1 !== count($alternatives = $e->getAlternatives()) || !$input->isInteractive()) { | ||
if (null !== $this->dispatcher) { | ||
$event = new ConsoleErrorEvent($input, $output, $e); | ||
$this->dispatcher->dispatch(ConsoleEvents::ERROR, $event); | ||
|
||
if (0 === $event->getExitCode()) { | ||
return 0; | ||
if (0 === $event->getExitCode()) { | ||
return 0; | ||
} | ||
|
||
$e = $event->getError(); | ||
} | ||
|
||
$e = $event->getError(); | ||
throw $e; | ||
} | ||
|
||
throw $e; | ||
$alternative = $alternatives[0]; | ||
$question = new ConfirmationQuestion(sprintf("<error>Command \"%s\" is not defined.</error>\n\nDo you want to run \"%s\" instead? [y/n] ", $name, $alternative), false); | ||
8000 |
||
if (!(new QuestionHelper())->ask($input, $output, $question)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest to use |
||
if (null !== $this->dispatcher) { | ||
$event = new ConsoleErrorEvent($input, $output, $e); | ||
$this->dispatcher->dispatch(ConsoleEvents::ERROR, $event); | ||
|
||
return $event->getExitCode(); | ||
} | ||
|
||
return 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error event allows to set a custom exit code and override the exception, it must keep working |
||
} | ||
|
||
$command = $this->find($alternative); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
$this->runningCommand = $command; | ||
|
@@ -533,7 +553,7 @@ public function getNamespaces() | |
* | ||
* @return string A registered namespace | ||
* | ||
* @throws CommandNotFoundException When namespace is incorrect or ambiguous | ||
* @throws NamespaceNotFoundException When namespace is incorrect or ambiguous | ||
*/ | ||
public function findNamespace($namespace) | ||
{ | ||
|
@@ -554,12 +574,12 @@ public function findNamespace($namespace) | |
$message .= implode("\n ", $alternatives); | ||
} | ||
|
||
throw new CommandNotFoundException($message, $alternatives); | ||
throw new NamespaceNotFoundException($message, $alternatives); | ||
} | ||
|
||
$exact = in_array($namespace, $namespaces, true); | ||
if (count($namespaces) > 1 && !$exact) { | ||
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)); | ||
throw new NamespaceNotFoundException(sprintf("The namespace \"%s\" is ambiguous.\nDid you mean one of these?\n%s", $namespace, $this->getAbbreviationSuggestions(array_values($namespaces))), array_values($namespaces)); | ||
} | ||
|
||
return $exact ? $namespace : reset($namespaces); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Console\Exception; | ||
|
||
/** | ||
* Represents an incorrect namespace typed in the console. | ||
* | ||
* @author Pierre du Plessis <pdples@gmail.com> | ||
*/ | ||
class NamespaceNotFoundException extends CommandNotFoundException | ||
{ | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This addition is not strictly needed, right? Not sure it's useful, and it involves a BC break with no upgrade path in the future (stop extending There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No it's not strictly needed, but I needed a way to distinguish between not finding a namespace and not finding a command to prevent incorrect suggestions when typing an incorrect namespace. Keeping this class extending from |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\CommandLoader\FactoryCommandLoader; | ||
use Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass; | ||
use Symfony\Component\Console\Exception\NamespaceNotFoundException; | ||
use Symfony\Component\Console\Helper\HelperSet; | ||
use Symfony\Component\Console\Helper\FormatterHelper; | ||
use Symfony\Component\Console\Input\ArgvInput; | ||
|
@@ -56,6 +57,7 @@ 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.'/FooWithoutAliasCommand.php'; | ||
require_once self::$fixturesPath.'/TestTiti.php'; | ||
require_once self::$fixturesPath.'/TestToto.php'; | ||
} | ||
|
@@ -275,10 +277,10 @@ public function testFindAmbiguousNamespace() | |
$expectedMsg = "The namespace \"f\" is ambiguous.\nDid you mean one of these?\n foo\n foo1"; | ||
|
||
if (method_exists($this, 'expectException')) { | ||
$this->expectException(CommandNotFoundException::class); | ||
$this->expectException(NamespaceNotFoundException::class); | ||
$this->expectExceptionMessage($expectedMsg); | ||
} else { | ||
$this->setExpectedException(CommandNotFoundException::class, $expectedMsg); | ||
$this->setExpectedException(NamespaceNotFoundException::class, $expectedMsg); | ||
} | ||
|
||
$application->findNamespace('f'); | ||
|
@@ -293,7 +295,7 @@ public function testFindNonAmbiguous() | |
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\Console\Exception\CommandNotFoundException | ||
* @expectedException \Symfony\Component\Console\Exception\NamespaceNotFoundException | ||
* @expectedExceptionMessage There are no commands defined in the "bar" namespace. | ||
*/ | ||
public function testFindInvalidNamespace() | ||
|
@@ -457,6 +459,47 @@ public function testFindAlternativeExceptionMessageSingle($name) | |
$application->find($name); | ||
} | ||
|
||
public function testDontRunAlternativeNamespaceName() | ||
{ | ||
$application = new Application(); | ||
$application->add(new \Foo1Command()); | ||
$application->setAutoExit(false); | ||
$tester = new ApplicationTester($application); | ||
$tester->run(array('command' => 'foos:bar1'), array('decorated' => false)); | ||
$this->assertSame(" | ||
|
||
There are no commands defined in the \"foos\" namespace. | ||
|
||
Did you mean this? | ||
foo | ||
|
||
|
||
", $tester->getDisplay(true)); | ||
} | ||
|
||
public function testCanRunAlternativeCommandName() | ||
{ | ||
$application = new Application(); | ||
$application->add(new \FooWithoutAliasCommand()); | ||
$application->setAutoExit(false); | ||
$tester = new ApplicationTester($application); | ||
$tester->setInputs(array('y')); | ||
$tester->run(array('command' => 'foos'), array('decorated' => false)); | ||
$this->assertSame("Command \"foos\" is not defined.\n\nDo you want to run \"foo\" instead? [y/n] called\n", $tester->getDisplay(true)); | ||
} | ||
|
||
public function testDontRunAlternativeCommandName() | ||
{ | ||
$application = new Application(); | ||
$application->add(new \FooWithoutAliasCommand()); | ||
$application->setAutoExit(false); | ||
$tester = new ApplicationTester($application); | ||
$tester->setInputs(array('n')); | ||
$exitCode = $tester->run(array('command' => 'foos'), array('decorated' => false)); | ||
$this->assertSame(1, $exitCode); | ||
$this->assertSame("Command \"foos\" is not defined.\n\nDo you want to run \"foo\" instead? [y/n] ", $tester->getDisplay(true)); | ||
} | ||
|
||
public function provideInvalidCommandNamesSingle() | ||
{ | ||
return array( | ||
|
@@ -486,10 +529,10 @@ public function testFindAlternativeExceptionMessageMultiple() | |
// Namespace + plural | ||
try { | ||
$application->find('foo2:bar'); | ||
$this->fail('->find() throws a CommandNotFoundException if command does not exist, with alternatives'); | ||
$this->fail('->find() throws a NamespaceNotFoundException if command does not exist, with alternatives'); | ||
} catch (\Exception $e) { | ||
$this->assertInstanceOf('Symfony\Component\Console\Exception\CommandNotFoundException', $e, '->find() throws a CommandNotFoundException if command does not exist, with alternatives'); | ||
$this->assertRegExp('/Did you mean one of these/', $e->getMessage(), '->find() throws a CommandNotFoundException if command does not exist, with alternatives'); | ||
$this->assertInstanceOf('Symfony\Component\Console\Exception\NamespaceNotFoundException', $e, '->find() throws a NamespaceNotFoundException if command does not exist, with alternatives'); | ||
$this->assertRegExp('/Did you mean one of these/', $e->getMessage(), '->find() throws a NamespaceNotFoundException if command does not exist, with alternatives'); | ||
$this->assertRegExp('/foo1/', $e->getMessage()); | ||
} | ||
|
||
|
@@ -563,26 +606,26 @@ public function testFindAlternativeNamespace() | |
|
||
try { | ||
$application->find('Unknown-namespace:Unknown-command'); | ||
$this->fail('->find() throws a CommandNotFoundException if namespace does not exist'); | ||
$this->fail('->find() throws a NamespaceNotFoundException if namespace does not exist'); | ||
} catch (\Exception $e) { | ||
$this->assertInstanceOf('Symfony\Component\Console\Exception\CommandNotFoundException', $e, '->find() throws a CommandNotFoundException if namespace does not exist'); | ||
$this->assertInstanceOf('Symfony\Component\Console\Exception\NamespaceNotFoundException', $e, '->find() throws a NamespaceNotFoundException if namespace does not exist'); | ||
$this->assertSame(array(), $e->getAlternatives()); | ||
$this->assertEquals('There are no commands defined in the "Unknown-namespace" namespace.', $e->getMessage(), '->find() throws a CommandNotFoundException if namespace does not exist, without alternatives'); | ||
$this->assertEquals('There are no commands defined in the "Unknown-namespace" namespace.', $e->getMessage(), '->find() throws a NamespaceNotFoundException if namespace does not exist, without alternatives'); | ||
} | ||
|
||
try { | ||
$application->find('foo2:command'); | ||
$this->fail('->find() throws a CommandNotFoundException if namespace does not exist'); | ||
$this->fail('->find() throws a NamespaceNotFoundException if namespace does not exist'); | ||
} catch (\Exception $e) { | ||
$this->assertInstanceOf('Symfony\Component\Console\Exception\CommandNotFoundException', $e, '->find() throws a CommandNotFoundException if namespace does not exist'); | ||
$this->assertInstanceOf('Symfony\Component\Console\Exception\NamespaceNotFoundException', $e, '->find() throws a NamespaceNotFoundException if namespace does not exist'); | ||
$this->assertCount(3, $e->getAlternatives()); | ||
$this->assertContains('foo', $e->getAlternatives()); | ||
$this->assertContains('foo1', $e->getAlternatives()); | ||
$this->assertContains('foo3', $e->getAlternatives()); | ||
$this->assertRegExp('/There are no commands defined in the "foo2" namespace./', $e->getMessage(), '->find() throws a CommandNotFoundException if namespace does not exist, with alternative'); | ||
$this->assertRegExp('/foo/', $e->getMessage(), '->find() throws a CommandNotFoundException if namespace does not exist, with alternative : "foo"'); | ||
$this->assertRegExp('/foo1/', $e->getMessage(), '->find() throws a CommandNotFoundException if namespace does not exist, with alternative : "foo1"'); | ||
$this->assertRegExp('/foo3/', $e->getMessage(), '->find() throws a CommandNotFoundException if namespace does not exist, with alternative : "foo3"'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They are still valid and ensure the new class keeps extending it (prevent a BC break) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would revert and only add a new assertion, avoiding merge conflicts |
||
$this->assertRegExp('/There are no commands defined in the "foo2" namespace./', $e->getMessage(), '->find() throws a NamespaceNotFoundException if namespace does not exist, with alternative'); | ||
$this->assertRegExp('/foo/', $e->getMessage(), '->find() throws a NamespaceNotFoundException if namespace does not exist, with alternative : "foo"'); | ||
$this->assertRegExp('/foo1/', $e->getMessage(), '->find() throws a NamespaceNotFoundException if namespace does not exist, with alternative : "foo1"'); | ||
$this->assertRegExp('/foo3/', $e->getMessage(), '->find() throws a NamespaceNotFoundException if namespace does not exist, with alternative : "foo3"'); | ||
} | ||
} | ||
|
||
|
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 FooWithoutAliasCommand extends Command | ||
{ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('foo') | ||
->setDescription('The foo command') | ||
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$output->writeln('called'); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This proposes to run the alternative even if it's a namespace only, right? A test case would be good to make sure it does not
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, if it's a namespace only then the behaviour stays unchanged (Which would giving you an error and giving a list suggested namespaces without the option to run a command)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added another test case to cover namespace error