This repository has been archived by the owner on Jul 26, 2021. It is now read-only.
/
template.go
99 lines (86 loc) · 2.24 KB
/
template.go
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
package cliutils
import "fmt"
var CustomTemplate string
func recursiveCommandMatcher(cmds []string) string {
if len(cmds) == 0 {
return "false"
}
cmd := cmds[0]
return fmt.Sprintf(`(or (eq .Name "%s") %s)`, cmd, recursiveCommandMatcher(cmds[1:]))
}
func init() {
fastAccessCmdStr := recursiveCommandMatcher([]string{
"ssh",
"help",
"deploy",
"edit",
"proxy",
"nc",
})
CustomTemplate = `{{define "FormatCommand"}}\
{{if .FlagSummary}} {{.FlagSummary}}{{end}}\
{{range .Args}} {{if not .Required}}[{{end}}<{{.Name}}>{{if .Value|IsCumulative}}...{{end}}{{if not .Required}}]{{end}}{{end}}\
{{end}}\
{{define "CustomCmd"}}\
{{if not .Hidden}}\
{{.Depth|Indent}} {{.Name}}{{if .Default}}*{{end}}{{template "FormatCommand" .}} -- {{.Help}}
{{range .Commands}}\
{{template "CustomCmd" .}}\
{{end}}\
{{end}}\
{{end}}\
{{define "AllCmds"}}\
{{range .}}\
{{template "CustomCmd" .}}\
{{end}}\
{{end}}\
{{define "TopLevelFastAccessCmds"}}\
{{range .}}\
{{if ` + fastAccessCmdStr + `}}\
{{template "CustomCmd" .}}\
{{end}}\
{{end}}\
{{end}}\
{{define "TopLevelRegularCmds"}}\
{{range .}}\
{{if not ` + fastAccessCmdStr + `}}\
{{template "CustomCmd" .}}\
{{.Depth|Indent}}
{{end}}\
{{end}}\
{{end}}\
{{define "FormatUsage"}}\
{{template "FormatCommand" .}}{{if .Commands}} <command> [<args> ...]{{end}}
{{if .Help}}
{{.Help|Wrap 0}}\
{{end}}\
{{end}}\
{{define "AutocompleteHelp"}}\
To add autocompletion, add ` + "`" + `eval "$({{.}} --completion-script-bash)"` + "`" + ` to your bashrc or zshrc file.
{{end}}\
{{if .Context.SelectedCommand}}\
usage: {{.App.Name}} {{.Context.SelectedCommand}}{{template "FormatUsage" .Context.SelectedCommand}}
{{else}}\
usage: {{.App.Name}}{{template "FormatUsage" .App}}{{template "AutocompleteHelp" .App.Name}}
{{end}}\
{{if .Context.Flags}}\
Flags:
{{.Context.Flags|FlagsToTwoColumns|FormatTwoColumns}}
{{end}}\
{{if .Context.Args}}\
Args:
{{.Context.Args|ArgsToTwoColumns|FormatTwoColumns}}
{{end}}\
{{if .Context.SelectedCommand}}\
{{if len .Context.SelectedCommand.Commands}}\
Subcommands:
{{template "AllCmds" .Context.SelectedCommand.Commands}}
{{end}}\
{{else if .App.Commands}}\
Fast-Access Commands:
{{template "TopLevelFastAccessCmds" .App.Commands}}
Commands:
{{template "TopLevelRegularCmds" .App.Commands}}
{{end}}\
`
}