-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmpl.go
90 lines (74 loc) · 1.94 KB
/
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
package flagset
import (
"bytes"
"fmt"
"strings"
"text/template"
)
// TmplData is the structure used for usage output templating. Custom template
// string values should be based on this type.
type TmplData struct {
FlagSet *FlagSet
}
// TmplConfig tracks the template string and function map used for usage output
// templating.
type TmplConfig struct {
Text string
FMap template.FuncMap
}
// NewDefaultTmplConfig returns the default TmplConfig value. This can be used
// as an example of how to setup custom usage output templating.
func NewDefaultTmplConfig() *TmplConfig {
typeHintFn := func(t string) string {
if t == "" {
return ""
}
pre, post := "=", ""
if t == "bool" {
pre, post = "[=", "]"
}
return pre + strings.ToUpper(t) + post
}
defaultHintFn := func(d string) string {
if d == "" {
return ""
}
return "default: " + d
}
tmplFMap := template.FuncMap{
"Join": strings.Join,
"TypeHint": typeHintFn,
"DefaultHint": defaultHintFn,
}
tmplText := strings.TrimSpace(`
{{- if .FlagSet.Flags -}}
Flags for {{.FlagSet.Name}}:
{{range $i, $flag := .FlagSet.Flags}}
{{- if $flag.HideUsage}}{{continue}}{{end}}
{{if .}} {{end}}{{if $flag.Shorts}}-{{Join $flag.Shorts ", -"}}{{end}}
{{- if and $flag.Shorts $flag.Longs}}, {{end}}
{{- if $flag.Longs}}--{{Join $flag.Longs ", --"}}{{end}}
{{- if $flag.TypeName}} {{TypeHint $flag.TypeName}}{{end}}
{{- if $flag.DefaultText}} {{DefaultHint $flag.DefaultText}}{{end}}
{{$flag.Description}}
{{end}}{{else}}{{- end}}
`)
return &TmplConfig{
Text: tmplText,
FMap: tmplFMap,
}
}
func executeTmpl(tc *TmplConfig, data any) string {
tmpl := template.New("flagset").Funcs(tc.FMap)
buf := &bytes.Buffer{}
tmpl, err := tmpl.Parse(tc.Text)
if err != nil {
fmt.Fprintf(buf, "%v\n", err)
return buf.String()
}
if err := tmpl.Execute(buf, data); err != nil {
fmt.Fprintf(buf, "%v\n", err)
return buf.String()
}
return buf.String()
}