From be4af127dc52931c145cb439a06aeafb1bb29452 Mon Sep 17 00:00:00 2001 From: Adrian Thurston Date: Wed, 25 Jun 2025 10:34:14 -0700 Subject: [PATCH 1/3] fix: use the custom help template in ShowSubcommand The custom help template was not getting used in subcommand help. This is called when using HideHelpCommand. Updated ShowSubcommand to use the custom template. It's possible a minor refactor in the help display cases is needed here, but this change fixes our immediate problem. --- help.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/help.go b/help.go index c039a5d02b..30ecc55884 100644 --- a/help.go +++ b/help.go @@ -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 } From 5005723859545212679072dc78264c52e156cb87 Mon Sep 17 00:00:00 2001 From: Adrian Thurston Date: Wed, 25 Jun 2025 11:11:44 -0700 Subject: [PATCH 2/3] test: added a test case for custom help in subcommand with hidden help command --- help_test.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/help_test.go b/help_test.go index 1decf8899a..6d9024e5c9 100644 --- a/help_test.go +++ b/help_test.go @@ -633,6 +633,68 @@ 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{ From 49b126a11b6ef02a0011d25a0a3f4697816dbf7d Mon Sep 17 00:00:00 2001 From: Adrian Thurston Date: Wed, 25 Jun 2025 11:24:15 -0700 Subject: [PATCH 3/3] fix: test case formatting --- help_test.go | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/help_test.go b/help_test.go index 6d9024e5c9..29ec4b347f 100644 --- a/help_test.go +++ b/help_test.go @@ -634,17 +634,16 @@ EXAMPLES: } 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: + cmd := &Command{ + Name: "parent", + Commands: []*Command{ + { + Name: "child", + Action: func(context.Context, *Command) error { + return nil + }, + HideHelpCommand: true, + CustomHelpTemplate: `NAME: {{.FullName}} - {{.Usage}} USAGE: @@ -657,9 +656,9 @@ CUSTOM SECTION: This is a custom help template for a subcommand. Parent command: {{with .Parent}}{{.Name}}{{end}} `, - }, }, - } + }, + } output := &bytes.Buffer{} cmd.Writer = output