-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharg.go
261 lines (213 loc) · 5.13 KB
/
arg.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
package cligobrr
import "slices"
import "strings"
import "strconv"
type IArg interface {
GetKind() string
GetName() string
GetAlias() string
GetDescription() string
GetSeparator() string
GetMultiple() bool
GetRequired() bool
GetDefault() string
GetChoices() []string
AsBool() (bool, error)
AsBools() ([]bool, error)
AsFloat() (float64, error)
AsFloats() ([]float64, error)
AsInt() (int64, error)
AsInts() ([]int64, error)
AsString() (string, error)
AsStrings() ([]string, error)
Validate() error
Parse(string)
Store([]string)
Stored() []string
}
type ArgFields struct {
Name string
Alias string
Description string
Separator string
Multiple bool
Required bool
Default string
Choices []string
}
type Arg struct {
ArgFields
kind string
values []string
}
func argNew(fields ArgFields) (*Arg, error) {
fields.Name = strings.TrimSpace(fields.Name)
if len(fields.Name) == 0 {
return nil, errNameRequired()
}
// Make sure everything is nice and tidy.
fields.Alias = strings.TrimSpace(fields.Alias)
fields.Description = strings.TrimSpace(fields.Description)
fields.Separator = strings.TrimSpace(fields.Separator)
fields.Default = strings.TrimSpace(fields.Default)
if len(fields.Choices) > 0 {
var choices []string
for _, choice := range fields.Choices {
choice = strings.TrimSpace(choice)
if len(choice) > 0 {
choices = append(choices, choice)
}
}
fields.Choices = choices
}
if fields.Multiple || len(fields.Choices) > 0 {
if len(fields.Separator) == 0 {
fields.Separator = separatorDefault
}
} else {
fields.Separator = separatorNone
}
if len(fields.Default) > 0 && len(fields.Choices) > 0 {
if !slices.Contains(fields.Choices, fields.Default) {
return nil, errDefaultNotAValidChoice(fields.Name)
}
}
arg := Arg{
ArgFields: fields,
kind: kindUndefined,
}
return &arg, nil
}
func (self *Arg) GetKind() string {
return self.kind
}
func (self *Arg) GetName() string {
return self.Name
}
func (self *Arg) GetAlias() string {
return self.Alias
}
func (self *Arg) GetDescription() string {
return self.Description
}
func (self *Arg) GetSeparator() string {
return self.Separator
}
func (self *Arg) GetMultiple() bool {
return self.Multiple
}
func (self *Arg) GetRequired() bool {
return self.Required
}
func (self *Arg) GetDefault() string {
return self.Default
}
func (self *Arg) GetChoices() []string {
return self.Choices
}
func (self *Arg) Store(values []string) {
self.values = values
}
func (self *Arg) Stored() []string {
return self.values
}
func (self *Arg) Validate() error {
// To validate means that each stored valued is
// checked against a rule to make sure it is valid
// input. For the base Arg, anything is valid because
// each sub-type must define what is and is not valid.
return nil
}
func (self *Arg) Parse(input string) {
var vals []string
if self.Multiple {
vals = strings.Split(input, self.Separator)
} else {
vals = []string{input}
}
// Make sure we start with empty values in the event
// that Parse gets called repeatedly (like in testing).
newValues := []string{}
for _, val := range vals {
val = strings.TrimSpace(val)
if len(val) > 0 {
newValues = append(newValues, val)
}
}
self.Store(newValues)
}
func (self *Arg) AsBool() (bool, error) {
stored := self.Stored()
if len(stored) == 0 {
return false, errArgHasNoValues(self.GetName())
}
truthy := slices.Contains(truthyValues(), stored[0])
return truthy, nil
}
func (self *Arg) AsBools() ([]bool, error) {
stored := self.Stored()
if len(stored) == 0 {
return nil, errArgHasNoValues(self.GetName())
}
var truthy []bool
for _, val := range stored {
truthy = append(truthy, slices.Contains(truthyValues(), val))
}
return truthy, nil
}
func (self *Arg) AsFloat() (float64, error) {
stored := self.Stored()
if len(stored) == 0 {
return 0.0, errArgHasNoValues(self.GetName())
}
return strconv.ParseFloat(stored[0], 64)
}
func (self *Arg) AsFloats() ([]float64, error) {
stored := self.Stored()
if len(stored) == 0 {
return nil, errArgHasNoValues(self.GetName())
}
var vals []float64
for _, val := range stored {
// Values have already been validated,
// so err can be ignored.
v, _ := strconv.ParseFloat(val, 64)
vals = append(vals, v)
}
return vals, nil
}
func (self *Arg) AsInt() (int64, error) {
stored := self.Stored()
if len(stored) == 0 {
return 0, errArgHasNoValues(self.GetName())
}
return strconv.ParseInt(stored[0], 10, 64)
}
func (self *Arg) AsInts() ([]int64, error) {
stored := self.Stored()
if len(stored) == 0 {
return nil, errArgHasNoValues(self.GetName())
}
var vals []int64
for _, val := range stored {
// Values have already been validated,
// so err can be ignored.
v, _ := strconv.ParseInt(val, 10, 64)
vals = append(vals, v)
}
return vals, nil
}
func (self *Arg) AsString() (string, error) {
stored := self.Stored()
if len(stored) == 0 {
return "", errArgHasNoValues(self.GetName())
}
return stored[0], nil
}
func (self *Arg) AsStrings() ([]string, error) {
stored := self.Stored()
if len(stored) == 0 {
return nil, errArgHasNoValues(self.GetName())
}
return stored, nil
}