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

Suggestion for unknown command and unknown option #1590

Merged
merged 23 commits into from
Sep 6, 2021

Conversation

shadowspawn
Copy link
Collaborator

@shadowspawn shadowspawn commented Aug 28, 2021

Pull Request

Problem

It is common for big utilities to suggest matches for command typos:

% git loe
git: 'loe' is not a git command. See 'git --help'.

The most similar commands are
	clone
	log

% npm publih

Usage: npm <command>

...short-help...

Did you mean one of these?
    publish
    unpublish

% hg loe
hg: unknown command 'loe'
(did you mean one of clone, locate, log?)

CLI parsing:

Resolves issue: #1015

Note. There have not been a lot of requests for this but I have been noticing it in programs I use. It is already possible for user to add their own suggestions for commands, and there has been interest in that:

Solution

  1. Check for possible matches to add to the unknown-command error message. Take into account the visible command alias, the implicit help command, and ignore hidden commands. Use the Damerau–Levenshtein distance to calculate the edit distance, which tries insertion, deletion, substitution, and transposition.
% node index.js rat
error: unknown command 'rat'
(Did you mean cat?)

This does not affect users who already have their own command:* listener.

  1. Check for possible matches to add to the unknown-option error message. (None of git or hg or npm do this.) Take into account global options, the implicit help option, and ignore hidden options.
% node index.js sub --debgu
error: unknown option '--debgu'
(Did you mean --debug?)
  1. Offer multiple suggestions (if more than one at closest edit distance).
% node index.js fake
error: unknown command 'fake'
(Did you mean one of bake, make, rake?)
  1. Make the suggestions off by default to minimise breakage in Commander 8, and probably switch to on by default in Commander 9.

ChangeLog

  • added: .showSuggestionAfterError() to show suggestions after unknown command or unknown option

And fixed a bug discovered when testing:

  • changed: show error for unknown global option before subcommand (rather than just help)

@shadowspawn shadowspawn marked this pull request as draft August 28, 2021 12:06
@shadowspawn shadowspawn changed the title Suggestion for unknown command Suggestion for unknown command and unknown option Aug 29, 2021
@shadowspawn shadowspawn marked this pull request as ready for review September 3, 2021 22:47
Copy link
Collaborator

@abetomo abetomo left a comment

Choose a reason for hiding this comment

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

awesome!

@shadowspawn
Copy link
Collaborator Author

Thanks Abetomo. 👍

@shadowspawn shadowspawn merged commit 91ccfd5 into tj:develop Sep 6, 2021
@shadowspawn shadowspawn deleted the feature/suggest branch September 6, 2021 06:27
@shadowspawn shadowspawn added pending release Merged into a branch for a future release, but not released yet and removed pending release Merged into a branch for a future release, but not released yet labels Sep 6, 2021
@shadowspawn
Copy link
Collaborator Author

Shipped in Commander v8.2.0

nicojs added a commit to stryker-mutator/stryker-js that referenced this pull request Oct 20, 2021
Example:

```
stryker run --concurency 1

error: unknown option '--concurency'
(Did you mean --concurrency?)
```

Thanks to [`showSuggestionAfterError`](tj/commander.js#1590)
@nicojs
Copy link

nicojs commented Oct 20, 2021

Thanks for adding this feature. Easy to use and works like a charm. Brings a lot of value! 💚

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.

4 participants