Generated Help Text
The default help flag (-h/--help
) is defined as cli.HelpFlag
and is checked
by the cli internals in order to print generated help text for the app, command,
or subcommand, and break execution.
Customization¶
All of the help text generation may be customized, and at multiple levels. The
templates are exposed as variables AppHelpTemplate
, CommandHelpTemplate
, and
SubcommandHelpTemplate
which may be reassigned or augmented, and full override
is possible by assigning a compatible func to the cli.HelpPrinter
variable,
e.g.:
package main
import (
"fmt"
"log"
"io"
"os"
"github.com/urfave/cli"
)
func main() {
// EXAMPLE: Append to an existing template
cli.AppHelpTemplate = fmt.Sprintf(`%s
WEBSITE: http://awesometown.example.com
SUPPORT: support@awesometown.example.com
`, cli.AppHelpTemplate)
// EXAMPLE: Override a template
cli.AppHelpTemplate = `NAME:
{{.Name}} - {{.Usage}}
USAGE:
{{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}
{{if len .Authors}}
AUTHOR:
{{range .Authors}}{{ . }}{{end}}
{{end}}{{if .Commands}}
COMMANDS:
{{range .Commands}}{{if not .HideHelp}} {{join .Names ", "}}{{ "\t"}}{{.Usage}}{{ "\n" }}{{end}}{{end}}{{end}}{{if .VisibleFlags}}
GLOBAL OPTIONS:
{{range .VisibleFlags}}{{.}}
{{end}}{{end}}{{if .Copyright }}
COPYRIGHT:
{{.Copyright}}
{{end}}{{if .Version}}
VERSION:
{{.Version}}
{{end}}
`
// EXAMPLE: Replace the `HelpPrinter` func
cli.HelpPrinter = func(w io.Writer, templ string, data interface{}) {
fmt.Println("Ha HA. I pwnd the help!!1")
}
err := cli.NewApp().Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
The default flag may be customized to something other than -h/--help
by
setting cli.HelpFlag
, e.g.:
package main
import (
"log"
"os"
"github.com/urfave/cli"
)
func main() {
cli.HelpFlag = cli.BoolFlag{
Name: "halp, haaaaalp",
Usage: "HALP",
EnvVar: "SHOW_HALP,HALPPLZ",
}
err := cli.NewApp().Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
Last update:
August 15, 2022