Debian Codegraph @

docs/v2/examples/generated-help-text.md

raw symbols SCIP
1 ---
2 tags:
3 - v2
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
15 templates are exposed as variables `AppHelpTemplate`, `CommandHelpTemplate`, and
16 `SubcommandHelpTemplate` which may be reassigned or augmented, and full override
17 is possible by assigning a compatible func to the `cli.HelpPrinter` variable,
18 e.g.:
19
20 <!-- {
21 "output": "Ha HA. I pwnd the help!!1"
22 } -->
23 ```go
24 package main
25
26 import (
27 "fmt"
28 "io"
29 "os"
30
31 "github.com/urfave/cli/v2"
32 )
33
34 func main() {
35 // EXAMPLE: Append to an existing template
36 cli.AppHelpTemplate = fmt.Sprintf(`%s
37
38 WEBSITE: http://awesometown.example.com
39
40 SUPPORT: [email protected]
41
42 `, cli.AppHelpTemplate)
43
44 // EXAMPLE: Override a template
45 cli.AppHelpTemplate = `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.App{}).Run(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": ["&#45;&#45halp"],
80 "output": "haaaaalp.*HALP"
81 } -->
82 ```go
83 package main
84
85 import (
86 "os"
87
88 "github.com/urfave/cli/v2"
89 )
90
91 func main() {
92 cli.HelpFlag = &cli.BoolFlag{
93 Name: "haaaaalp",
94 Aliases: []string{"halp"},
95 Usage: "HALP",
96 EnvVars: []string{"SHOW_HALP", "HALPPLZ"},
97 }
98
99 (&cli.App{}).Run(os.Args)
100 }
101 ```