-
Notifications
You must be signed in to change notification settings - Fork 15
/
parameter.go
272 lines (238 loc) · 6.59 KB
/
parameter.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
263
264
265
266
267
268
269
270
271
272
package clif
import (
"encoding/json"
"fmt"
"github.com/ukautz/reflekt"
"regexp"
// "strings"
"time"
)
/*
Options and Argument are parameters for commands.
Arguments are fixed positioned, meaning their order does matter. Options
command foo --bar baz -ding
*/
// SetupMethod is type for callback on Setup of Argument or Option. The return
// string replaces the original input. Called on each value (in case of multiple).
type ParseMethod func(name, value string) (string, error)
// parameter is core for Argument and Option
type parameter struct {
// Name is used for describing and accessing this argument
Name string
// Value holds what was provided on the command line
Values []string
// Usage is a short description of this argument
Usage string
// Descriptions is a lengthy elaboration of the purpose, use-case, life-story of this argument
Description string
// Required determines whether command can execute without this argument
// Should NOT be changed after adding with `AddCommand` from `Command`
Required bool
// Multiple decides whether multiple values are allowed.
Multiple bool
// Default is used if no value is provided
Default string
// Optional environment variable name, which is used in case not provided (overwrites
// any default)
Env string
// Parse is optional callback, which is applied on parameter values
// they are assigned. It can be used to validate, transform or otherwise
// utilize user provided inputs. Mind that inputs can be multiple and it
// will be called for each of those multiple inputs.
Parse ParseMethod
// Regex for checking if input value can be accepted
Regex *regexp.Regexp
}
/*
---------------------
SETTER
---------------------
*/
// Assign tries to add value to parameter and returns error if it fails due to invalid format or
// invalid amount (single vs multiple parameters)
func (this *parameter) Assign(val string) error {
if this.Values == nil {
this.Values = make([]string, 0)
}
l := len(this.Values)
if l > 0 && !this.Multiple {
return fmt.Errorf("Parameter \"%s\" does not support multiple values", this.Name)
} else {
print := func(m string) string {
return fmt.Sprintf("Parameter \"%s\" invalid: %s", this.Name, m)
}
if l > 1 {
print = func(m string) string {
return fmt.Sprintf("Parameter \"%s\" (%d) is invalid: %s", this.Name, l+2, m)
}
}
if this.Regex != nil && !this.Regex.MatchString(val) {
return fmt.Errorf(print("Does not match criteria"))
}
if this.Parse != nil {
if replace, err := this.Parse(this.Name, val); err != nil {
return fmt.Errorf(print(err.Error()))
} else {
val = replace
}
}
this.Values = append(this.Values, val)
return nil
}
}
/*
---------------------
GETTER
---------------------
*/
// Provided returns bool whether argument was provided
func (this *parameter) Provided() bool {
return this.Values != nil
}
// Provided returns amount of values provided
func (this *parameter) Count() int {
return len(this.Values)
}
// String representation of the value (can be empty string)
func (this *parameter) String() string {
if this.Values == nil {
return ""
} else {
return this.Values[0]
}
}
// Strings returns values as array of strings
func (this *parameter) Strings() []string {
return this.Values
}
// Int representation of the value (will be 0, if not given or not parsable)
func (this *parameter) Int() int {
if this.Values == nil {
return 0
} else {
return reflekt.AsInt(this.Values[0])
}
}
// Ints returns values as int array (values will be 0, if not parsable to int)
func (this *parameter) Ints() []int {
if this.Values == nil {
return nil
} else {
res := make([]int, this.Count())
for i, v := range this.Values {
res[i] = reflekt.AsInt(v)
}
return res
}
}
// Float representation of the value (will be 0.0, if not given or not parsable)
func (this *parameter) Float() float64 {
if this.Values == nil {
return 0
} else {
return reflekt.AsFloat(this.Values[0])
}
}
// Floats returns values as float64 array (values will be 0.0, if not parsable to float64)
func (this *parameter) Floats() []float64 {
if this.Values == nil {
return nil
} else {
res := make([]float64, this.Count())
for i, v := range this.Values {
res[i] = reflekt.AsFloat(v)
}
return res
}
}
// Bool representation of the value (will be false, if not given or not parsable)
func (this *parameter) Bool() bool {
if this.Values == nil {
return false
} else {
return reflekt.AsBool(this.Values[0])
}
}
// Bools returns values as bool array (values will be false, if not parsable to float64)
func (this *parameter) Bools() []bool {
if this.Values == nil {
return nil
} else {
res := make([]bool, this.Count())
for i, v := range this.Values {
res[i] = reflekt.AsBool(v)
}
return res
}
}
// Time is a date time representation of the value with a provided format.
// If no format is provided, then `2006-01-02 15:04:05` will be used
func (this *parameter) Time(format ...string) (*time.Time, error) {
if this.Values == nil {
return nil, nil
} else {
f := "2006-01-02 15:04:05"
if len(format) > 0 {
f = format[0]
}
if t, err := time.Parse(f, this.Values[0]); err != nil {
return nil, err
} else {
return &t, nil
}
}
}
// Times returns array for `time.Time` values, parsed from provided format.
// See `Time()`.
func (this *parameter) Times(format ...string) ([]time.Time, error) {
if this.Values == nil {
return nil, nil
} else {
f := "2006-01-02 15:04:05"
if len(format) > 0 {
f = format[0]
}
tt := make([]time.Time, len(this.Values))
for i, v := range this.Values {
if t, err := time.Parse(f, v); err != nil {
return nil, err
} else {
tt[i] = t
}
}
return tt, nil
}
}
// Json assumes the input is a JSON string and parses into a standard map[string]interface{}
// Returns error, if not parsable (or eg array JSON).
//
// Helpful to allow complex inputs: `my-app do --foo '{"bar": "baz"}'`
func (this *parameter) Json() (map[string]interface{}, error) {
if this.Values == nil {
return nil, nil
} else {
m := make(map[string]interface{})
if err := json.Unmarshal([]byte(this.Values[0]), &m); err != nil {
return nil, err
} else {
return m, nil
}
}
}
// Jsons returns values as individual JSON strings. See `Json()` above.
func (this *parameter) Jsons() ([]map[string]interface{}, error) {
if this.Values == nil {
return nil, nil
} else {
res := make([]map[string]interface{}, len(this.Values))
for i, v := range this.Values {
m := make(map[string]interface{})
if err := json.Unmarshal([]byte(v), &m); err != nil {
return nil, err
} else {
res[i] = m
}
}
return res, nil
}
}