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: 3 additions & 2 deletions suggestions.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,12 @@ func suggestFlag(flags []Flag, provided string, hideHelp bool) string {
}

// suggestCommand takes a list of commands and a provided string to suggest a
// command name
// command name. The help command is one of the commands unless it is hidden,
// and then it must not be suggested, as it does not exist.
func suggestCommand(commands []*Command, provided string) (suggestion string) {
distance := 0.0
for _, command := range commands {
for _, name := range append(command.Names(), helpName, helpAlias) {
for _, name := range command.Names() {
newDistance := jaroWinkler(name, provided)
if newDistance > distance {
distance = newDistance
Expand Down
35 changes: 35 additions & 0 deletions suggestions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,38 @@ func TestSuggestCommand(t *testing.T) {
assert.Equal(t, testCase.expected, res)
}
}

func TestSuggestCommandWithoutHelpCommand(t *testing.T) {
// The help command is not suggested by itself: it is in the list of
// commands unless it is hidden.
commands := []*Command{{Name: "hello"}}

assert.Equal(t, "hello", suggestCommand(commands, "hel"))
assert.Equal(t, "hello", suggestCommand(commands, "h"))
}

func TestPrefixMatchCommandsWithHiddenHelpCommand(t *testing.T) {
for _, arg := range []string{"h", "he", "hel", "hell"} {
t.Run(arg, func(t *testing.T) {
ran := false
cmd := &Command{
Name: "app",
PrefixMatchCommands: true,
HideHelpCommand: true,
Writer: io.Discard,
Commands: []*Command{
{
Name: "hello",
Action: func(context.Context, *Command) error {
ran = true
return nil
},
},
},
}

assert.NoError(t, cmd.Run(buildTestContext(t), []string{"app", arg}))
assert.True(t, ran, "expected the hello command to run")
})
}
}
Loading