| 1 | --- |
| 2 | tags: |
| 3 | - v3 |
| 4 | search: |
| 5 | boost: 2 |
| 6 | --- |
| 7 | |
| 8 | The default help flag (`-h/--help`) is defined as `cli.HelpFlag` and is checked |
| 9 | by the cli internals in order to print generated help text for the app, command, |
| 10 | or subcommand, and break execution. |
| 11 | |
| 12 | #### Customization |
| 13 | |
| 14 | All of the help text generation may be customized, and at multiple levels. The templates |
| 15 | are exposed as variables `RootCommandHelpTemplate`, `CommandHelpTemplate`, and |
| 16 | `SubcommandHelpTemplate` which may be reassigned or augmented, and full override is |
| 17 | possible by assigning a compatible func to the `cli.HelpPrinter` variable, e.g.: |
| 18 | |
| 19 | <!-- { |
| 20 | "output": "Ha HA. I pwnd the help!!1" |
| 21 | } --> |
| 22 | ```go |
| 23 | package main |
| 24 | |
| 25 | import ( |
| 26 | "fmt" |
| 27 | "io" |
| 28 | "os" |
| 29 | "context" |
| 30 | |
| 31 | "github.com/urfave/cli/v3" |
| 32 | ) |
| 33 | |
| 34 | func main() { |
| 35 | // EXAMPLE: Append to an existing template |
| 36 | cli.RootCommandHelpTemplate = fmt.Sprintf(`%s |
| 37 | |
| 38 | WEBSITE: http://awesometown.example.com |
| 39 | |
| 40 | SUPPORT: [email protected] |
| 41 | |
| 42 | `, cli.RootCommandHelpTemplate) |
| 43 | |
| 44 | // EXAMPLE: Override a template |
| 45 | cli.RootCommandHelpTemplate = `NAME: |
| 46 | {{.Name}} - {{.Usage}} |
| 47 | USAGE: |
| 48 | {{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}} |
| 49 | {{if len .Authors}} |
| 50 | AUTHOR: |
| 51 | {{range .Authors}}{{ . }}{{end}} |
| 52 | {{end}}{{if .Commands}} |
| 53 | COMMANDS: |
| 54 | {{range .Commands}}{{if not .HideHelp}} {{join .Names ", "}}{{ "\t"}}{{.Usage}}{{ "\n" }}{{end}}{{end}}{{end}}{{if .VisibleFlags}} |
| 55 | GLOBAL OPTIONS: |
| 56 | {{range .VisibleFlags}}{{.}} |
| 57 | {{end}}{{end}}{{if .Copyright }} |
| 58 | COPYRIGHT: |
| 59 | {{.Copyright}} |
| 60 | {{end}}{{if .Version}} |
| 61 | VERSION: |
| 62 | {{.Version}} |
| 63 | {{end}} |
| 64 | ` |
| 65 | |
| 66 | // EXAMPLE: Replace the `HelpPrinter` func |
| 67 | cli.HelpPrinter = func(w io.Writer, templ string, data interface{}) { |
| 68 | fmt.Println("Ha HA. I pwnd the help!!1") |
| 69 | } |
| 70 | |
| 71 | (&cli.Command{}).Run(context.Background(), os.Args) |
| 72 | } |
| 73 | ``` |
| 74 | |
| 75 | The default flag may be customized to something other than `-h/--help` by |
| 76 | setting `cli.HelpFlag`, e.g.: |
| 77 | |
| 78 | <!-- { |
| 79 | "args": ["--halp"], |
| 80 | "output": "haaaaalp.*HALP" |
| 81 | } --> |
| 82 | ```go |
| 83 | package main |
| 84 | |
| 85 | import ( |
| 86 | "os" |
| 87 | "context" |
| 88 | |
| 89 | "github.com/urfave/cli/v3" |
| 90 | ) |
| 91 | |
| 92 | func main() { |
| 93 | cli.HelpFlag = &cli.BoolFlag{ |
| 94 | Name: "haaaaalp", |
| 95 | Aliases: []string{"halp"}, |
| 96 | Usage: "HALP", |
| 97 | Sources: cli.EnvVars("SHOW_HALP", "HALPPLZ"), |
| 98 | } |
| 99 | |
| 100 | (&cli.Command{}).Run(context.Background(), os.Args) |
| 101 | } |
| 102 | ``` |