Skip to content

Commit 4ddd242

Browse files
committed
fix: let a user flag named help reach the action
checkHelp looked up the names of the global HelpFlag, so it also matched user flags that took those names. A user flag named help, which #2174 asks to be able to call, still showed the built-in help instead of running the action, and so did a user bool -h flag: foo --help # user help flag: built-in help, action not run foo -h # user bool -h flag: built-in help, action not run Keep the help flag that ensureHelp adds and check only that one, as checkVersion does with the version flag. When a user flag takes help or -h, the action now runs with that flag set. Assisted-by: claude:claude-opus-5-5 Signed-off-by: Eng Zer Jun <[email protected]>
1 parent f8edc15 commit 4ddd242

3 files changed

Lines changed: 25 additions & 11 deletions

File tree

‎command.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ type Command struct {
163163
globaVersionFlagAdded bool
164164
// generated root version flag
165165
versionFlag Flag
166+
// generated help flag
167+
helpFlag Flag
166168
// whether this is a completion command
167169
isCompletionCommand bool
168170
// whether this is the built-in help command
@@ -182,7 +184,7 @@ func (cmd *Command) Command(name string) *Command {
182184
func (cmd *Command) checkHelp() bool {
183185
tracef("checking if help is wanted (cmd=%[1]q)", cmd.Name)
184186

185-
return HelpFlag != nil && slices.ContainsFunc(HelpFlag.Names(), cmd.Bool)
187+
return cmd.helpFlag != nil && slices.ContainsFunc(cmd.helpFlag.Names(), cmd.Bool)
186188
}
187189

188190
func (cmd *Command) checkDuplicateFlagNames() error {

‎command_setup.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ func (cmd *Command) ensureHelp() {
257257
if !flagNamesInUse(cmd.allFlags(), localHelpFlag.Names()) {
258258
tracef("appending HelpFlag (cmd=%[1]q)", cmd.Name)
259259
cmd.appendFlag(localHelpFlag)
260+
cmd.helpFlag = localHelpFlag
260261
cmd.globaHelpFlagAdded = true
261262
}
262263
} else {

‎command_test.go‎

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4110,19 +4110,23 @@ func TestDuplicateFlagNamesSkipExtFlags(t *testing.T) {
41104110

41114111
func TestUserDefinedHelpFlagOverridesBuiltin(t *testing.T) {
41124112
writer := &bytes.Buffer{}
4113+
called := false
41134114
cmd := &Command{
41144115
Writer: writer,
41154116
Flags: []Flag{
41164117
&BoolFlag{Name: "help", Usage: "custom help behavior"},
41174118
},
41184119
Action: func(context.Context, *Command) error {
4120+
called = true
41194121
return nil
41204122
},
41214123
}
41224124

41234125
err := cmd.Run(buildTestContext(t), []string{"foo", "--help"})
41244126
require.NoError(t, err)
4127+
require.True(t, called, "the user flag should reach the action")
41254128
require.True(t, cmd.Bool("help"))
4129+
require.Empty(t, writer.String(), "the built-in help should not be shown")
41264130

41274131
var helpFlags int
41284132
for _, fl := range cmd.Flags {
@@ -4131,28 +4135,37 @@ func TestUserDefinedHelpFlagOverridesBuiltin(t *testing.T) {
41314135
}
41324136
}
41334137
require.Equal(t, 1, helpFlags)
4134-
require.Contains(t, writer.String(), "--help custom help behavior")
4135-
require.NotContains(t, writer.String(), "--help, -h show help")
41364138
}
41374139

41384140
// When a user flag claims the -h alias, only that alias is yielded: -h
41394141
// goes to the user flag and --help must still show help.
41404142
func TestHelpFlagWorksWhenAliasYieldedToUserFlag(t *testing.T) {
41414143
tests := []struct {
41424144
name string
4145+
flag Flag
41434146
args []string
41444147
wantCalled bool
4145-
wantHost string
4148+
wantValue any
41464149
}{
41474150
{
4148-
name: "long help",
4149-
args: []string{"foo", "--help"},
4151+
name: "long help",
4152+
flag: &StringFlag{Name: "h", Usage: "host"},
4153+
args: []string{"foo", "--help"},
4154+
wantValue: "",
41504155
},
41514156
{
41524157
name: "user short flag",
4158+
flag: &StringFlag{Name: "h", Usage: "host"},
41534159
args: []string{"foo", "-h", "example.com"},
41544160
wantCalled: true,
4155-
wantHost: "example.com",
4161+
wantValue: "example.com",
4162+
},
4163+
{
4164+
name: "user short bool flag",
4165+
flag: &BoolFlag{Name: "h", Usage: "human readable"},
4166+
args: []string{"foo", "-h"},
4167+
wantCalled: true,
4168+
wantValue: true,
41564169
},
41574170
}
41584171

@@ -4163,9 +4176,7 @@ func TestHelpFlagWorksWhenAliasYieldedToUserFlag(t *testing.T) {
41634176

41644177
cmd := &Command{
41654178
Writer: writer,
4166-
Flags: []Flag{
4167-
&StringFlag{Name: "h", Usage: "host"},
4168-
},
4179+
Flags: []Flag{test.flag},
41694180
Action: func(context.Context, *Command) error {
41704181
called = true
41714182
return nil
@@ -4175,7 +4186,7 @@ func TestHelpFlagWorksWhenAliasYieldedToUserFlag(t *testing.T) {
41754186
err := cmd.Run(buildTestContext(t), test.args)
41764187
require.NoError(t, err)
41774188
require.Equal(t, test.wantCalled, called)
4178-
require.Equal(t, test.wantHost, cmd.String("h"))
4189+
require.Equal(t, test.wantValue, cmd.Value("h"))
41794190
if !test.wantCalled {
41804191
require.Contains(t, writer.String(), "show help")
41814192
require.NotContains(t, writer.String(), "--help, -h")

0 commit comments

Comments
 (0)