Skip to content

Sort alternatives alphabetically when a command is not found #19887

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

Merged
merged 2 commits into from
Mar 22, 2017

Conversation

javiereguiluz
Copy link
Member

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #10893
License MIT
Doc PR -

@fabpot
Copy link
Member

fabpot commented Sep 8, 2016

As we are listing alternatives in a lot of different places in the framework, wouldn't it make sense to change the sort for all of them?

'afoobar',
'afoobar1',
'afoobar2',
'foo1:bar',
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 have expected foo1:bar to be after foo:bar as intuitively, I sort the namespace first, then the command name. Not sure if this is something we need to fix though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's probably because of the : when sorting

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do understand that, my point is that we need to take care of that : when sorting like a human would do.

Copy link
Member Author

@javiereguiluz javiereguiluz Sep 14, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The good thing is that in real apps we probably won't have this problem. This example (foo:, foo1:, foo3:) is too abstract and probably our users don't use project:, project1: or app:, app1: namespaces.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair enough

@fabpot
Copy link
Member

fabpot commented Sep 14, 2016

Have you had time to have a look at the other exceptions using alternatives to see if sorting them would also make sense?

@ro0NL
Copy link
Contributor

ro0NL commented Sep 17, 2016

I thought of this yesterday, and it's more or less related; wouldn't it be nice to filter the command list if a command is not found?

Ie. bin/console vs. bin/console keyword render the same output, but the latter only shows matching commands and some little info like "Showing 1 of 3 commands matching 'keyword'".

@fabpot
Copy link
Member

fabpot commented Nov 9, 2016

@javiereguiluz Any news?

@nicolas-grekas nicolas-grekas added this to the 3.x milestone Dec 6, 2016
@fabpot
Copy link
Member

fabpot commented Mar 1, 2017

@javiereguiluz Still interested in finishing this PR?

@javiereguiluz javiereguiluz self-assigned this Mar 1, 2017
@javiereguiluz
Copy link
Member Author

Yes ... but I don't know where to look for more places where this feature could be implemented. Anyone can give me a clue? Thanks!

@fabpot
Copy link
Member

fabpot commented Mar 1, 2017

I think looking for levenshtein in the code base would give you all the places where we need to make the same change.

@javiereguiluz
Copy link
Member Author

I've committed some changes:

  • For the list of commands, I think that sorting best matches alphabetically is the most natural thing...
  • ... but for the other levenshtein candidates (service ids, param names, etc.) I propose to sort them from best to worst (i.e. from lowest to highest levenshtein distance)

if (false !== strpos($definedTag, $tag) || levenshtein($tag, $definedTag) <= strlen($tag) / 3) {
$candidates[] = $definedTag;
if (false !== strpos($definedTag, $tag) || $lev = levenshtein($tag, $definedTag) <= strlen($tag) / 3) {
$candidates[$definedTag] = $lev;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just $candidates[$definedTag] = true;? That would avoid creating the $lev variable that is not used anyway.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this. $lev is the score ... and we need that for the two new lines added below to sort results from best to worst:

asort($candidates);
$candidates = array_keys($candidates);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@fabpot
Copy link
Member

fabpot commented Mar 14, 2017

Tests fail on deps=low, we should find a way to fix that.

@javiereguiluz
Copy link
Member Author

I can't see which is the error that makes test fail for "deps = low". Can anyone help me? Thanks!


Some context --> The original test was:

Did you mean one of these: "bar", "baz"?

But after this PR, that fails on most Symfony versions, so I changed it to:

Did you mean one of these: "baz", "bar"?

and now test pass ... except when using "deps = low", which fails because the expected output is again:

Did you mean one of these: "bar", "baz"?

@linaori
Copy link
Contributor

linaori commented Mar 19, 2017

@javiereguiluz my guess is that the DependencyInjection needs a higher console version for that as it seems like it's the DependencyInjection component that fails. I could be wrong though, but that's my hunch after seeing the failure.

@javiereguiluz
Copy link
Member Author

After thinking about this, what if we do the following:

  1. Remove the last 3 commits
  2. Keep only the original feature that sorts commands alphabetically

Reason:

We already show very good alternatives because we limit the levenhstein cost to < 3, so every alternative displayed is almost as good as the other ones. The proposed changes wouldn't make the results much better.

What do you think?

@fabpot
Copy link
Member

fabpot commented Mar 22, 2017

Make sense 👍

@fabpot
Copy link
Member

fabpot commented Mar 22, 2017

Thank you @javiereguiluz.

@fabpot fabpot merged commit ba6c946 into symfony:master Mar 22, 2017
fabpot added a commit that referenced this pull request Mar 22, 2017
… found (javiereguiluz)

This PR was squashed before being merged into the 3.3-dev branch (closes #19887).

Discussion
----------

Sort alternatives alphabetically when a command is not found

| Q | A |
| --- | --- |
| Branch? | master |
| Bug fix? | no |
| New feature? | yes |
| BC breaks? | no |
| Deprecations? | no |
| Tests pass? | yes |
| Fixed tickets | #10893 |
| License | MIT |
| Doc PR | - |

Commits
-------

ba6c946 Sort commands like a human would do
f04b1bd Sort alternatives alphabetically when a command is not found
@nicolas-grekas nicolas-grekas modified the milestones: 3.x, 3.3 Mar 24, 2017
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.

6 participants