Skip to content

Commit 24bab17

Browse files
committed
fix: don't suggest hidden commands for unknown help topics
With Suggest enabled, "app help <name>" with an unknown name appends the closest command name to the "No help topic" error, including commands declared Hidden. With a hidden "putz" command, "app help put" fails with: No help topic for 'put'. putz Hidden commands are already left out of help output and shell completion, so leave them out of this suggestion too. Filter them at this call site instead of in suggestCommand, because PrefixMatchCommands also resolves command names through suggestCommand. Fixes: 002bde2 ("Add suggestions support") Assisted-by: claude:claude-opus-5-5 Signed-off-by: Eng Zer Jun <[email protected]>
1 parent 3d526cf commit 24bab17

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

‎help.go‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,14 @@ func DefaultShowCommandHelp(ctx context.Context, cmd *Command, commandName strin
339339
errMsg := fmt.Sprintf("No help topic for '%v'", commandName)
340340

341341
if cmd.Suggest {
342-
if suggestion := SuggestCommand(cmd.Commands, commandName); suggestion != "" {
342+
// Hidden commands must not be suggested, as in shell completion.
343+
var commands []*Command
344+
for _, subCmd := range cmd.Commands {
345+
if !subCmd.Hidden {
346+
commands = append(commands, subCmd)
347+
}
348+
}
349+
if suggestion := SuggestCommand(commands, commandName); suggestion != "" {
343350
errMsg += ". " + suggestion
344351
}
345352
}

‎help_test.go‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2379,6 +2379,26 @@ func TestCommandHelpSuggest(t *testing.T) {
23792379
assert.ErrorContains(t, err, "No help topic for 'put'. putz")
23802380
}
23812381

2382+
func TestCommandHelpSuggestHiddenCommand(t *testing.T) {
2383+
cmd := &Command{
2384+
Suggest: true,
2385+
Commands: []*Command{
2386+
{
2387+
Name: "putz",
2388+
Hidden: true,
2389+
},
2390+
{
2391+
Name: "pull",
2392+
},
2393+
},
2394+
}
2395+
2396+
cmd.setupDefaults([]string{"foo"})
2397+
2398+
err := ShowCommandHelp(context.Background(), cmd, "put")
2399+
assert.EqualError(t, err, "No help topic for 'put'. pull")
2400+
}
2401+
23822402
func TestWrapLine(t *testing.T) {
23832403
assert.Equal(t, " ", wrapLine(" ", 0, 3, " "))
23842404
}

0 commit comments

Comments
 (0)