Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pkg/cmd/suggest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
29 changes: 29 additions & 0 deletions pkg/cmd/suggest_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}