Skip to content

Commit 835fca1

Browse files
authored
Merge pull request #2480 from INo-xious/fix/suggest-hidden-flags
fix: don't suggest hidden flags or commands Reviewed-by: Kai Ase Siren <coilysiren@gmail.com> Reviewed-by: Eng Zer Jun <engzerjun@gmail.com> Tested-by: Eng Zer Jun <engzerjun@gmail.com>
2 parents 05d84c1 + 24bab17 commit 835fca1

4 files changed

Lines changed: 71 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
}

‎suggestions.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ func suggestFlag(flags []Flag, provided string, hideHelp bool) string {
108108

109109
for _, flag := range flags {
110110
flagNames := flag.Names()
111+
// Hidden flags must not be suggested; help is still offered.
112+
if vf, ok := flag.(VisibleFlag); ok && !vf.IsVisible() {
113+
flagNames = nil
114+
}
111115
if !hideHelp && HelpFlag != nil {
112116
flagNames = append(flagNames, HelpFlag.Names()...)
113117
}

‎suggestions_test.go‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cli
22

33
import (
4+
"bytes"
5+
"context"
46
"errors"
57
"fmt"
68
"testing"
@@ -69,6 +71,43 @@ func TestSuggestFlagHideHelp(t *testing.T) {
6971
assert.Equal(t, "--fl", res)
7072
}
7173

74+
func TestSuggestFlagHiddenFlag(t *testing.T) {
75+
// Given
76+
flags := []Flag{
77+
&BoolFlag{Name: "internal-debug", Hidden: true},
78+
&BoolFlag{Name: "verbose"},
79+
}
80+
81+
// When
82+
res := suggestFlag(flags, "internal-debu", true)
83+
84+
// Then
85+
assert.Equal(t, "--verbose", res)
86+
}
87+
88+
func TestSuggestFlagHiddenFlagFromRun(t *testing.T) {
89+
// Given
90+
var out bytes.Buffer
91+
cmd := &Command{
92+
Name: "app",
93+
Suggest: true,
94+
Writer: &out,
95+
ErrWriter: &out,
96+
Flags: []Flag{
97+
&BoolFlag{Name: "internal-debug", Hidden: true},
98+
&BoolFlag{Name: "verbose"},
99+
},
100+
Action: func(context.Context, *Command) error { return nil },
101+
}
102+
103+
// When
104+
err := cmd.Run(buildTestContext(t), []string{"app", "--internal-debu"})
105+
106+
// Then
107+
assert.Error(t, err)
108+
assert.NotContains(t, out.String(), "internal-debug")
109+
}
110+
72111
func TestSuggestFlagFromError(t *testing.T) {
73112
// Given
74113
app := buildExtendedTestCommand()

0 commit comments

Comments
 (0)