-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalues.go
More file actions
188 lines (156 loc) · 4.52 KB
/
values.go
File metadata and controls
188 lines (156 loc) · 4.52 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
package flags
import (
"fmt"
"strconv"
)
// BoolValue represents a boolean argument value.
type BoolValue bool
// NewBoolValue creates a new BoolValue.
func NewBoolValue(init bool) *BoolValue {
p := new(bool)
*p = init
return (*BoolValue)(p)
}
// Set will attempt to convert the given string to a value.
func (p *BoolValue) Set(s string) error {
v, err := strconv.ParseBool(s)
if err != nil {
return fmt.Errorf("`%s` cannot be interpreted as %T", s, v)
}
*p = BoolValue(v)
return nil
}
// String satisfies the fmt.Stringer interface.
func (p BoolValue) String() string {
return strconv.FormatBool(bool(p))
}
// IntValue represents a integer argument value.
type IntValue int
// NewIntValue creates a new IntValue.
func NewIntValue(init int) *IntValue {
p := new(int)
*p = init
return (*IntValue)(p)
}
// Set will attempt to convert the given string to a value.
func (p *IntValue) Set(s string) error {
v, err := strconv.Atoi(s)
if err != nil {
return fmt.Errorf("`%s` cannot be interpreted as %T", s, v)
}
*p = IntValue(v)
return nil
}
// String satisfies the fmt.Stringer interface.
func (p IntValue) String() string {
return strconv.Itoa(int(p))
}
// FloatValue represents a float argument value.
type FloatValue float64
// NewFloatValue creates a new FloatValue.
func NewFloatValue(init float64) *FloatValue {
p := new(float64)
*p = init
return (*FloatValue)(p)
}
// Set will attempt to convert the given string to a value.
func (p *FloatValue) Set(s string) error {
v, err := strconv.ParseFloat(s, 64)
if err != nil {
return fmt.Errorf("`%s` cannot be interpreted as %T", s, v)
}
*p = FloatValue(v)
return nil
}
// String satisfies the fmt.Stringer interface.
func (p FloatValue) String() string {
return strconv.FormatFloat(float64(p), 'g', -1, 64)
}
// StringValue represents a string argument value.
type StringValue string
// NewStringValue creates a new StringValue.
func NewStringValue(init string) *StringValue {
p := new(string)
*p = init
return (*StringValue)(p)
}
// Set will attempt to convert the given string to a value.
func (p *StringValue) Set(s string) error {
*p = StringValue(s)
return nil
}
// String satisfies the fmt.Stringer interface.
func (p StringValue) String() string {
return string(p)
}
// IntSliceValue represents a variable number int argument value.
type IntSliceValue []int
// NewIntSliceValue creates a new IntSliceValue.
func NewIntSliceValue(init []int) *IntSliceValue {
p := new([]int)
*p = init
return (*IntSliceValue)(p)
}
// Len will return the length of the slice value.
func (p IntSliceValue) Len() int { return len(p) }
// Set will attempt to convert and append the given string to the slice.
func (p *IntSliceValue) Set(s string) error {
ii := []int(*p)
v, err := strconv.Atoi(s)
if err != nil {
return fmt.Errorf("`%s` cannot be interpreted as %T", s, p)
}
ii = append(ii, v)
*p = IntSliceValue(ii)
return nil
}
// String satisfies the fmt.Stringer interface.
func (p IntSliceValue) String() string {
return fmt.Sprintf("%v", []int(p))
}
// FloatSliceValue represents a variable number float argument value.
type FloatSliceValue []float64
// NewFloatSliceValue creates a new FloatSliceValue.
func NewFloatSliceValue(init []float64) *FloatSliceValue {
p := new([]float64)
*p = init
return (*FloatSliceValue)(p)
}
// Len will return the length of the slice value.
func (p FloatSliceValue) Len() int { return len(p) }
// Set will attempt to convert and append the given string to the slice.
func (p *FloatSliceValue) Set(s string) error {
ff := []float64(*p)
v, err := strconv.ParseFloat(s, 64)
if err != nil {
return fmt.Errorf("`%s` cannot be interpreted as %T", s, v)
}
ff = append(ff, v)
*p = FloatSliceValue(ff)
return nil
}
// String satisfies the fmt.Stringer interface.
func (p FloatSliceValue) String() string {
return fmt.Sprintf("%v", []float64(p))
}
// StringSliceValue represents a variable number string argument value.
type StringSliceValue []string
// NewStringSliceValue creates a new StringSliceValue.
func NewStringSliceValue(init []string) *StringSliceValue {
p := new([]string)
*p = init
return (*StringSliceValue)(p)
}
// Len will return the length of the slice value.
func (p StringSliceValue) Len() int { return len(p) }
// Set will attempt to convert and append the given string to the slice.
func (p *StringSliceValue) Set(s string) error {
ss := []string(*p)
ss = append(ss, s)
*p = StringSliceValue(ss)
return nil
}
// String satisfies the fmt.Stringer interface.
func (p StringSliceValue) String() string {
return fmt.Sprintf("%v", []string(p))
}