Skip to content

Commit

Permalink
Merge pull request #1728 from jojje/bug/fix-issue-1703
Browse files Browse the repository at this point in the history
Bug/fix issue 1703
  • Loading branch information
dearchap committed May 1, 2023
2 parents c69f466 + 3541e28 commit dc77f3c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
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

0 comments on commit dc77f3c

Please sign in to comment.