-
Notifications
You must be signed in to change notification settings - Fork 2
/
value.go
262 lines (202 loc) · 5.06 KB
/
value.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
package xflags
import (
"fmt"
"strconv"
"time"
)
// Value is the interface to the dynamic value stored in a flag.
// (The default value is represented as a string.)
//
// Set is called once, in command line order, for each flag present.
type Value interface {
// String() string
Set(s string) error
}
// BoolValue is an optional interface to indicate boolean flags that can be
// supplied without a "=value" argument.
type BoolValue interface {
Value
IsBoolFlag() bool
}
func isBoolValue(v Value) bool {
if bv, ok := v.(BoolValue); ok {
return bv.IsBoolFlag()
}
return false
}
// ValidateFunc is a function that validates an argument before it is parsed.
type ValidateFunc = func(arg string) error
type bitFieldValue struct {
p *uint64
mask uint64
}
func newBitFieldValue(val bool, p *uint64, mask uint64) *bitFieldValue {
v := &bitFieldValue{p: p, mask: mask}
v.set(val)
return v
}
func (p *bitFieldValue) IsBoolFlag() bool { return true }
func (p *bitFieldValue) String() string { return fmt.Sprintf("0x%0x", *p.p) }
func (p *bitFieldValue) Get() interface{} { return *p.p }
func (p *bitFieldValue) Set(s string) error {
v, err := strconv.ParseBool(s)
if err != nil {
return err
}
p.set(v)
return nil
}
func (p *bitFieldValue) set(v bool) {
if v {
*p.p |= p.mask
}
}
type boolValue bool
func newBoolValue(val bool, p *bool) *boolValue {
*p = val
return (*boolValue)(p)
}
func (p *boolValue) IsBoolFlag() bool { return true }
func (p *boolValue) String() string { return strconv.FormatBool((bool)(*p)) }
func (p *boolValue) Get() interface{} { return (bool)(*p) }
func (p *boolValue) Set(s string) error {
v, err := strconv.ParseBool(s)
if err != nil {
return err
}
*p = boolValue(v)
return nil
}
type durationValue time.Duration
func newDurationValue(val time.Duration, p *time.Duration) *durationValue {
*p = val
return (*durationValue)(p)
}
func (p *durationValue) String() string { return (time.Duration)(*p).String() }
func (p *durationValue) Get() interface{} { return (time.Duration)(*p) }
func (p *durationValue) Set(s string) error {
v, err := time.ParseDuration(s)
if err != nil {
return err
}
*p = durationValue(v)
return nil
}
type float64Value float64
func newFloat64Value(val float64, p *float64) *float64Value {
*p = val
return (*float64Value)(p)
}
func (p *float64Value) String() string {
return strconv.FormatFloat((float64)(*p), 'e', -1, 64)
}
func (p *float64Value) Get() interface{} { return (float64)(*p) }
func (p *float64Value) Set(s string) error {
v, err := strconv.ParseFloat(s, 64)
if err != nil {
return err
}
*p = float64Value(v)
return nil
}
type funcValue func(string) error
func (f funcValue) Set(s string) error { return f(s) }
type intValue int
func newIntValue(val int, p *int) *intValue {
*p = val
return (*intValue)(p)
}
func (p *intValue) String() string {
return strconv.FormatInt((int64)(*p), 10)
}
func (p *intValue) Get() interface{} { return (int64)(*p) }
func (p *intValue) Set(s string) error {
v, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return err
}
*p = intValue(v)
return nil
}
type int64Value int64
func newInt64Value(val int64, p *int64) *int64Value {
*p = val
return (*int64Value)(p)
}
func (p *int64Value) String() string {
return strconv.FormatInt((int64)(*p), 10)
}
func (p *int64Value) Get() interface{} { return (int64)(*p) }
func (p *int64Value) Set(s string) error {
v, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return err
}
*p = int64Value(v)
return nil
}
type stringValue string
func newStringValue(val string, p *string) *stringValue {
*p = val
return (*stringValue)(p)
}
func (p *stringValue) String() string { return (string)(*p) }
func (p *stringValue) Get() interface{} { return (string)(*p) }
func (p *stringValue) Set(s string) error {
*p = stringValue(s)
return nil
}
type stringSliceValue struct {
p *[]string
hot bool
}
func newStringSliceValue(val []string, p *[]string) *stringSliceValue {
*p = val
return &stringSliceValue{p: p}
}
func (p *stringSliceValue) String() string {
return fmt.Sprintf("%v", *p.p)
}
func (p *stringSliceValue) Get() interface{} { return *p.p }
func (p *stringSliceValue) Set(s string) error {
if !p.hot {
*p.p = make([]string, 0, 1)
p.hot = true
}
*p.p = append(*p.p, s)
return nil
}
type uintValue uint
func newUintValue(val uint, p *uint) *uintValue {
*p = val
return (*uintValue)(p)
}
func (p *uintValue) String() string {
return strconv.FormatInt((int64)(*p), 10)
}
func (p *uintValue) Get() interface{} { return (int64)(*p) }
func (p *uintValue) Set(s string) error {
v, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return err
}
*p = uintValue(v)
return nil
}
type uint64Value uint64
func newUint64Value(val uint64, p *uint64) *uint64Value {
*p = val
return (*uint64Value)(p)
}
func (p *uint64Value) String() string {
return strconv.FormatInt((int64)(*p), 10)
}
func (p *uint64Value) Get() interface{} { return (int64)(*p) }
func (p *uint64Value) Set(s string) error {
v, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return err
}
*p = uint64Value(v)
return nil
}