Skip to content
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

Suggest most simillar command #1272

Merged
merged 5 commits into from
Aug 4, 2023

Conversation

federicoemartinez
Copy link
Contributor

@federicoemartinez federicoemartinez commented Jun 29, 2023

closes #1271
The app can be configured to suggest the most similar command. In that case, the most similar command of the visible commands (more similar than a threshold) will be suggested and added to the default error message.

By default SequenceMatcher.ratio() from the standard library is used but a subclass can define _suggest_similar_command with a new algorithm.

cmd2/cmd2.py Outdated
@@ -529,6 +531,9 @@ def __init__(
# Add functions decorated to be subcommands
self._register_subcommands(self)

self.suggest_similar_command = suggest_similar_command
self.default_suggestion_message = "The most similar command is {}"
Copy link
Contributor

Choose a reason for hiding this comment

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

When I doubt I tend to fall back on what bash does. How about:
', did you mean: {}'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was my first choice. I changed it because of git:

git comit

git: 'comit' is not a git command. See 'git --help'.

The most similar command is
	commit

But sure, I can change it.

cmd2/cmd2.py Outdated
if self.suggest_similar_command:
suggested_command = self._suggest_similar_command(statement.command)
if suggested_command:
err_msg = err_msg + os.linesep + self.default_suggestion_message.format(suggested_command)
Copy link
Contributor

Choose a reason for hiding this comment

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

With my alternate suggestion message, no need for line separator.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can use a space

@codecov
Copy link

codecov bot commented Jun 30, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (a55bb1e) 98.54% compared to head (9968437) 98.55%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1272   +/-   ##
=======================================
  Coverage   98.54%   98.55%           
=======================================
  Files          22       22           
  Lines        5705     5728   +23     
=======================================
+ Hits         5622     5645   +23     
  Misses         83       83           
Files Changed Coverage Δ
cmd2/cmd2.py 98.25% <100.00%> (+<0.01%) ⬆️
cmd2/utils.py 98.06% <100.00%> (+0.05%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@federicoemartinez
Copy link
Contributor Author

federicoemartinez commented Jun 30, 2023

I see doc jobs are failing but I don't think that's related to this branch

@pablopissi
Copy link

It would be great to have this merged

@anselor
Copy link
Contributor

anselor commented Jul 28, 2023

My PR just merged which also addresses many of the issues you probably ran into. You may want to rebase over latest to see.

@anselor
Copy link
Contributor

anselor commented Jul 29, 2023

I'm not sure what's going on with your branch but my changes are now showing up as changes in your branch. I highly suggest squashing related commits and then rebasing it all on top of latest master.

@federicoemartinez
Copy link
Contributor Author

I'm not sure what's going on with your branch but my changes are now showing up as changes in your branch. I highly suggest sq

I think it's done now. I made a mistake rebasing, I'm much more used to mercurial to be honest.
Thanks for pointing it out.

anselor
anselor previously approved these changes Aug 2, 2023
@federicoemartinez
Copy link
Contributor Author

@anselor I applied the changes from the formatter jobs and the doc job. Don't know about the linter. It looks like it is not from my PR

@anselor
Copy link
Contributor

anselor commented Aug 2, 2023

@anselor I applied the changes from the formatter jobs and the doc job. Don't know about the linter. It looks like it is not from my PR

There pyinvoke commands you can run locally to check

@federicoemartinez
Copy link
Contributor Author

I'm trying

@anselor I applied the changes from the formatter jobs and the doc job. Don't know about the linter. It looks like it is not from my PR

There pyinvoke commands you can run locally to check

They passed now (Though with python 3.8 in my local setup).
The linter job still fails but it is totally unrelated to my branch

@anselor
Copy link
Contributor

anselor commented Aug 3, 2023

@federicoemartinez The linter problem looks spurious and not applicable to the situation. Would you mind just adding # noqa: E731 to those lines to make sure this doesn't hide something else?

@federicoemartinez
Copy link
Contributor Author

federicoemartinez commented Aug 3, 2023

done!
Does this job work for master?

@anselor
Copy link
Contributor

anselor commented Aug 3, 2023

As far as I'm aware it passed before the last PR. What I've sometimes see happen is the validation library updates and new errors are found that did not previously fail.

@anselor anselor merged commit d4a2fc3 into python-cmd2:master Aug 4, 2023
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Adding a did you mean? or most similar command in case of error
3 participants