Debian Codegraph @

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

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