Debian Codegraph @

usage/usage.go

raw symbols SCIP
1 package usage
2
3 import (
4 "bytes"
5 "fmt"
6 "html"
7 "strconv"
8 "strings"
9 "text/template"
10 )
11
12 var usageTextTempl = " {{.Name}}\n {{.Usage}} {{if .Required}}(Required){{else}}(Optional){{end}}{{if .Multiple}} (Multiple can be specified){{end}}\n"
13 var templ *template.Template
14
15 func init() {
16 templ = template.Must(template.New("usageText").Parse(usageTextTempl))
17 }
18
19 // Argument specifies the Name, Usage, and whether or not an Argument is
20 // required or not
21 type Argument struct {
22 Required bool
23 Multiple bool
24 Name string
25 Usage string
26 }
27
28 // Decorate returns the name of an Argument and decorates it with notation to
29 // indicate whether its required or not
30 func (a Argument) Decorate() string {
31 name := a.Name
32 if a.Multiple {
33 name = name + "(s)..."
34 }
35 if a.Required {
36 return fmt.Sprintf("<%s>", name)
37 }
38
39 return fmt.Sprintf("[%s]", name)
40 }
41
42 // Arguments is an array of Argument structs that specify which arguments are
43 // accepted by a Command
44 type Arguments []Argument
45
46 // UsageText returns the value of the UsageText property for a cli.Command for
47 // these arguments
48 func (args Arguments) UsageText() string {
49 var buf bytes.Buffer
50 for _, a := range args {
51 data := map[string]interface{}{
52 "Name": a.Decorate(),
53 "Multiple": a.Multiple,
54 "Required": a.Required,
55 "Usage": a.Usage,
56 }
57
58 err := templ.Execute(&buf, data)
59 if err != nil {
60 panic(fmt.Sprintf("Could not generate args template for %s: %s", a.Name, err))
61 }
62 }
63
64 return "\n\n" + buf.String()
65 }
66
67 // ArgsUsage returns the value of the ArgsUsage property for a cli.Command for
68 // these arguments
69 func (args Arguments) ArgsUsage() string {
70 out := ""
71 for i, a := range args {
72 out += a.Decorate()
73 if i < len(args)-1 {
74 out += " "
75 }
76 }
77
78 return out
79 }
80
81 // AppHelpTemplate contains the modified template for the main app
82 var AppHelpTemplate = `## NAME
83 **{{.HelpName}}** -- {{.Usage}}
84
85 ## USAGE
86 {{if .UsageText}}{{.UsageText}}{{else}}**{{.HelpName}}**{{if .Commands}} <command>{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}_[arguments]_{{end}}{{end}}{{if .Description}}
87
88 ## DESCRIPTION
89 {{.Description}}{{end}}{{if .VisibleCommands}}
90
91 ## COMMANDS
92
93 {{range .VisibleCategories}}{{if .Name}}{{.Name}}:{{end}}
94 |||
95 |---|---|{{range .VisibleCommands}}
96 | **{{join .Names ", "}}** | {{.Usage}} |{{end}}
97 {{end}}{{if .VisibleFlags}}{{end}}
98
99 ## OPTIONS
100
101 {{range $index, $option := .VisibleFlags}}{{if $index}}
102 {{end}}{{$option}}
103 {{end}}{{end}}{{if .Copyright}}{{if len .Authors}}
104
105 ## AUTHOR{{with $length := len .Authors}}{{if ne 1 $length}}S{{end}}{{end}}:
106
107 {{range $index, $author := .Authors}}{{if $index}}
108 {{end}}{{$author}}{{end}}{{end}}{{if .Version}}{{if not .HideVersion}}
109
110 ## ONLINE
111
112 This documentation is available online at https://smallstep.com/docs/cli
113
114 ## VERSION
115
116 {{.Version}}{{end}}{{end}}
117
118 ## COPYRIGHT
119
120 {{.Copyright}}
121
122 ## FEEDBACK ` +
123 html.UnescapeString("&#"+strconv.Itoa(128525)+";") + " " +
124 html.UnescapeString("&#"+strconv.Itoa(127867)+";") +
125 `
126
127 The **step** utility is not instrumented for usage statistics. It does not phone home.
128 But your feedback is extremely valuable. Any information you can provide regarding how you’re using **step** helps.
129 Please send us a sentence or two, good or bad: **[email protected]** or ask in [GitHub Discussions](https://github.com/smallstep/certificates/discussions).
130 {{end}}
131 `
132
133 // SubcommandHelpTemplate contains the modified template for a sub command
134 // Note that the weird "|||\n|---|---|" syntax sets up a markdown table with empty headers.
135 var SubcommandHelpTemplate = `## NAME
136 **{{.HelpName}}** -- {{.Usage}}
137
138 ## USAGE
139
140 {{if .UsageText}}{{.UsageText}}{{else}}**{{.HelpName}}** <command>{{if .VisibleFlags}} _[options]_{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}_[arguments]_{{end}}{{end}}{{if .Description}}
141
142 ## DESCRIPTION
143
144 {{.Description}}{{end}}
145
146 ## COMMANDS
147
148 {{range .VisibleCategories}}{{if .Name}}{{.Name}}:{{end}}
149 |||
150 |---|---|{{range .VisibleCommands}}
151 | **{{join .Names ", "}}** | {{.Usage}} |{{end}}
152 {{end}}{{if .VisibleFlags}}
153
154 ## OPTIONS
155
156 {{range .VisibleFlags}}
157 {{.}}
158 {{end}}{{end}}
159 `
160
161 // CommandHelpTemplate contains the modified template for a command
162 var CommandHelpTemplate = `## NAME
163 **{{.HelpName}}** -- {{.Usage}}
164
165 ## USAGE
166
167 {{if .UsageText}}{{.UsageText}}{{else}}**{{.HelpName}}**{{if .VisibleFlags}} _[options]_{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}_[arguments]_{{end}}{{end}}{{if .Category}}
168
169 ## CATEGORY
170
171 {{.Category}}{{end}}{{if .Description}}
172
173 ## DESCRIPTION
174
175 {{.Description}}{{end}}{{if .VisibleFlags}}
176
177 ## OPTIONS
178
179 {{range .VisibleFlags}}
180 {{.}}
181 {{end}}{{end}}
182 `
183
184 // FlagNamePrefixer converts a full flag name and its placeholder into the help
185 // message flag prefix. This is used by the default FlagStringer.
186 //
187 // This method clones urflave/cli functionality but adds a new line at the end.
188 func FlagNamePrefixer(fullName, placeholder string) string {
189 var prefixed string
190 parts := strings.Split(fullName, ",")
191 for i, name := range parts {
192 name = strings.Trim(name, " ")
193 prefixed += "**" + prefixFor(name) + name + "**"
194
195 if placeholder != "" {
196 prefixed += "=" + placeholder
197 }
198 if i < len(parts)-1 {
199 prefixed += ", "
200 }
201 }
202 //return "* " + prefixed + "\n"
203 return prefixed + "\n: "
204 }
205
206 func prefixFor(name string) (prefix string) {
207 if len(name) == 1 {
208 prefix = "-"
209 } else {
210 prefix = "--"
211 }
212
213 return
214 }