Improve suggestions - add explicit SuggestFor attribute
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Fabiano Franz · committed by Fabiano Franz · parent 3afa4db · Browse files · 3 files·+29 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+29 added−6 removed
- other:+29 added−6 removed
Provenance
- Commit
a83a172- Tree
0128820- Author
Fabiano Franz <ffranz@redhat.com>- Committer
Fabiano Franz <ffranz@redhat.com>- Parent
3afa4db
3 files changed+29 added−6 removed
modified:cobra_test.go+5 added-3 removed
80 unmodified lines81 },82 }83 84 var cmdTimes = &Command{85- Use: "times [# times] [string to echo]",86- Short: "Echo anything to the screen more times",87- Long: `a slightly useless command for testing.`,85+ Use: "times [# times] [string to echo]",86+ SuggestFor: []string{"counts"},87+ Short: "Echo anything to the screen more times",88+ Long: `a slightly useless command for testing.`,89 PersistentPreRun: func(cmd *Command, args []string) {90 timesPersPre = args91 },92 Run: func(cmd *Command, args []string) {734 unmodified lines827 "timely": "times",828 "ri": "",829 "timezone": "",830 "foo": "",831+ "counts": "times",832 }833 834 for typo, suggestion := range tests {835 for _, suggestionsDisabled := range []bool{false, true} {223 unmodified linesmodified:command.go+12 added-3 removed
37 unmodified lines38 // The one-line usage message.39 Use string40 // An array of aliases that can be used instead of the first word in Use.41 Aliases []string42+ // An array of command names for which this command will be suggested - similar to aliases but only suggests.43+ SuggestFor []string44 // The short description shown in the 'help' output.45 Short string46 // The long message shown in the 'help <this-command>' output.47 Long string400 unmodified lines448 449 return commandFound, a, nil450 }451 450-func (c *Command) SuggestionsFor(cmdName string) []string {452+func (c *Command) SuggestionsFor(typedName string) []string {453 suggestions := []string{}454 for _, cmd := range c.commands {455 if cmd.IsAvailableCommand() {454- levenshtein := ld(cmdName, cmd.Name(), true)455- if levenshtein <= c.SuggestionsMinimumDistance || strings.HasPrefix(strings.ToLower(cmd.Name()), strings.ToLower(cmdName)) {456+ levenshteinDistance := ld(typedName, cmd.Name(), true)457+ suggestByLevenshtein := levenshteinDistance <= c.SuggestionsMinimumDistance458+ suggestByPrefix := strings.HasPrefix(strings.ToLower(cmd.Name()), strings.ToLower(typedName))459+ if suggestByLevenshtein || suggestByPrefix {460 suggestions = append(suggestions, cmd.Name())461 }462+ for _, explicitSuggestion := range cmd.SuggestFor {463+ if strings.EqualFold(typedName, explicitSuggestion) {464+ suggestions = append(suggestions, cmd.Name())465+ }466+ }467 }468 }469 return suggestions470 }689 unmodified linesmodified:README.md+12 added
440 unmodified lines441 or 442 443 command.SuggestionsMinimumDistance = 1444 445+You can also explicitly set names for which a given command will be suggested using the `SuggestFor` attribute. This allows suggestions for strings that are not close in terms of string distance, but makes sense in your set of commands and for some which you don't want aliases. Example:446+447+```448+$ hugo delete449+unknown command "delete" for "hugo"450+451+Did you mean this?452+ remove453+454+Run 'hugo --help' for usage.455+```456+457 ## Generating markdown formatted documentation for your command458 459 Cobra can generate a markdown formatted document based on the subcommands, flags, etc. A simple example of how to do this for your command can be found in [Markdown Docs](md_docs.md)460 65 unmodified lines