Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion command_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,25 @@ 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)

helpCommand := buildHelpCommand(true)

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)
}
Expand Down
13 changes: 13 additions & 0 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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) {
Expand Down