-
Notifications
You must be signed in to change notification settings - Fork 60
Fix unknown command suggestions #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 "" | ||
|
Comment on lines
+117
to
+118
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
With the actual root command list, an unrelated invocation such as Useful? React with 👍 / 👎. |
||
| } | ||
|
|
||
| var parts []string | ||
| for _, command := range lineage { | ||
|
|
||
| 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) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the real
runsubcommand declared with suggestions enabled inpkg/cmd/finetuningalphagrader.go:17-20, omitting the middle character producesjaroWinkler("run", "rn") == 0.611, so this fixed threshold returns no suggestion even though the input is only one deletion away. The new filter therefore suppresses a close typo that the change is intended to preserve; the cutoff should account for short command lengths or use an edit-distance fallback.Useful? React with 👍 / 👎.