Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[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

Conversation

pierredup
Copy link
Contributor

@pierredup pierredup commented Jan 9, 2018

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

console

@pierredup pierredup force-pushed the console-run-command-suggestion branch from e8f9bdf to 5abc30f Compare January 9, 2018 14:02
@nicolas-grekas nicolas-grekas added this to the 4.1 milestone Jan 9, 2018
@pierredup pierredup changed the title [Console] Add options to automatically run suggested command if there is only 1 alternative [Console] Add option to automatically run suggested command if there is only 1 alternative Jan 9, 2018
@pierredup pierredup force-pushed the console-run-command-suggestion branch 2 times, most recently from 3524d9d to bc36d20 Compare January 9, 2018 20:26
*/
class NamespaceNotFoundException extends CommandNotFoundException
{
}
Copy link
Member

@chalasr chalasr Jan 11, 2018

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).

Copy link
Contributor Author

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
Copy link
Member

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()) {
Copy link
Member

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`
Copy link
Member

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:
Copy link
Member

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

Copy link
Contributor Author

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;
Copy link
Member

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

@chalasr
Copy link
Member

chalasr commented Jan 12, 2018

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

How that? AFAIU it behaves the same for both exceptions currently

@pierredup pierredup force-pushed the console-run-command-suggestion branch from 6c1cd01 to 34222d7 Compare January 15, 2018 12:31
@chalasr chalasr self-requested a review January 19, 2018 14:47
@fabpot
Copy link
Member

fabpot commented Jan 23, 2018

@pierredup Can you rebase to get rid of the merge commit? We never merge a PR with a merge commit. Thanks.

@pierredup pierredup force-pushed the console-run-command-suggestion branch from 6e93303 to 222af7c Compare January 23, 2018 06:36
@pierredup pierredup force-pushed the console-run-command-suggestion branch from 222af7c to e4deda7 Compare January 23, 2018 06:38
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()) {
Copy link
Member

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

Copy link
Contributor Author

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)

Copy link
Contributor Author

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);
Copy link
Member

@chalasr chalasr Feb 8, 2018

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 wrong line

$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"');
Copy link
Member

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)

Copy link
Member

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)) {
Copy link
Member

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

@chalasr
Copy link
Member

chalasr commented Feb 17, 2018

@pierredup Can you look at the failing tests? looks like a spacing issue.

@pierredup
Copy link
Contributor Author

New failures on travis seems unrelated

@fabpot
Copy link
Member

fabpot commented Feb 22, 2018

Thank you @pierredup.

@fabpot fabpot closed this Feb 22, 2018
fabpot added a commit that referenced this pull request Feb 22, 2018
…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

![console](https://user-images.githubusercontent.com/144858/34724377-4b46c726-f556-11e7-94a3-a9d7c9d75e74.gif)

Commits
-------

83d52f0 [Console] Add option to automatically run suggested command if there is only 1 alternative
@pierredup pierredup deleted the console-run-command-suggestion branch February 22, 2018 06:50
@fabpot fabpot mentioned this pull request May 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.