diff --git a/pkg/cmd/suggest.go b/pkg/cmd/suggest.go index b4b637c..00a8269 100644 --- a/pkg/cmd/suggest.go +++ b/pkg/cmd/suggest.go @@ -101,6 +101,8 @@ func jaroWinkler(a, b string) float64 { // suggestCommand takes a list of commands and a provided string to suggest a // command name func suggestCommand(commands []*cli.Command, provided string) string { + const minSuggestionDistance = 0.75 + distance := 0.0 var lineage []*cli.Command for _, command := range commands { @@ -112,6 +114,9 @@ func suggestCommand(commands []*cli.Command, provided string) string { } } } + if distance < minSuggestionDistance || len(lineage) == 0 { + return "" + } var parts []string for _, command := range lineage { diff --git a/pkg/cmd/suggest_test.go b/pkg/cmd/suggest_test.go new file mode 100644 index 0000000..f210f2a --- /dev/null +++ b/pkg/cmd/suggest_test.go @@ -0,0 +1,29 @@ +package cmd + +import ( + "testing" + + "github.com/urfave/cli/v3" +) + +func TestSuggestCommandSkipsUnrelatedInput(t *testing.T) { + commands := []*cli.Command{ + {Name: "completions"}, + {Name: "responses"}, + } + + if got := suggestCommand(commands, "totallybogus"); got != "" { + t.Fatalf("expected no suggestion for unrelated input, got %q", got) + } +} + +func TestSuggestCommandKeepsCloseMatches(t *testing.T) { + commands := []*cli.Command{ + {Name: "create"}, + {Name: "delete"}, + } + + if got := suggestCommand(commands, "creat"); got != "Did you mean 'create'?" { + t.Fatalf("expected create suggestion, got %q", got) + } +}