-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoption.go
More file actions
143 lines (116 loc) · 3.63 KB
/
option.go
File metadata and controls
143 lines (116 loc) · 3.63 KB
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
package redant
import (
"os"
"github.com/spf13/pflag"
)
// Option is a configuration option for a CLI application.
type Option struct {
// Flag is the long name of the flag used to configure this option. If unset,
// flag configuring is disabled. This also serves as the option's identifier.
Flag string `json:"flag,omitempty"`
Description string `json:"description,omitempty"`
// Required means this value must be set by some means. It requires
// `ValueSourceType != ValueSourceNone`
// If `Default` is set, then `Required` is ignored.
Required bool `json:"required,omitempty"`
// Shorthand is the one-character shorthand for the flag. If unset, no
// shorthand is used.
Shorthand string `json:"shorthand,omitempty"`
// Envs is a list of environment variables used to configure this option.
// The first non-empty environment variable value will be used.
// If unset, environment configuring is disabled.
Envs []string `json:"env,omitempty"`
// Default is parsed into Value if set.
Default string `json:"default,omitempty"`
// Value includes the types listed in values.go.
Value pflag.Value `json:"value,omitempty"`
Hidden bool `json:"hidden,omitempty"`
Deprecated string
Category string
// Action is called after the flag is parsed and set.
// It receives the flag value and can perform additional validation or side effects.
// If Action returns an error, command execution will fail.
Action func(val pflag.Value) error `json:"-"`
}
// OptionSet is a group of options that can be applied to a command.
type OptionSet []Option
// Add adds the given Options to the OptionSet.
func (optSet *OptionSet) Add(opts ...Option) {
*optSet = append(*optSet, opts...)
}
// Filter will only return options that match the given filter. (return true)
func (optSet *OptionSet) Filter(filter func(opt Option) bool) OptionSet {
cpy := make(OptionSet, 0)
for _, opt := range *optSet {
if filter(opt) {
cpy = append(cpy, opt)
}
}
return cpy
}
// Type returns the type of the option value
func (o Option) Type() string {
if o.Value != nil {
return o.Value.Type()
}
return "string"
}
func (optSet *OptionSet) FlagSet(name string) *pflag.FlagSet {
if optSet == nil {
return &pflag.FlagSet{}
}
fs := pflag.NewFlagSet(name, pflag.PanicOnError)
for _, opt := range *optSet {
if opt.Flag == "" {
continue
}
var noOptDefValue string
{
no, ok := opt.Value.(NoOptDefValuer)
if ok {
noOptDefValue = no.NoOptDefValue()
}
}
val := opt.Value
if val == nil {
val = DiscardValue
}
// Apply default value to the Value before adding the flag
if opt.Default != "" && val != DiscardValue {
_ = val.Set(opt.Default) // Ignore error, will be caught during validation
}
fs.AddFlag(&pflag.Flag{
Name: opt.Flag,
Shorthand: opt.Shorthand,
Usage: opt.Description,
Value: val,
DefValue: opt.Default,
Changed: false,
Deprecated: opt.Deprecated,
NoOptDefVal: noOptDefValue,
Hidden: opt.Hidden,
})
}
fs.Usage = func() {
_, _ = os.Stderr.WriteString("Override (*FlagSet).Usage() to print help text.\n")
}
// Read environment variables and set flag values
// Use the first non-empty environment variable value
for _, opt := range *optSet {
if opt.Flag == "" || opt.Value == nil {
continue
}
// Try each environment variable in order, use the first non-empty one
for _, envName := range opt.Envs {
if envValue := os.Getenv(envName); envValue != "" {
if flag := fs.Lookup(opt.Flag); flag != nil {
if err := flag.Value.Set(envValue); err == nil {
flag.Changed = true
break // Use the first non-empty value
}
}
}
}
}
return fs
}