/
flags_options.go
44 lines (37 loc) · 928 Bytes
/
flags_options.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
package cli
type (
// FlagOption provides optional behavior to the CLI flags
// inlined in a Command definition.
FlagOption func(*flagOption)
flagOption struct {
fn RegisterFunc
required bool
persistent bool
configKey string
}
)
func flagWithOptions(fn RegisterFunc, opts []FlagOption) flagOption {
fl := flagOption{fn: fn}
for _, apply := range opts {
apply(&fl)
}
return fl
}
// FlagIsRequired declares the flag as required for the command.
func FlagIsRequired() FlagOption {
return func(o *flagOption) {
o.required = true
}
}
// FlagIsPersistent declares the flag as persistent for the command.
func FlagIsPersistent() FlagOption {
return func(o *flagOption) {
o.persistent = true
}
}
// BindFlagToConfig declares the flag as bound to a configuration key in the viper registry.
func BindFlagToConfig(key string) FlagOption {
return func(o *flagOption) {
o.configKey = key
}
}