Skip to content
Myr

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

3 files+29 added−6 removed

modified:cobra_test.go+5 added-3 removed
80 unmodified lines
81 },82}8384var 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 lines
827 "timely": "times",828 "ri": "",829 "timezone": "",830 "foo": "",831 "counts": "times",832 }833834 for typo, suggestion := range tests {835 for _, suggestionsDisabled := range []bool{false, true} {
223 unmodified lines
modified:command.go+12 added-3 removed
37 unmodified lines
38 // 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 string
400 unmodified lines
448449 return commandFound, a, nil450}451450func (c *Command) SuggestionsFor(cmdName string) []string {452func (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 lines
modified:README.md+12 added
440 unmodified lines
441or 442443 command.SuggestionsMinimumDistance = 1444445You 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:446447```448$ hugo delete449unknown command "delete" for "hugo"450451Did you mean this?452 remove453454Run 'hugo --help' for usage.455```456457## Generating markdown formatted documentation for your command458459Cobra 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