-
Notifications
You must be signed in to change notification settings - Fork 6
/
flags.go
116 lines (94 loc) · 2.49 KB
/
flags.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
package cmd
import (
"fmt"
"strings"
"github.com/spf13/pflag"
)
type DocOpt[D any] struct {
choices []string
defaultValue *D
shorthand *string
}
func combine[D any](dst *DocOpt[D], src DocOpt[D]) {
if src.choices != nil {
dst.choices = src.choices
}
if src.defaultValue != nil {
dst.defaultValue = src.defaultValue
}
if src.shorthand != nil {
dst.shorthand = src.shorthand
}
}
func ViperNameToEnv(s string) string {
s = envKeyReplacer.Replace(s)
s = fmt.Sprintf("%s_%s", envPrefix, s)
s = strings.ToUpper(s)
return s
}
func boolFlag(flags *pflag.FlagSet, name, usage string, opts ...DocOpt[bool]) {
addFlag(name, usage, opts, flags.Bool, flags.BoolP)
}
func newBoolOpts() DocOpt[bool] {
return DocOpt[bool]{}
}
func newInt64Opts() DocOpt[int64] { return DocOpt[int64]{} }
func newStringOpts() DocOpt[string] {
return DocOpt[string]{}
}
func newStringSliceOpts() DocOpt[[]string] {
return DocOpt[[]string]{}
}
func int64Flag(flags *pflag.FlagSet, name, usage string, opts ...DocOpt[int64]) {
addFlag(name, usage, opts, flags.Int64, flags.Int64P)
}
func stringFlag(flags *pflag.FlagSet, name, usage string, opts ...DocOpt[string]) {
addFlag(name, usage, opts, flags.String, flags.StringP)
}
func stringSliceFlag(flags *pflag.FlagSet, name, usage string, opts ...DocOpt[[]string]) {
addFlag(name, usage, opts, flags.StringArray, flags.StringArrayP)
}
func addFlag[D any](
name, usage string,
opts []DocOpt[D],
onlyLong func(string, D, string) *D,
longAndShort func(string, string, D, string) *D,
) {
var opt DocOpt[D]
for _, o := range opts {
combine(&opt, o)
}
usage = generateUsage(opt, usage, name)
var defaultValue D
if opt.defaultValue != nil {
defaultValue = *opt.defaultValue
}
if opt.shorthand != nil {
longAndShort(name, *opt.shorthand, defaultValue, usage)
} else {
onlyLong(name, defaultValue, usage)
}
}
func generateUsage[D any](opt DocOpt[D], usage string, name string) string {
if !strings.HasSuffix(usage, ".") {
panic(fmt.Sprintf("usage for %q must end with a period.", name))
}
if opt.choices != nil {
usage = fmt.Sprintf("%s One of %s.", usage, strings.Join(opt.choices, ", "))
}
envVar := ViperNameToEnv(name)
usage = fmt.Sprintf("%s (%s)", usage, envVar)
return usage
}
func (d DocOpt[D]) withDefault(def D) DocOpt[D] {
d.defaultValue = &def
return d
}
func (d DocOpt[D]) withShortHand(short string) DocOpt[D] {
d.shorthand = &short
return d
}
func (d DocOpt[D]) withChoices(choices ...string) DocOpt[D] {
d.choices = choices
return d
}