Skip to content

Commit 7b8baa0

Browse files
committed
fix: stop a user bool -h flag from showing help
The help flag now gives up -h to a user flag that claims it, and help lists -h as that flag. checkHelp still matched the names of the global HelpFlag though, so a user bool -h flag showed help instead of running the action: foo -h # user bool -h flag: help shown, action not run Keep the help flag that ensureHelp adds and check that one, as checkVersion does with the version flag. When a command has no help flag of its own, check a user flag that took its name instead, such as HelpFlag itself listed in Flags or a help flag added with HideHelp, so these still show help as before. With HideHelp and no such flag, checkHelp no longer looks up help and h, so InvalidFlagAccessHandler is no longer called for them. Assisted-by: claude:claude-opus-5-5 Signed-off-by: Eng Zer Jun <[email protected]>
1 parent f8edc15 commit 7b8baa0

3 files changed

Lines changed: 95 additions & 9 deletions

File tree

‎command.go‎

Lines changed: 9 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,13 @@ 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+
helpFlag := cmd.helpFlag
188+
if helpFlag == nil && HelpFlag != nil {
189+
// A user flag that took the name of the help flag, such as
190+
// HelpFlag itself listed in Flags, still shows help.
191+
helpFlag = cmd.lFlag(HelpFlag.Names()[0])
192+
}
193+
return helpFlag != nil && slices.ContainsFunc(helpFlag.Names(), cmd.Bool)
186194
}
187195

188196
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: 85 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4140,19 +4140,30 @@ func TestUserDefinedHelpFlagOverridesBuiltin(t *testing.T) {
41404140
func TestHelpFlagWorksWhenAliasYieldedToUserFlag(t *testing.T) {
41414141
tests := []struct {
41424142
name string
4143+
flag Flag
41434144
args []string
41444145
wantCalled bool
4145-
wantHost string
4146+
wantValue any
41464147
}{
41474148
{
4148-
name: "long help",
4149-
args: []string{"foo", "--help"},
4149+
name: "long help",
4150+
flag: &StringFlag{Name: "h", Usage: "host"},
4151+
args: []string{"foo", "--help"},
4152+
wantValue: "",
41504153
},
41514154
{
41524155
name: "user short flag",
4156+
flag: &StringFlag{Name: "h", Usage: "host"},
41534157
args: []string{"foo", "-h", "example.com"},
41544158
wantCalled: true,
4155-
wantHost: "example.com",
4159+
wantValue: "example.com",
4160+
},
4161+
{
4162+
name: "user short bool flag",
4163+
flag: &BoolFlag{Name: "h", Usage: "human readable"},
4164+
args: []string{"foo", "-h"},
4165+
wantCalled: true,
4166+
wantValue: true,
41564167
},
41574168
}
41584169

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

41644175
cmd := &Command{
41654176
Writer: writer,
4166-
Flags: []Flag{
4167-
&StringFlag{Name: "h", Usage: "host"},
4168-
},
4177+
Flags: []Flag{test.flag},
41694178
Action: func(context.Context, *Command) error {
41704179
called = true
41714180
return nil
@@ -4175,7 +4184,7 @@ func TestHelpFlagWorksWhenAliasYieldedToUserFlag(t *testing.T) {
41754184
err := cmd.Run(buildTestContext(t), test.args)
41764185
require.NoError(t, err)
41774186
require.Equal(t, test.wantCalled, called)
4178-
require.Equal(t, test.wantHost, cmd.String("h"))
4187+
require.Equal(t, test.wantValue, cmd.Value("h"))
41794188
if !test.wantCalled {
41804189
require.Contains(t, writer.String(), "show help")
41814190
require.NotContains(t, writer.String(), "--help, -h")
@@ -4184,6 +4193,74 @@ func TestHelpFlagWorksWhenAliasYieldedToUserFlag(t *testing.T) {
41844193
}
41854194
}
41864195

4196+
// A user flag that takes the name of the help flag, such as HelpFlag
4197+
// itself listed in Flags, still shows help instead of running the action.
4198+
func TestHelpFlagNameTakenByUserFlag(t *testing.T) {
4199+
origHelpFlag := HelpFlag
4200+
t.Cleanup(func() { HelpFlag = origHelpFlag })
4201+
helpFlag := *origHelpFlag.(*BoolFlag)
4202+
HelpFlag = &helpFlag
4203+
4204+
tests := []struct {
4205+
name string
4206+
hideHelp bool
4207+
flag Flag
4208+
args []string
4209+
}{
4210+
{
4211+
name: "HelpFlag listed",
4212+
flag: HelpFlag,
4213+
args: []string{"foo", "--help"},
4214+
},
4215+
{
4216+
name: "HelpFlag listed in subcommand",
4217+
flag: HelpFlag,
4218+
args: []string{"foo", "sub", "--help"},
4219+
},
4220+
{
4221+
name: "HelpFlag listed in subcommand, short",
4222+
flag: HelpFlag,
4223+
args: []string{"foo", "sub", "-h"},
4224+
},
4225+
{
4226+
name: "HideHelp and user help flag",
4227+
hideHelp: true,
4228+
flag: &BoolFlag{Name: "help"},
4229+
args: []string{"foo", "--help"},
4230+
},
4231+
}
4232+
4233+
for _, test := range tests {
4234+
t.Run(test.name, func(t *testing.T) {
4235+
writer := &bytes.Buffer{}
4236+
called := false
4237+
action := func(context.Context, *Command) error {
4238+
called = true
4239+
return nil
4240+
}
4241+
4242+
cmd := &Command{
4243+
Writer: writer,
4244+
HideHelp: test.hideHelp,
4245+
Flags: []Flag{test.flag},
4246+
Action: action,
4247+
Commands: []*Command{
4248+
{
4249+
Name: "sub",
4250+
Flags: []Flag{test.flag},
4251+
Action: action,
4252+
},
4253+
},
4254+
}
4255+
4256+
err := cmd.Run(buildTestContext(t), test.args)
4257+
require.NoError(t, err)
4258+
require.False(t, called, "help should short-circuit the action")
4259+
require.Contains(t, writer.String(), "USAGE:")
4260+
})
4261+
}
4262+
}
4263+
41874264
func TestShorthandCommand(t *testing.T) {
41884265
af := func(p *int) ActionFunc {
41894266
return func(context.Context, *Command) error {

0 commit comments

Comments
 (0)