From c8d40bf7f5ccfc4773c13bf21f637aceea855767 Mon Sep 17 00:00:00 2001 From: Utkarsh Tiwari Date: Sun, 26 Jul 2026 22:46:07 +0530 Subject: [PATCH] fix: inherit HideHelpCommand in subcommands --- command_setup.go | 13 ++++++++++++- help_test.go | 13 +++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/command_setup.go b/command_setup.go index 646e270ce7..52ecc06278 100644 --- a/command_setup.go +++ b/command_setup.go @@ -227,6 +227,17 @@ func (cmd *Command) hideHelp() bool { return false } +func (cmd *Command) hideHelpCommand() bool { + tracef("hide help command (cmd=%[1]q)", cmd.Name) + for c := cmd; c != nil; c = c.parent { + if c.HideHelpCommand { + return true + } + } + + return false +} + func (cmd *Command) ensureHelp() { tracef("ensuring help (cmd=%[1]q)", cmd.Name) @@ -234,7 +245,7 @@ func (cmd *Command) ensureHelp() { if !cmd.hideHelp() { if cmd.Command(helpCommand.Name) == nil { - if !cmd.HideHelpCommand { + if !cmd.hideHelpCommand() { tracef("appending helpCommand (cmd=%[1]q)", cmd.Name) cmd.appendCommand(helpCommand) } diff --git a/help_test.go b/help_test.go index d3c831371d..a125764853 100644 --- a/help_test.go +++ b/help_test.go @@ -1271,8 +1271,11 @@ func TestHideHelpCommand_WithHideHelp(t *testing.T) { } func TestHideHelpCommand_WithSubcommands(t *testing.T) { + out := &bytes.Buffer{} cmd := &Command{ HideHelpCommand: true, + Writer: out, + ErrWriter: out, Commands: []*Command{ { Name: "nully", @@ -1289,6 +1292,16 @@ func TestHideHelpCommand_WithSubcommands(t *testing.T) { r.ErrorContains(cmd.Run(buildTestContext(t), []string{"cli.test", "help"}), "No help topic for 'help'") r.NoError(cmd.Run(buildTestContext(t), []string{"cli.test", "--help"})) + + out.Reset() + r.ErrorContains(cmd.Run(buildTestContext(t), []string{"cli.test", "nully", "help"}), "No help topic for 'help'") + r.NoError(cmd.Run(buildTestContext(t), []string{"cli.test", "nully", "--help"})) + r.NotContains(out.String(), "help, h") + + out.Reset() + r.ErrorContains(cmd.Run(buildTestContext(t), []string{"cli.test", "nully", "nully2", "help"}), "No help topic for 'help'") + r.NoError(cmd.Run(buildTestContext(t), []string{"cli.test", "nully", "nully2", "--help"})) + r.NotContains(out.String(), "help, h") } func TestDefaultCompleteWithFlags(t *testing.T) {