Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug/fix issue 1703 #1728

Merged
merged 2 commits into from
May 1, 2023
Merged
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
11 changes: 9 additions & 2 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var helpCommand = &Command{
// 3 $ app foo
// 4 $ app help foo
// 5 $ app foo help
// 6 $ app foo -h (with no other sub-commands nor flags defined)

// Case 4. when executing a help command set the context to parent
// to allow resolution of subsequent args. This will transform
Expand Down Expand Up @@ -77,6 +78,8 @@ var helpCommand = &Command{
HelpPrinter(cCtx.App.Writer, templ, cCtx.Command)
return nil
}

// Case 6, handling incorporated in the callee itself
return ShowSubcommandHelp(cCtx)
},
}
Expand Down Expand Up @@ -292,8 +295,12 @@ func ShowSubcommandHelp(cCtx *Context) error {
if cCtx == nil {
return nil
}

HelpPrinter(cCtx.App.Writer, SubcommandHelpTemplate, cCtx.Command)
// use custom template when provided (fixes #1703)
templ := SubcommandHelpTemplate
if cCtx.Command != nil && cCtx.Command.CustomHelpTemplate != "" {
templ = cCtx.Command.CustomHelpTemplate
}
HelpPrinter(cCtx.App.Writer, templ, cCtx.Command)
return nil
}

Expand Down
23 changes: 23 additions & 0 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,29 @@ func TestHideHelpCommand_WithSubcommands(t *testing.T) {
}
}

func TestHideHelpCommand_RunAsSubcommand_True_CustomTemplate(t *testing.T) {
var buf bytes.Buffer

app := &App{
Writer: &buf,
Commands: []*Command{
{
Name: "dummy",
CustomHelpTemplate: "TEMPLATE",
HideHelpCommand: true,
},
},
}

err := app.RunAsSubcommand(newContextFromStringSlice([]string{"", "dummy", "-h"}))
if err != nil {
t.Errorf("Run returned unexpected error: %v", err)
}
if !strings.Contains(buf.String(), "TEMPLATE") {
t.Errorf("Custom Help template ignored")
}
}

func TestDefaultCompleteWithFlags(t *testing.T) {
origEnv := os.Environ()
origArgv := os.Args
Expand Down