-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathhelp.go
More file actions
326 lines (280 loc) · 9.25 KB
/
help.go
File metadata and controls
326 lines (280 loc) · 9.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package gcli
import (
"fmt"
"sort"
"strings"
"text/template"
"github.com/gookit/color"
"github.com/gookit/gcli/v3/events"
"github.com/gookit/gcli/v3/internal/helper"
"github.com/gookit/goutil/maputil"
"github.com/gookit/goutil/strutil"
"github.com/gookit/goutil/sysutil"
)
// HelpConfig struct
type HelpConfig struct {
// AfterCmdText add text after commands list
AfterCmdText string
// FooterText add help footer text on help end
FooterText string
}
// CmdGroup is a group of commands by category. used for render help.
type CmdGroup struct {
// Name is the category name. "" means the default group.
Name string
// Title is the display title for help. eg: "Available Commands"
Title string
// Cmds are the visible commands of this group, sorted by name.
Cmds []*Command
}
// CommandsByGroup group all visible commands by their Category field.
//
// - groups keep the category insertion order(see base.cmdCategories).
// - the default group(empty Category) uses defaultTitle as its title.
// - returns nil when there is no visible command.
func (b *base) CommandsByGroup(defaultTitle string) []*CmdGroup {
groups := make([]*CmdGroup, 0, len(b.cmdCategories))
for _, cat := range b.cmdCategories {
var cmds []*Command
for _, c := range b.commands {
if c.Category == cat && c.Visible() {
cmds = append(cmds, c)
}
}
if len(cmds) == 0 {
continue
}
sort.Slice(cmds, func(i, j int) bool {
return cmds[i].Name < cmds[j].Name
})
title := defaultTitle
if cat != "" {
title = strutil.UpperFirst(cat)
}
groups = append(groups, &CmdGroup{Name: cat, Title: title, Cmds: cmds})
}
return groups
}
/*************************************************************
* display app help
*************************************************************/
// display app version info
func (app *App) showVersionInfo() bool {
Debugf("print application version info")
color.Printf(
"%s\n\nVersion: <cyan>%s</>\n",
strutil.UpperFirst(app.Desc),
app.Version,
)
if app.Logo.Text != "" {
color.Printf("%s\n", color.WrapTag(app.Logo.Text, app.Logo.Style))
}
return false
}
// display unknown input command and similar commands tips
func (app *App) showCommandTips(name string) {
Debugf("will find and show similar command tips")
color.Error.Tips(`unknown input command "<mga>%s</>"`, name)
if ns := app.findSimilarCmd(name); len(ns) > 0 {
color.Printf("\nMaybe you mean:\n <green>%s</>\n", strings.Join(ns, ", "))
}
color.Printf("\nUse <cyan>%s --help</> to see available commands\n", app.Ctx.binName)
}
// AppHelpTemplate help template for app(all commands)
var AppHelpTemplate = `{{.Desc}} (Version: <info>{{.Version}}</>)
<comment>Usage:</>
{$binName} [global options...] <info>COMMAND</> [--options ...] [arguments ...]{{if .HasSubs }}
{$binName} [global options...] <info>COMMAND</> [--options ...] <info>SUBCOMMAND</> [--options ...] [arguments ...]{{end}}
<comment>Global Options:</>
{{.GOpts}}
{{range $g := .CmdGroups}}<comment>{{$g.Title}}:</>{{range $c := $g.Cmds}}
<info>{{$c.Name | paddingName }}</> {{$c.HelpDesc}}{{if $c.Aliases}} (alias: <green>{{ join $c.Aliases ","}}</>){{end}}{{end}}
{{end}} <info>{{ paddingName "help" }}</> Display help information
{{.Help.AfterCmdText}}Use "<cyan>{$binName} COMMAND -h</>" for more information about a command.{{.Help.FooterText}}
`
// display app help and list all commands. showCommandList()
func (app *App) showApplicationHelp() bool {
Debugf("render application help and commands list, replaces=%s", maputil.ToString2(app.Replaces()))
app.Fire(events.OnAppHelpBefore, nil)
// cmdHelpTemplate = color.ReplaceTag(cmdHelpTemplate)
// render help text template
s := helper.RenderText(AppHelpTemplate, map[string]any{
"CmdGroups": app.CommandsByGroup("Available Commands"),
"GOpts": app.fs.BuildOptsHelp(),
// app version
"Version": app.Version,
"HasSubs": app.hasSubcommands,
// always upper first char
"Desc": strutil.UpperFirst(app.Desc),
// user custom help vars
"Vars": app.HelpVars,
// custom help config
"Help": app.HelpConfig,
}, template.FuncMap{
"paddingName": func(n string) string {
return strutil.PadRight(n, " ", app.nameMaxWidth)
},
})
// parse help vars and render color tags
color.Print(app.ReplacePairs(s))
app.Fire(events.OnAppHelpAfter, nil)
if sysutil.IsLinux() {
fmt.Println()
}
return false
}
// showCommandHelp display help for a command
func (app *App) showCommandHelp(list []string) (code PrepareState) {
binName := app.Ctx.binName
// if len(list) == 0 { TODO support multi level sub command?
if len(list) > 1 {
color.Error.Tips("Too many arguments given.\n\nUsage: %s help COMMAND", binName)
return ERR
}
// get real name
name := app.cmdAliases.ResolveAlias(list[0])
if name == HelpCommand || name == "-h" {
Debugf("render help command information")
color.Println("Display help message for application or command.\n")
color.Printf(`<yellow>Usage:</>
<cyan>%s COMMAND --help</>
<cyan>%s COMMAND SUBCOMMAND --help</>
<cyan>%s COMMAND SUBCOMMAND ... --help</>
<cyan>%s help COMMAND</>
`, binName, binName, binName, binName)
return
}
cmd, exist := app.Command(name)
if !exist {
color.Error.Prompt("Unknown command name '%s'. Run '%s -h' see all commands", name, binName)
return ERR
}
// show help for the give command.
_ = cmd.ShowHelp()
return
}
// showAutoCompletion 计算并逐行打印运行期动态补全候选(纯文本, 无颜色), 供 shell 脚本解析。
//
// words 为 shell 传入、已去掉 bin 名的命令行片段; 无候选时不输出。
func (app *App) showAutoCompletion(words []string) {
for _, item := range app.resolveCompletion(words) {
fmt.Println(item)
}
}
// findSimilarCmd find similar cmd by input string
func (app *App) findSimilarCmd(input string) []string {
var ss []string
// ins := strings.Split(input, "")
// fmt.Print(input, ins)
ln := len(input)
// NOTE: copy the map. CmdNameMap() returns the real cmdNames map, mutating it
// here would pollute the command registry(eg add a phantom 'help' command).
src := app.CmdNameMap()
names := make(map[string]int, len(src)+1)
for n, l := range src {
names[n] = l
}
names["help"] = 4 // add built-in 'help' command for matching
// find from command names
for name := range names {
cln := len(name)
if cln > ln && strings.Contains(name, input) {
ss = append(ss, name)
} else if ln > cln && strings.Contains(input, name) {
// sns := strings.Split(str, "")
ss = append(ss, name)
}
// max find 5 items
if len(ss) == 5 {
break
}
}
// find from aliases
for alias := range app.cmdAliases.Mapping() {
// max find 5 items
if len(ss) >= 5 {
break
}
cln := len(alias)
if cln > ln && strings.Contains(alias, input) {
ss = append(ss, alias)
} else if ln > cln && strings.Contains(input, alias) {
ss = append(ss, alias)
}
}
return ss
}
/*************************************************************
* display command help
*************************************************************/
// CmdHelpTemplate help template for a command
var CmdHelpTemplate = `{{.Desc}}
{{if .Cmd.NotStandalone}}
<comment>Name:</> {{.Cmd.Name}}{{if .Cmd.Aliases}} (alias: <info>{{.Cmd.Aliases.String}}</>){{end}}{{end}}
<comment>Usage:</>
{$binName} [global options] {{if .Cmd.NotStandalone}}<cyan>{{.Cmd.Path}}</> {{end}}[--options ...] [arguments ...]{{ if .SubGroups }}
{$binName} [global options] {{if .Cmd.NotStandalone}}<cyan>{{.Cmd.Path}}</> {{end}}<cyan>SUBCOMMAND</> [--options ...] [arguments ...]{{end}}
{{if .GOpts}}
<comment>Global Options:</>
{{.GOpts}}{{end}}{{if .Options}}
<comment>Options:</>
{{.Options}}{{end}}{{if .ArgsHelp}}
<comment>Arguments:</>
{{.ArgsHelp}}{{end}}{{range $g := .SubGroups}}
<comment>{{$g.Title}}:</>{{range $c := $g.Cmds}}
<info>{{$c.Name | paddingName }}</> {{$c.HelpDesc}}{{if $c.Aliases}} (alias: <green>{{ join $c.Aliases ","}}</>){{end}}{{end}}
{{end}}{{.Help.AfterCmdText}}{{if .Cmd.Examples}}
<comment>Examples:</>
{{.Cmd.Examples}}{{end}}{{if .Cmd.Help}}
<comment>Help:</>
{{.Cmd.Help}}{{end}}{{.Help.FooterText}}`
// ShowHelp show command help information
func (c *Command) ShowHelp() (err error) {
Debugf("render the command '%s' help information", c.Name)
// custom help render func
if c.HelpRender != nil {
c.HelpRender(c)
return
}
// clear space and empty new line
if c.Examples != "" {
c.Examples = strings.Trim(c.Examples, "\n") + "\n"
}
// clear space and empty new line
if c.Help != "" {
c.Help = strings.TrimSpace(c.Help) + "\n"
}
vars := map[string]any{
"Cmd": c,
"SubGroups": c.CommandsByGroup("Subcommands"),
// global options
// - on standalone
"GOpts": nil,
// parse options to string
"Options": c.Flags.BuildOptsHelp(),
// parse options to string
"ArgsHelp": c.Flags.BuildArgsHelp(),
// always upper first char
"Desc": c.HelpDesc(),
// user custom help vars
"Vars": c.HelpVars,
// custom help config
"Help": c.HelpConfig,
}
// if c.NotStandalone() {
// vars["GOpts"] = c.GFlags().BuildHelp()
// }
// render help message
str := helper.RenderText(CmdHelpTemplate, vars, template.FuncMap{
"paddingName": func(n string) string {
return strutil.PadRight(n, " ", c.nameMaxWidth)
},
})
// parse gcli help vars then print help
// fmt.Printf("%#v\n", s)
color.Print(c.ReplacePairs(str))
if sysutil.IsLinux() {
fmt.Println()
}
return
}