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
6 changes: 5 additions & 1 deletion help.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,11 @@ func ShowSubcommandHelpAndExit(cmd *Command, exitCode int) {

// ShowSubcommandHelp prints help for the given subcommand
func ShowSubcommandHelp(cmd *Command) error {
HelpPrinter(cmd.Root().Writer, SubcommandHelpTemplate, cmd)
tmpl := cmd.CustomHelpTemplate
if tmpl == "" {
tmpl = SubcommandHelpTemplate
}
HelpPrinter(cmd.Root().Writer, tmpl, cmd)
return nil
}

Expand Down
61 changes: 61 additions & 0 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,67 @@ EXAMPLES:
"expected output to include \"$ foo frobbly wobbly\"")
}

func TestShowCommandHelp_CustomtemplateWithSubcommandsHideHelpCommand(t *testing.T) {
cmd := &Command{
Name: "parent",
Commands: []*Command{
{
Name: "child",
Action: func(context.Context, *Command) error {
return nil
},
HideHelpCommand: true,
CustomHelpTemplate: `NAME:
{{.FullName}} - {{.Usage}}

USAGE:
{{.FullName}} [FLAGS] TARGET [TARGET ...]

FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
CUSTOM SECTION:
This is a custom help template for a subcommand.
Parent command: {{with .Parent}}{{.Name}}{{end}}
`,
},
},
}
output := &bytes.Buffer{}
cmd.Writer = output

// Test both ways of showing help
testCases := []struct {
name string
args []string
}{
{
name: "using --help flag",
args: []string{"parent", "child", "--help"},
},
{
name: "using help command",
args: []string{"parent", "help", "child"},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
output.Reset()
_ = cmd.Run(buildTestContext(t), tc.args)

assert.Contains(t, output.String(), "This is a custom help template for a subcommand.",
"expected output to include custom section")

assert.Contains(t, output.String(), "CUSTOM SECTION:",
"expected output to include custom section header")

assert.Contains(t, output.String(), "parent child",
"expected output to include full command path")
})
}
}

func TestShowSubcommandHelp_CommandUsageText(t *testing.T) {
cmd := &Command{
Commands: []*Command{
Expand Down