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

Simplify HelpPrinter and CustomHelpPrinter behaviors #912

Merged
merged 5 commits into from
Oct 21, 2019
Merged
Changes from 3 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
28 changes: 16 additions & 12 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ type helpPrinter func(w io.Writer, templ string, data interface{})
// Prints help for the App or Command with custom template function.
type helpPrinterCustom func(w io.Writer, templ string, data interface{}, customFunc map[string]interface{})

// HelpPrinter is a function that writes the help output. If not set a default
// is used. The function signature is:
// func(w io.Writer, templ string, data interface{})
// HelpPrinter is a function that writes the help output. If not set explicitly,
// this calls HelpPrinterCustom using only the default template functions.
var HelpPrinter helpPrinter = printHelp

// HelpPrinterCustom is same as HelpPrinter but
// takes a custom function for template function map.
// HelpPrinterCustom is a function that writes the help output. If not set
// explicitly, a default is used.
//
// The customFuncs map will be combined with a default template.FuncMap to
// allow using arbitrary functions in template rendering.
var HelpPrinterCustom helpPrinterCustom = printHelpCustom

// VersionPrinter prints the version for the App
Expand Down Expand Up @@ -186,11 +188,13 @@ func ShowCommandHelp(ctx *Context, command string) error {

for _, c := range ctx.App.Commands {
if c.HasName(command) {
if c.CustomHelpTemplate != "" {
HelpPrinterCustom(ctx.App.Writer, c.CustomHelpTemplate, c, nil)
} else {
HelpPrinter(ctx.App.Writer, CommandHelpTemplate, c)
templ := c.CustomHelpTemplate
if templ == "" {
templ = CommandHelpTemplate
}

HelpPrinter(ctx.App.Writer, templ, c)

return nil
}
}
Expand Down Expand Up @@ -238,11 +242,11 @@ func ShowCommandCompletions(ctx *Context, command string) {

}

func printHelpCustom(out io.Writer, templ string, data interface{}, customFunc map[string]interface{}) {
func printHelpCustom(out io.Writer, templ string, data interface{}, customFuncs map[string]interface{}) {
funcMap := template.FuncMap{
"join": strings.Join,
}
for key, value := range customFunc {
for key, value := range customFuncs {
funcMap[key] = value
}

Expand All @@ -261,7 +265,7 @@ func printHelpCustom(out io.Writer, templ string, data interface{}, customFunc m
}

func printHelp(out io.Writer, templ string, data interface{}) {
printHelpCustom(out, templ, data, nil)
HelpPrinterCustom(out, templ, data, nil)
}

func checkVersion(c *Context) bool {
Expand Down