/
option.go
276 lines (241 loc) · 6.29 KB
/
option.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package cmdkit
import (
"fmt"
"reflect"
"strconv"
"strings"
)
// Types of Command options
const (
Invalid = reflect.Invalid
Bool = reflect.Bool
Int = reflect.Int
Uint = reflect.Uint
Int64 = reflect.Int64
Uint64 = reflect.Uint64
Float = reflect.Float64
String = reflect.String
)
type OptMap map[string]interface{}
// Option is used to specify a field that will be provided by a consumer
type Option interface {
Name() string // the main name of the option
Names() []string // a list of unique names matched with user-provided flags
Type() reflect.Kind // value must be this type
Description() string // a short string that describes this option
WithDefault(interface{}) Option // sets the default value of the option
Default() interface{}
Parse(str string) (interface{}, error)
}
type option struct {
names []string
kind reflect.Kind
description string
defaultVal interface{}
}
func (o *option) Name() string {
return o.names[0]
}
func (o *option) Names() []string {
return o.names
}
func (o *option) Type() reflect.Kind {
return o.kind
}
func (o *option) Description() string {
if len(o.description) == 0 {
return ""
}
if !strings.HasSuffix(o.description, ".") {
o.description += "."
}
if o.defaultVal != nil {
if strings.Contains(o.description, "<<default>>") {
return strings.Replace(o.description, "<<default>>",
fmt.Sprintf("Default: %v.", o.defaultVal), -1)
} else {
return fmt.Sprintf("%s Default: %v.", o.description, o.defaultVal)
}
}
return o.description
}
type converter func(string) (interface{}, error)
var converters = map[reflect.Kind]converter{
Bool: func(v string) (interface{}, error) {
if v == "" {
return true, nil
}
v = strings.ToLower(v)
return strconv.ParseBool(v)
},
Int: func(v string) (interface{}, error) {
val, err := strconv.ParseInt(v, 0, 32)
if err != nil {
return nil, err
}
return int(val), err
},
Uint: func(v string) (interface{}, error) {
val, err := strconv.ParseUint(v, 0, 32)
if err != nil {
return nil, err
}
return uint(val), err
},
Int64: func(v string) (interface{}, error) {
val, err := strconv.ParseInt(v, 0, 64)
if err != nil {
return nil, err
}
return val, err
},
Uint64: func(v string) (interface{}, error) {
val, err := strconv.ParseUint(v, 0, 64)
if err != nil {
return nil, err
}
return val, err
},
Float: func(v string) (interface{}, error) {
return strconv.ParseFloat(v, 64)
},
String: func(v string) (interface{}, error) {
return v, nil
},
}
func (o *option) Parse(v string) (interface{}, error) {
conv, ok := converters[o.Type()]
if !ok {
return nil, fmt.Errorf("option %q takes %s arguments, but was passed %q", o.Name(), o.Type(), v)
}
return conv(v)
}
// constructor helper functions
func NewOption(kind reflect.Kind, names ...string) Option {
var desc string
if len(names) >= 2 {
desc = names[len(names)-1]
names = names[:len(names)-1]
}
return &option{
names: names,
kind: kind,
description: desc,
}
}
func (o *option) WithDefault(v interface{}) Option {
o.defaultVal = v
return o
}
func (o *option) Default() interface{} {
return o.defaultVal
}
// TODO handle description separately. this will take care of the panic case in
// NewOption
// For all func {Type}Option(...string) functions, the last variadic argument
// is treated as the description field.
func BoolOption(names ...string) Option {
return NewOption(Bool, names...)
}
func IntOption(names ...string) Option {
return NewOption(Int, names...)
}
func UintOption(names ...string) Option {
return NewOption(Uint, names...)
}
func Int64Option(names ...string) Option {
return NewOption(Int64, names...)
}
func Uint64Option(names ...string) Option {
return NewOption(Uint64, names...)
}
func FloatOption(names ...string) Option {
return NewOption(Float, names...)
}
func StringOption(names ...string) Option {
return NewOption(String, names...)
}
type OptionValue struct {
Value interface{}
ValueFound bool
Def Option
}
// Found returns true if the option value was provided by the user (not a default value)
func (ov *OptionValue) Found() bool {
return ov.ValueFound
}
// Definition returns the option definition for the provided value
func (ov *OptionValue) Definition() Option {
return ov.Def
}
// value accessor methods, gets the value as a certain type
func (ov *OptionValue) Bool() (value bool, found bool, err error) {
if ov == nil || !ov.ValueFound && ov.Value == nil {
return false, false, nil
}
val, ok := ov.Value.(bool)
if !ok {
err = fmt.Errorf("expected type %T, got %T", val, ov.Value)
}
return val, ov.ValueFound, err
}
func (ov *OptionValue) Int() (value int, found bool, err error) {
if ov == nil || !ov.ValueFound && ov.Value == nil {
return 0, false, nil
}
val, ok := ov.Value.(int)
if !ok {
err = fmt.Errorf("expected type %T, got %T", val, ov.Value)
}
return val, ov.ValueFound, err
}
func (ov *OptionValue) Uint() (value uint, found bool, err error) {
if ov == nil || !ov.ValueFound && ov.Value == nil {
return 0, false, nil
}
val, ok := ov.Value.(uint)
if !ok {
err = fmt.Errorf("expected type %T, got %T", val, ov.Value)
}
return val, ov.ValueFound, err
}
func (ov *OptionValue) Int64() (value int64, found bool, err error) {
if ov == nil || !ov.ValueFound && ov.Value == nil {
return 0, false, nil
}
val, ok := ov.Value.(int64)
if !ok {
err = fmt.Errorf("expected type %T, got %T", val, ov.Value)
}
return val, ov.ValueFound, err
}
func (ov *OptionValue) Uint64() (value uint64, found bool, err error) {
if ov == nil || !ov.ValueFound && ov.Value == nil {
return 0, false, nil
}
val, ok := ov.Value.(uint64)
if !ok {
err = fmt.Errorf("expected type %T, got %T", val, ov.Value)
}
return val, ov.ValueFound, err
}
func (ov *OptionValue) Float() (value float64, found bool, err error) {
if ov == nil || !ov.ValueFound && ov.Value == nil {
return 0, false, nil
}
val, ok := ov.Value.(float64)
if !ok {
err = fmt.Errorf("expected type %T, got %T", val, ov.Value)
}
return val, ov.ValueFound, err
}
func (ov *OptionValue) String() (value string, found bool, err error) {
if ov == nil || !ov.ValueFound && ov.Value == nil {
return "", false, nil
}
val, ok := ov.Value.(string)
if !ok {
err = fmt.Errorf("expected type %T, got %T", val, ov.Value)
}
return val, ov.ValueFound, err
}