-
Notifications
You must be signed in to change notification settings - Fork 10
/
help_tmpl.go
149 lines (119 loc) · 3.51 KB
/
help_tmpl.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
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
package clop
import (
"io"
"sort"
"strings"
"text/template"
)
func init() {
funcMap = template.FuncMap{
"addSpace": addSpace,
"sub": sub,
}
}
var funcMap map[string]interface{}
func addSpace(max, cur int) string {
return strings.Repeat(" ", max-cur)
}
func sub(index int) int {
index--
return index
}
type showOption struct {
Opt string
Usage string
Env string
Default string
}
type Help struct {
ProcessName string
Version string
About string
Flags []showOption
Options []showOption
Args []showOption
Envs []showOption
Subcommand []showOption
MaxNameLen int
ShowUsageDefault bool
}
func (h *Help) output(w io.Writer) error {
sort.Slice(h.Flags, func(i, j int) bool {
return h.Flags[i].Opt < h.Flags[j].Opt
})
sort.Slice(h.Options, func(i, j int) bool {
return h.Options[i].Opt < h.Options[j].Opt
})
sort.Slice(h.Subcommand, func(i, j int) bool {
return h.Subcommand[i].Opt < h.Subcommand[j].Opt
})
tmpl := newTemplate()
return tmpl.Execute(w, *h)
}
var usageDefaultTmpl = `{{- $ShowUsageDefault := .ShowUsageDefault}}{{- if gt (len .About) 0}}
{{- .About}}
{{end}}
{{- if or (gt (len .Flags) 0) (gt (len .Options) 0) (gt (len .Args) 0) (gt (len .Subcommand) 0)}}Usage:
{{if gt (len .ProcessName) 0}}{{.ProcessName}} {{end}}
{{- if gt (len .Flags) 0}}[Flags] {{end}}
{{- if gt (len .Options) 0}}[Options] {{end}}
{{- range $_, $flag := .Args}}{{$flag.Opt}} {{end}}
{{- if gt (len .Subcommand) 0}}<Subcommand> {{end}}
{{- end}}
{{- $maxNameLen :=.MaxNameLen}}
{{- if gt (len .Flags) 0 }}
Flags:
{{- $length := len .Flags}}
{{- $length = sub $length}}
{{range $index, $flag:= .Flags}} {{addSpace $maxNameLen (len $flag.Opt)|printf "%s%s" $flag.Opt}} {{$flag.Usage}}
{{- if gt (len $flag.Env) 0 }} [env: {{$flag.Env}}] {{- end}}
{{- if and (gt (len $flag.Default) 0) $ShowUsageDefault}} [default: {{$flag.Default}}] {{- end}}
{{- if ne $index $length}}
{{end}}
{{- end}}
{{- end}}
{{- if gt (len .Options) 0 }}
Options:
{{- $length := len .Options}}
{{- $length = sub $length}}
{{range $index, $flag:= .Options}} {{addSpace $maxNameLen (len $flag.Opt)|printf "%s%s" $flag.Opt}} {{$flag.Usage}}
{{- if gt (len $flag.Env) 0 }} [env: {{$flag.Env}}]{{- end}}
{{- if and (gt (len $flag.Default) 0 ) $ShowUsageDefault}} [default: {{$flag.Default}}]{{- end}}
{{- if ne $index $length}}
{{end}}
{{- end}}
{{- end}}
{{- if gt (len .Args) 0}}
Args:
{{- $length := len .Args}}
{{- $length = sub $length}}
{{range $index, $flag:= .Args}} {{addSpace $maxNameLen (len $flag.Opt)|printf "%s%s" $flag.Opt}} {{$flag.Usage}}
{{- if gt (len $flag.Env) 0 }} [env: {{$flag.Env}}]{{- end}}
{{- if ne $index $length}}
{{end}}
{{- end}}
{{- end}}
{{- if gt (len .Envs) 0}}
Environment Variable:
{{- $length := len .Envs}}
{{- $length = sub $length}}
{{range $index, $flag:= .Envs}} {{addSpace $maxNameLen (len $flag.Opt)|printf "%s%s" $flag.Opt}} {{$flag.Usage}}
{{- if ne $index $length}}
{{end}}
{{- end}}
{{- end}}
{{- if gt (len .Subcommand) 0 }}
Subcommand:
{{- $length := len .Subcommand}}
{{- $length = sub $length}}
{{range $index, $flag:= .Subcommand}} {{addSpace $maxNameLen (len $flag.Opt)|printf "%s%s" $flag.Opt}} {{$flag.Usage}}
{{- if gt (len $flag.Env) 0 }} [env: {{$flag.Env}}]{{- end}}
{{- if ne $index $length}}
{{end}}
{{- end}}
{{- end}}
`
func newTemplate() *template.Template {
tmpl := usageDefaultTmpl
return template.Must(template.New("clop-default-usage").Funcs(funcMap).Parse(tmpl))
}