Improve suggestions - prefix will match
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 53d9650 · Browse files · 2 files·+42 added−28 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+42 added−28 removed
- other:+42 added−28 removed
Provenance
- Commit
3afa4db- Tree
599d94f- Author
Fabiano Franz <ffranz@redhat.com>- Committer
Fabiano Franz <ffranz@redhat.com>- Parent
53d9650
2 files changed+42 added−28 removed
modified:cobra_test.go+23 added-13 removed
814 unmodified lines815 cmd := initializeWithRootCmd()816 cmd.AddCommand(cmdTimes)817 818 tests := map[string]string{819- "time": "times",820- "tiems": "times",821- "timeS": "times",822- "rimes": "times",819+ "time": "times",820+ "tiems": "times",821+ "tims": "times",822+ "timeS": "times",823+ "rimes": "times",824+ "ti": "times",825+ "t": "times",826+ "timely": "times",827+ "ri": "",828+ "timezone": "",829+ "foo": "",830 }831 832 for typo, suggestion := range tests {826- cmd.DisableSuggestions = false827- result := simpleTester(cmd, typo)828- if expected := fmt.Sprintf(outputWithSuggestions, typo, suggestion); result.Output != expected {829- t.Errorf("Unexpected response.\nExpecting to be:\n %q\nGot:\n %q\n", expected, result.Output)833+ for _, suggestionsDisabled := range []bool{false, true} {834+ cmd.DisableSuggestions = suggestionsDisabled835+ result := simpleTester(cmd, typo)836+ expected := ""837+ if len(suggestion) == 0 || suggestionsDisabled {838+ expected = fmt.Sprintf(outputWithoutSuggestions, typo)839+ } else {840+ expected = fmt.Sprintf(outputWithSuggestions, typo, suggestion)841+ }842+ if result.Output != expected {843+ t.Errorf("Unexpected response.\nExpecting to be:\n %q\nGot:\n %q\n", expected, result.Output)844+ }845 }831- cmd.DisableSuggestions = true832- result = simpleTester(cmd, typo)833- if expected := fmt.Sprintf(outputWithoutSuggestions, typo); result.Output != expected {834- t.Errorf("Unexpected response.\nExpecting to be:\n %q\nGot:\n %q\n", expected, result.Output)835- }846 }847 }848 849 func TestFlagsBeforeCommand(t *testing.T) {207 unmodified linesmodified:command.go+19 added-15 removed
427 unmodified lines428 }429 430 // root command with subcommands, do subcommand checking431 if commandFound == c && len(argsWOflags) > 0 {432- suggestions := ""432+ suggestionsString := ""433 if !c.DisableSuggestions {434 if c.SuggestionsMinimumDistance <= 0 {435 c.SuggestionsMinimumDistance = 2436 }437- similar := []string{}438- for _, cmd := range c.commands {439- if cmd.IsAvailableCommand() {440- levenshtein := ld(argsWOflags[0], cmd.Name(), true)441- if levenshtein <= c.SuggestionsMinimumDistance {442- similar = append(similar, cmd.Name())443- }437+ if suggestions := c.SuggestionsFor(argsWOflags[0]); len(suggestions) > 0 {438+ suggestionsString += "\n\nDid you mean this?\n"439+ for _, s := range suggestions {440+ suggestionsString += fmt.Sprintf("\t%v\n", s)441 }442 }446- if len(similar) > 0 {447- suggestions += "\n\nDid you mean this?\n"448- for _, s := range similar {449- suggestions += fmt.Sprintf("\t%v\n", s)450- }451- }443 }453- return commandFound, a, fmt.Errorf("unknown command %q for %q%s", argsWOflags[0], commandFound.CommandPath(), suggestions)444+ return commandFound, a, fmt.Errorf("unknown command %q for %q%s", argsWOflags[0], commandFound.CommandPath(), suggestionsString)445 }446 447 return commandFound, a, nil448 }449 450+func (c *Command) SuggestionsFor(cmdName string) []string {451+ suggestions := []string{}452+ for _, cmd := range c.commands {453+ 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+ suggestions = append(suggestions, cmd.Name())457+ }458+ }459+ }460+ return suggestions461+}462+463 func (c *Command) Root() *Command {464 var findRoot func(*Command) *Command465 466 findRoot = func(x *Command) *Command {684 unmodified lines