This repository has been archived by the owner on Oct 31, 2020. It is now read-only.
/
option.go
299 lines (235 loc) · 7.59 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package config
import (
"fmt"
"strconv"
)
// Type is a string representing the type of data stored by an Option
type Type string
// These Types constants parallel their standard counterparts, and are the four elementary types that come
// when unmarshaling JSON
const (
BoolType Type = "bool"
StringType = "string"
FloatType = "float64"
IntType = "int64"
CustomType = "custom"
)
// Option holds information for a configuration option
type Option struct {
// The name of the option is what's used to reference the option and its value during the program
Name string
// What the option is for. This also shows up when invoking `program --help`.
Description string
// Holds the actual value contained by this option
Value interface{}
// Holds the default value for this option
DefaultValue interface{}
// Holds the type of this option
Type Type
// Extra options
Options OptionMeta
overridden bool
scopes []string
isBuiltIn bool
}
// OptionMeta holds information for configuring options on Options
type OptionMeta struct {
// Exportable is true if the option is exportable to a config.json file
Exportable bool
// Validate is true if the option is required
Validate bool
// Filters is a set of boolean functions that are tested with the given value. If Validate is true, all of these must succeed.
Filters []OptionFilterFunc
// SortOrder controls the sort order of Options when displayed in Usage(). Defaults to 0; ties are resolved alphabetically.
SortOrder int
}
// OptionFilterFunc is a function type that takes an *Option as a parameter. It returns true, nil if the *Option passes the filter, and false, error with a reason why if it didn't.
type OptionFilterFunc func(*Option) (bool, error)
// DefaultOptionMeta returns the default OptionMeta object
var DefaultOptionMeta = OptionMeta{
Exportable: false,
Validate: true,
Filters: []OptionFilterFunc{},
SortOrder: 0,
}
// Str creates an Option with the parameters given of type string
func Str(name string, defaultValue string, description string) *Option {
v := Option{
Name: name,
Description: description,
DefaultValue: defaultValue,
Value: defaultValue,
Type: StringType,
Options: DefaultOptionMeta,
}
return &v
}
// Bool creates an Option with the parameters given of type bool
func Bool(name string, defaultValue bool, description string) *Option {
v := Option{
Name: name,
Description: description,
DefaultValue: defaultValue,
Value: defaultValue,
Type: BoolType,
Options: DefaultOptionMeta,
}
return &v
}
// Int creates an Option with the parameters given of type int64
func Int(name string, defaultValue int64, description string) *Option {
v := Option{
Name: name,
Description: description,
DefaultValue: defaultValue,
Value: defaultValue,
Type: IntType,
Options: DefaultOptionMeta,
}
return &v
}
// Float creates an Option with the parameters given of type float64
func Float(name string, defaultValue float64, description string) *Option {
v := Option{
Name: name,
Description: description,
DefaultValue: defaultValue,
Value: defaultValue,
Type: FloatType,
Options: DefaultOptionMeta,
}
return &v
}
// Enum creates an Option with the parameters given of type string and a built-in validation to make sure
// that the parsed Option value is contained within the possibleValues argument.
func Enum(name string, possibleValues []string, defaultValue string, description string) *Option {
v := Option{
Name: name,
Description: description,
DefaultValue: defaultValue,
Value: defaultValue,
Type: StringType,
Options: DefaultOptionMeta,
}
v.
Validate(true).
AddFilter(IsOneOfStrings(possibleValues))
return &v
}
// DebugString returns a string describing some attributes about the Option, including the name, value, type and what scopes it came from.
func (o Option) DebugString() string {
return fmt.Sprintf(`name: %s, value: %v, type: %s, scopes: %s`, o.Name, o.Value, o.Type, o.scopes)
}
// String implements fmt.Stringer. This is used for printing the OptionSet if needed; you should use Str() to
// return the string value of a string Option, as it'll return what you expect all the time.
func (o Option) String() string {
return fmt.Sprintf(`%v`, o.Value)
}
// Str returns the string value of the option. Will panic if the Option's type is not a string.
func (o Option) Str() string {
return o.Value.(string)
}
// Bool returns the bool value of the option. Will panic if the Option's type is not a bool.
func (o Option) Bool() bool {
return o.Value.(bool)
}
// Float returns the float64 value of the option. Will panic if the Option's type is not a float64.
func (o Option) Float() float64 {
return o.Value.(float64)
}
// Int returns the int64 value of the option. Will panic if the Option's type not an int64.
func (o Option) Int() int64 {
return o.Value.(int64)
}
// defaultValueString returns the Option's default value as a string. If that value resolves to "", it'll return the
// emptyReplacement argument instead.
func (o Option) defaultValueString(emptyReplacement string) string {
ret := fmt.Sprintf(`%v`, o.DefaultValue)
if ret == "" {
ret = emptyReplacement
}
return ret
}
// AddScope adds a scope to an Option indicating that it was parsed in a file with the given scope.
func (o *Option) AddScope(s string) {
if o.scopes == nil {
o.scopes = make([]string, 0)
}
o.scopes = append(o.scopes, s)
}
// HasScope returns true if the Option has the specified scope.
func (o *Option) HasScope(s string) bool {
for _, v := range o.scopes {
if v == s {
return true
}
}
return false
}
// DefaultValueString returns the Option's default value as a string.
func (o Option) DefaultValueString() string {
return o.defaultValueString("")
}
// SetFromFlagValue attempts to set the Option's value as its proper type by parsing the string argument, and also
// sets a hidden value on the Option indicating it was overridden by a flag argument.
func (o *Option) SetFromFlagValue(val string) (err error) {
err = o.SetFromString(val)
if err != nil {
return err
}
o.overridden = true
o.AddScope("flag")
return nil
}
// SetFromString attempts to set the Option's value as its proper type by parsing the string argument
func (o *Option) SetFromString(val string) (err error) {
switch o.Type {
case StringType:
o.Value = val
case IntType:
v, err := strconv.ParseInt(val, 0, 64)
if err == nil {
o.Value = v
}
case FloatType:
v, err := strconv.ParseFloat(val, 64)
if err == nil {
o.Value = v
}
case BoolType:
switch val {
case "1", "t", "T", "true", "TRUE", "True":
o.Value = true
case "0", "f", "F", "false", "FALSE", "False":
o.Value = false
default:
err = fmt.Errorf("Invalid boolean value: %s", val)
}
}
return
}
// Exportable sets whether or not the Option is exportable to a config file.
func (o *Option) Exportable(v bool) *Option {
o.Options.Exportable = v
return o
}
// Validate sets whether or not the Filters on the Option will be tested for validity before being accepted.
func (o *Option) Validate(v bool) *Option {
o.Options.Validate = v
return o
}
// AddFilter adds an OptionFilterFunc to the Option's filter set. It also sets Validate to true.
func (o *Option) AddFilter(v OptionFilterFunc) *Option {
o.Options.Filters = append(o.Options.Filters, v)
o.Options.Validate = true
return o
}
// SortOrder sets the sort order on the Option used in Usage().
func (o *Option) SortOrder(i int) *Option {
o.Options.SortOrder = i
return o
}
func (o *Option) builtIn() *Option {
o.isBuiltIn = true
return o
}