-
Notifications
You must be signed in to change notification settings - Fork 0
Suggest a command or flag when one is mistyped #17
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| package mflags | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
| ) | ||
|
|
||
| // UnknownCommandError reports a command word that matches nothing registered. | ||
| // It carries the surrounding context — which program, which parent command, and | ||
| // what the user might have meant — so callers can render it themselves rather | ||
| // than parsing the message back out of a string. | ||
| type UnknownCommandError struct { | ||
| // Program is the binary name, e.g. "miren". | ||
| Program string | ||
| // ParentPath is the command the unknown word appeared under, e.g. "app". | ||
| // Empty when the word was typed at the top level. | ||
| ParentPath string | ||
| // Name is the single word that matched nothing. | ||
| Name string | ||
| // Suggestions holds close matches, nearest first. May be empty. | ||
| Suggestions []string | ||
| } | ||
|
|
||
| func (e *UnknownCommandError) Error() string { | ||
| helpTarget := strings.TrimSpace(e.Program + " " + e.ParentPath) | ||
|
|
||
| var sections []string | ||
|
|
||
| if e.ParentPath != "" { | ||
| sections = append(sections, fmt.Sprintf("unknown command %q for %q", e.Name, helpTarget)) | ||
| } else { | ||
| sections = append(sections, fmt.Sprintf("unknown command %q", e.Name)) | ||
| } | ||
|
|
||
| if len(e.Suggestions) > 0 { | ||
| var b strings.Builder | ||
| b.WriteString("Did you mean?") | ||
| for _, s := range e.Suggestions { | ||
| fmt.Fprintf(&b, "\n %s", s) | ||
| } | ||
| sections = append(sections, b.String()) | ||
| } | ||
|
|
||
| if helpTarget != "" { | ||
| sections = append(sections, fmt.Sprintf("Run '%s --help' to see available commands.", helpTarget)) | ||
| } | ||
|
|
||
| return strings.Join(sections, "\n\n") | ||
| } | ||
|
|
||
| // UnknownFlagError reports a flag the command does not define. It unwraps to | ||
| // ErrUnknownFlag, so existing errors.Is checks keep working. | ||
| type UnknownFlagError struct { | ||
| // Flag is the flag as the user typed it, dashes included. | ||
| Flag string | ||
| // Suggestions holds close long-flag names, dashes included, nearest first. | ||
| // Short flags carry no suggestions: a single letter is too little to guess | ||
| // from. | ||
| Suggestions []string | ||
| } | ||
|
|
||
| func (e *UnknownFlagError) Error() string { | ||
| sections := []string{"unknown flag: " + e.Flag} | ||
|
|
||
| if len(e.Suggestions) > 0 { | ||
| var b strings.Builder | ||
| b.WriteString("Did you mean?") | ||
| for _, s := range e.Suggestions { | ||
| fmt.Fprintf(&b, "\n %s", s) | ||
| } | ||
| sections = append(sections, b.String()) | ||
| } | ||
|
|
||
| return strings.Join(sections, "\n\n") | ||
| } | ||
|
|
||
| func (e *UnknownFlagError) Unwrap() error { return ErrUnknownFlag } | ||
|
|
||
| // UnexpectedArgsError reports positional arguments a command has nowhere to put. | ||
| type UnexpectedArgsError struct { | ||
| Args []string | ||
| } | ||
|
|
||
| func (e *UnexpectedArgsError) Error() string { | ||
| if len(e.Args) == 1 { | ||
| return fmt.Sprintf("unexpected argument %q", e.Args[0]) | ||
| } | ||
|
|
||
| quoted := make([]string, 0, len(e.Args)) | ||
| for _, a := range e.Args { | ||
| quoted = append(quoted, fmt.Sprintf("%q", a)) | ||
| } | ||
| return "unexpected arguments: " + strings.Join(quoted, " ") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.