-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalue.go
More file actions
249 lines (223 loc) · 4.86 KB
/
Copy pathvalue.go
File metadata and controls
249 lines (223 loc) · 4.86 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
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
package applause
import (
"strconv"
"strings"
"time"
)
// ValueType represents the expected type of an argument value
type ValueType int
const (
StringType ValueType = iota
IntType
BoolType
Float64Type
DurationType
)
// String returns the string representation of the ValueType
func (vt ValueType) String() string {
switch vt {
case StringType:
return "string"
case IntType:
return "int"
case BoolType:
return "bool"
case Float64Type:
return "float64"
case DurationType:
return "duration"
default:
return "unknown"
}
}
// Value represents a typed command-line argument value
type Value struct {
raw string
valueType ValueType
parsed interface{}
valid bool
err error
}
// NewValue creates a new Value from a raw string with the specified type
func NewValue(raw string, valueType ValueType) *Value {
v := &Value{
raw: raw,
valueType: valueType,
}
v.parse()
return v
}
// NewStringValue creates a new string Value
func NewStringValue(raw string) *Value {
return NewValue(raw, StringType)
}
// NewIntValue creates a new int Value
func NewIntValue(raw string) *Value {
return NewValue(raw, IntType)
}
// NewBoolValue creates a new bool Value
func NewBoolValue(raw string) *Value {
return NewValue(raw, BoolType)
}
// NewFloat64Value creates a new float64 Value
func NewFloat64Value(raw string) *Value {
return NewValue(raw, Float64Type)
}
// NewDurationValue creates a new duration Value
func NewDurationValue(raw string) *Value {
return NewValue(raw, DurationType)
}
// parse converts the raw string to the appropriate type
func (v *Value) parse() {
switch v.valueType {
case StringType:
v.parsed = v.raw
v.valid = true
case IntType:
if parsed, err := strconv.Atoi(v.raw); err != nil {
v.err = &ParseError{
Type: "int",
Value: v.raw,
Original: err,
}
v.valid = false
} else {
v.parsed = parsed
v.valid = true
}
case BoolType:
// Handle various boolean representations
lower := strings.ToLower(v.raw)
switch lower {
case "true", "t", "yes", "y", "1", "on":
v.parsed = true
v.valid = true
case "false", "f", "no", "n", "0", "off":
v.parsed = false
v.valid = true
case "": // For flag-only arguments (presence = true)
v.parsed = true
v.valid = true
default:
v.err = &ParseError{
Type: "bool",
Value: v.raw,
Original: nil,
}
v.valid = false
}
case Float64Type:
if parsed, err := strconv.ParseFloat(v.raw, 64); err != nil {
v.err = &ParseError{
Type: "float64",
Value: v.raw,
Original: err,
}
v.valid = false
} else {
v.parsed = parsed
v.valid = true
}
case DurationType:
if parsed, err := time.ParseDuration(v.raw); err != nil {
v.err = &ParseError{
Type: "duration",
Value: v.raw,
Original: err,
}
v.valid = false
} else {
v.parsed = parsed
v.valid = true
}
}
}
// IsValid returns true if the value was successfully parsed
func (v *Value) IsValid() bool {
return v.valid
}
// Error returns the parsing error if any
func (v *Value) Error() error {
return v.err
}
// Raw returns the original string value
func (v *Value) Raw() string {
return v.raw
}
// Type returns the expected type of this value
func (v *Value) Type() ValueType {
return v.valueType
}
// String returns the string representation of the value
func (v *Value) String() (string, error) {
if !v.valid {
return "", v.err
}
if v.valueType == StringType {
return v.parsed.(string), nil
}
return v.raw, nil
}
// Int returns the int representation of the value
func (v *Value) Int() (int, error) {
if !v.valid {
return 0, v.err
}
if v.valueType != IntType {
return 0, &TypeMismatchError{
Expected: IntType,
Actual: v.valueType,
Value: v.raw,
}
}
return v.parsed.(int), nil
}
// Bool returns the bool representation of the value
func (v *Value) Bool() (bool, error) {
if !v.valid {
return false, v.err
}
if v.valueType != BoolType {
return false, &TypeMismatchError{
Expected: BoolType,
Actual: v.valueType,
Value: v.raw,
}
}
return v.parsed.(bool), nil
}
// Float64 returns the float64 representation of the value
func (v *Value) Float64() (float64, error) {
if !v.valid {
return 0, v.err
}
if v.valueType != Float64Type {
return 0, &TypeMismatchError{
Expected: Float64Type,
Actual: v.valueType,
Value: v.raw,
}
}
return v.parsed.(float64), nil
}
// Duration returns the duration representation of the value
func (v *Value) Duration() (time.Duration, error) {
if !v.valid {
return 0, v.err
}
if v.valueType != DurationType {
return 0, &TypeMismatchError{
Expected: DurationType,
Actual: v.valueType,
Value: v.raw,
}
}
return v.parsed.(time.Duration), nil
}
// Interface returns the parsed value as an interface{}
func (v *Value) Interface() (interface{}, error) {
if !v.valid {
return nil, v.err
}
return v.parsed, nil
}