Skip to content

Commit 65ded98

Browse files
authored
Merge pull request #2269 from TimSoethout/main
fix(help): show GLOBAL OPTIONS for leaf subcommands when HideHelpCommand is true
2 parents 600026f + f5d0884 commit 65ded98

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

help.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,7 @@ func helpCommandAction(ctx context.Context, cmd *Command) error {
124124
}
125125

126126
// Case 3, 5
127-
if (len(cmd.Commands) == 1 && !cmd.HideHelp) ||
128-
(len(cmd.Commands) == 0 && cmd.HideHelp) {
127+
if len(cmd.VisibleCommands()) == 0 {
129128

130129
tmpl := cmd.CustomHelpTemplate
131130
if tmpl == "" {

help_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,52 @@ GLOBAL OPTIONS:
797797
assert.Contains(t, output.String(), expected, "expected output to include global options")
798798
}
799799

800+
func TestShowSubcommandHelp_GlobalOptions_HideHelpCommand(t *testing.T) {
801+
cmd := &Command{
802+
Flags: []Flag{
803+
&StringFlag{
804+
Name: "foo",
805+
},
806+
},
807+
Commands: []*Command{
808+
{
809+
Name: "frobbly",
810+
HideHelpCommand: true,
811+
Flags: []Flag{
812+
&StringFlag{
813+
Name: "bar",
814+
Local: true,
815+
},
816+
},
817+
Action: func(context.Context, *Command) error {
818+
return nil
819+
},
820+
},
821+
},
822+
}
823+
824+
output := &bytes.Buffer{}
825+
cmd.Writer = output
826+
827+
_ = cmd.Run(buildTestContext(t), []string{"foo", "frobbly", "--help"})
828+
829+
expected := `NAME:
830+
foo frobbly
831+
832+
USAGE:
833+
foo frobbly [options]
834+
835+
OPTIONS:
836+
--bar string
837+
--help, -h show help
838+
839+
GLOBAL OPTIONS:
840+
--foo string
841+
`
842+
843+
assert.Contains(t, output.String(), expected, "expected output to include global options")
844+
}
845+
800846
func TestShowSubcommandHelp_SubcommandUsageText(t *testing.T) {
801847
cmd := &Command{
802848
Commands: []*Command{

0 commit comments

Comments
 (0)