-
-
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
Closed
pierredup
wants to merge
9
commits into
symfony:master
from
pierredup:console-run-command-suggestion
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5bdcdda
Add options to automatically run suggested command if there is only 1…
pierredup d93ca23
Remove deprecation and future BC break
pierredup f42a2bb
Revert unrelated changes in the changelog
pierredup e4deda7
Dispatch console error event
pierredup 1b9f9eb
Dont run command if suggestion is only a namespace
pierredup f8a816d
Use SymfonyStyle
pierredup bf46ffe
Revert some changes to tests
pierredup d7dc6c7
Fix CS
pierredup 4de52d2
Change spacing
pierredup File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\Style\SymfonyStyle; | ||
use Symfony\Component\Debug\ErrorHandler; | ||
use Symfony\Component\Debug\Exception\FatalThrowableError; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
|
@@ -223,18 +225,37 @@ 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]; | ||
|
||
$style = new SymfonyStyle($input, $output); | ||
$style->block(sprintf("\nCommand \"%s\" is not defined.\n", $name), null, 'error'); | ||
if (!$style->confirm(sprintf('Do you want to run "%s" instead? ', $alternative), false)) { | ||
if (null !== $this->dispatcher) { | ||
$event = new ConsoleErrorEvent($input, $output, $e); | ||
$this->dispatcher->dispatch(ConsoleEvents::ERROR, $event); | ||
|
||
return $event->getExitCode(); | ||
} | ||
|
||
return 1; | ||
} | ||
|
||
$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 +554,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 +575,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); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/Symfony/Component/Console/Exception/NamespaceNotFoundException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/Symfony/Component/Console/Tests/Fixtures/FooWithoutAliasCommand.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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