Skip to content

Commit 5c945f0

Browse files
committed
fix: keep --help when a user flag claims -h
The previous commit skips the help flag once a user flag takes any of its names, so a user -h flag, such as -h for host, also removed --help: foo --help # flag provided but not defined: -help Drop the aliases that user flags already claim before that check, as the version flag does since #2229. A user -h flag now wins and --help still shows help. A user flag named help still replaces the help flag. Assisted-by: claude:claude-opus-5-5 Signed-off-by: Eng Zer Jun <[email protected]>
1 parent 44743e2 commit 5c945f0

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

‎command_setup.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ func (cmd *Command) ensureHelp() {
245245
var localHelpFlag Flag
246246
if globalHelpFlag, ok := HelpFlag.(*BoolFlag); ok {
247247
flag := *globalHelpFlag
248+
// Drop any alias a user flag already claims (e.g. -h
249+
// for --host) so the user flag wins but --help still
250+
// works, as for the version flag.
251+
flag.Aliases = dropClashingAliases(flag.Aliases, cmd.allFlags(), flag.Name)
248252
localHelpFlag = &flag
249253
} else {
250254
localHelpFlag = HelpFlag

‎command_test.go‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4052,6 +4052,55 @@ func TestUserDefinedHelpFlagOverridesBuiltin(t *testing.T) {
40524052
require.NotContains(t, writer.String(), "--help, -h show help")
40534053
}
40544054

4055+
// When a user flag claims the -h alias, only that alias is yielded: -h
4056+
// goes to the user flag and --help must still show help.
4057+
func TestHelpFlagWorksWhenAliasYieldedToUserFlag(t *testing.T) {
4058+
tests := []struct {
4059+
name string
4060+
args []string
4061+
wantCalled bool
4062+
wantHost string
4063+
}{
4064+
{
4065+
name: "long help",
4066+
args: []string{"foo", "--help"},
4067+
},
4068+
{
4069+
name: "user short flag",
4070+
args: []string{"foo", "-h", "example.com"},
4071+
wantCalled: true,
4072+
wantHost: "example.com",
4073+
},
4074+
}
4075+
4076+
for _, test := range tests {
4077+
t.Run(test.name, func(t *testing.T) {
4078+
writer := &bytes.Buffer{}
4079+
called := false
4080+
4081+
cmd := &Command{
4082+
Writer: writer,
4083+
Flags: []Flag{
4084+
&StringFlag{Name: "h", Usage: "host"},
4085+
},
4086+
Action: func(context.Context, *Command) error {
4087+
called = true
4088+
return nil
4089+
},
4090+
}
4091+
4092+
err := cmd.Run(buildTestContext(t), test.args)
4093+
require.NoError(t, err)
4094+
require.Equal(t, test.wantCalled, called)
4095+
require.Equal(t, test.wantHost, cmd.String("h"))
4096+
if !test.wantCalled {
4097+
require.Contains(t, writer.String(), "show help")
4098+
require.NotContains(t, writer.String(), "--help, -h")
4099+
}
4100+
})
4101+
}
4102+
}
4103+
40554104
func TestShorthandCommand(t *testing.T) {
40564105
af := func(p *int) ActionFunc {
40574106
return func(context.Context, *Command) error {

0 commit comments

Comments
 (0)