-
-
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
[Console] Add option to automatically run suggested command if there is only 1 alternative #25732
Conversation
e8f9bdf
to
5abc30f
Compare
3524d9d
to
bc36d20
Compare
*/ | ||
class NamespaceNotFoundException extends CommandNotFoundException | ||
{ | ||
} |
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 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 CommandNotFoundException
, new unhandled exceptions in userland).
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 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 CommandNotFound
seemed weird as they are for different use cases, which is why I added the deprecation
* | ||
* @author Pierre du Plessis <pdples@gmail.com> | ||
* | ||
* @deprecated This class won't extend CommandNotFoundException in Symfony 5 |
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.
@deprecated
should only be used when a class is actually deprecated, the debug class loader parses such annotations, could lead to wrong notices e.g. when extending it.
@@ -226,7 +228,18 @@ public function doRun(InputInterface $input, OutputInterface $output) | ||
$e = $event->getError(); | ||
} | ||
|
||
throw $e; | ||
if (!($e instanceof CommandNotFoundException && !$e instanceof NamespaceNotFoundException) || 1 !== count($alternatives = $e->getAlternatives()) || !$input->isInteractive()) { |
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.
Doing this here keeps dispatching a console.error event, not sure it's desired in case the user choose to run the single alternative? Especially given it's currently not taking into account the potential changes made on the event (see comment below)
* removed `ConsoleExceptionEvent` | ||
* removed `ConsoleEvents::EXCEPTION` | ||
* removed `ConsoleExceptionEvent` | ||
* removed `ConsoleEvents::EXCEPTION` |
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.
please revert, should be done on 3.4
@@ -1,5 +1,5 @@ | ||
|
||
In ApplicationTest.php line 770: | ||
In ApplicationTest.php line 795: |
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.
Going to be a nightmare, we should make the related tests more flexible by using a placeholder where they have been introduced
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.
Addressed in #25794
$question = new ConfirmationQuestion(sprintf("<error>Command \"%s\" is not defined.</error>\n\nDo you want to run \"%s\" instead? [y/n] ", $name, $alternative), false); | ||
|
||
if (!(new QuestionHelper())->ask($input, $output, $question)) { | ||
return 1; |
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.
The error event allows to set a custom exit code and override the exception, it must keep working
How that? AFAIU it behaves the same for both exceptions currently |
6c1cd01
to
34222d7
Compare
@pierredup Can you rebase to get rid of the merge commit? We never merge a PR with a merge commit. Thanks. |
6e93303
to
222af7c
Compare
222af7c
to
e4deda7
Compare
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()) { |
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
return 1; | ||
} | ||
|
||
$command = $this->find($alternative); |
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.
I suggest to use wrong lineSymfonyStyle
instead
$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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
I would revert and only add a new assertion, avoiding merge conflicts
$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); | ||
|
||
if (!(new QuestionHelper())->ask($input, $output, $question)) { |
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.
I suggest to use SymfonyStyle
instead
@pierredup Can you look at the failing tests? looks like a spacing issue. |
New failures on travis seems unrelated |
Thank you @pierredup. |
…mmand if there is only 1 alternative (pierredup) This PR was squashed before being merged into the 4.1-dev branch (closes #25732). Discussion ---------- [Console] Add option to automatically run suggested command if there is only 1 alternative | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | When mistyping a console command, you get an error giving suggested commands. If there is only 1 alternative suggestion, this PR will give you the option to run that command instead. This makes it easier to run the correct command without having to re-type/copy-paste/update the previous run command  Commits ------- 83d52f0 [Console] Add option to automatically run suggested command if there is only 1 alternative
When mistyping a console command, you get an error giving suggested commands.
If there is only 1 alternative suggestion, this PR will give you the option to run that command instead. This makes it easier to run the correct command without having to re-type/copy-paste/update the previous run command