-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag_def.go
209 lines (197 loc) · 5.12 KB
/
flag_def.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
package command
import (
"cmp"
"encoding/csv"
"errors"
"fmt"
"reflect"
"strconv"
"strings"
)
type ErrInvalidValue struct {
Cause error
Value string
Flag string
}
func (e *ErrInvalidValue) Error() string {
return fmt.Sprintf("invalid value '%s' for flag '%s': %s", e.Value, e.Flag, e.Cause)
}
func (e *ErrInvalidValue) Unwrap() error {
return e.Cause
}
type flagInfo struct {
Name string
EnvVarName *string
HasValue bool
ValueName *string
Description *string
Required *bool
DefaultValue string
}
type flagDef struct {
flagInfo
Inherited bool
Targets []reflect.Value
applied bool
}
func (fd *flagDef) isRequired() bool {
return fd.Required != nil && *fd.Required
}
func (fd *flagDef) getValueName() string {
if fd.HasValue {
if fd.ValueName != nil {
return *fd.ValueName
} else {
return "VALUE"
}
} else {
return ""
}
}
func (fd *flagDef) setValue(sv string) error {
for _, fv := range fd.Targets {
switch fv.Kind() {
case reflect.Bool:
if b, err := strconv.ParseBool(sv); err != nil {
var ne *strconv.NumError
if errors.As(err, &ne) {
return &ErrInvalidValue{Cause: ne.Err, Value: ne.Num, Flag: fd.Name}
} else {
return &ErrInvalidValue{Cause: err, Value: sv, Flag: fd.Name}
}
} else {
fv.SetBool(b)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if i, err := strconv.ParseInt(sv, 10, 64); err != nil {
var ne *strconv.NumError
if errors.As(err, &ne) {
return &ErrInvalidValue{Cause: ne.Err, Value: ne.Num, Flag: fd.Name}
} else {
return &ErrInvalidValue{Cause: err, Value: sv, Flag: fd.Name}
}
} else {
fv.SetInt(i)
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if ui, err := strconv.ParseUint(sv, 10, 64); err != nil {
var ne *strconv.NumError
if errors.As(err, &ne) {
return &ErrInvalidValue{Cause: ne.Err, Value: ne.Num, Flag: fd.Name}
} else {
return &ErrInvalidValue{Cause: err, Value: sv, Flag: fd.Name}
}
} else {
fv.SetUint(ui)
}
case reflect.Float32, reflect.Float64:
if f, err := strconv.ParseFloat(sv, 64); err != nil {
var ne *strconv.NumError
if errors.As(err, &ne) {
return &ErrInvalidValue{Cause: ne.Err, Value: ne.Num, Flag: fd.Name}
} else {
return &ErrInvalidValue{Cause: err, Value: sv, Flag: fd.Name}
}
} else {
fv.SetFloat(f)
}
case reflect.String:
fv.SetString(sv)
case reflect.Slice:
r := csv.NewReader(strings.NewReader(sv))
r.LazyQuotes = true
r.TrimLeadingSpace = true
rec, err := r.Read()
if err != nil {
return &ErrInvalidValue{Cause: err, Value: sv, Flag: fd.Name}
}
inValue := reflect.ValueOf(rec)
targetType := fv.Type().Elem()
outSlice := reflect.MakeSlice(reflect.SliceOf(targetType), inValue.Len(), inValue.Len())
for i, inElem := range rec {
var outElem interface{}
var err error
switch targetType.Kind() {
case reflect.String:
outElem = inElem
case reflect.Int:
outElem, err = strconv.Atoi(inElem)
case reflect.Float32:
if f64, parseErr := strconv.ParseFloat(inElem, 32); parseErr == nil {
outElem = float32(f64)
} else {
outElem = nil
err = parseErr
}
case reflect.Float64:
outElem, err = strconv.ParseFloat(inElem, 64)
case reflect.Bool:
outElem, err = strconv.ParseBool(inElem)
default:
return fmt.Errorf("%w: field kind is '%s'", errors.ErrUnsupported, fv.Kind())
}
if err != nil {
return &ErrInvalidValue{Cause: err, Value: inElem, Flag: fd.Name}
}
outSlice.Index(i).Set(reflect.ValueOf(outElem).Convert(outSlice.Type().Elem()))
}
fv.Set(outSlice)
default:
return fmt.Errorf("%w: field kind is '%s'", errors.ErrUnsupported, fv.Kind())
}
}
fd.applied = true
return nil
}
func (fd *flagDef) isLessThan(b *flagDef) bool {
a := fd
name := cmp.Compare(a.Name, b.Name)
if name < 0 {
return true
} else if name > 0 {
return false
}
envVarName := cmp.Compare(defaultIfNil(a.EnvVarName, ""), defaultIfNil(b.EnvVarName, ""))
if envVarName < 0 {
return true
} else if envVarName > 0 {
return false
}
hasValue := cmp.Compare(intForBool(a.HasValue), intForBool(b.HasValue))
if hasValue < 0 {
return true
} else if hasValue > 0 {
return false
}
valueName := cmp.Compare(defaultIfNil(a.ValueName, ""), defaultIfNil(b.ValueName, ""))
if valueName < 0 {
return true
} else if valueName > 0 {
return false
}
description := cmp.Compare(defaultIfNil(a.Description, ""), defaultIfNil(b.Description, ""))
if description < 0 {
return true
} else if description > 0 {
return false
}
required := cmp.Compare(intForBool(defaultIfNil(a.Required, false)), intForBool(defaultIfNil(b.Required, false)))
if required < 0 {
return true
} else if required > 0 {
return false
}
defaultValue := cmp.Compare(a.DefaultValue, b.DefaultValue)
if defaultValue < 0 {
return true
} else if defaultValue > 0 {
return false
}
inherited := cmp.Compare(intForBool(a.Inherited), intForBool(b.Inherited))
if inherited < 0 {
return true
} else if inherited > 0 {
return false
}
return false
}