-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse-for.go
447 lines (416 loc) · 12.3 KB
/
parse-for.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// Copyright (C) 2023-2024 Takayuki Sato. All Rights Reserved.
// This program is free software under MIT License.
// See the file LICENSE in this distribution for more details.
package cliargs
import (
"reflect"
"strconv"
"strings"
"github.com/sttk/cliargs/errors"
)
// ParseFor is the method to parse command line arguments and set their values to the option store
// which is passed as an argument.
//
// This method divides command line arguments to command arguments and options, then sets the
// options to the option store.
//
// Within this method, a slice of OptCfg is made from the fields of the option store.
// This OptCfg array is set to the public field `OptCfgs` of this Cmd instance.
//
// The configurations of options are determined by types and struct tags of
// fields of the option store.
// If the type is bool, the option takes no argument.
// If the type is integer, floating point number or string, the option can
// takes single option argument, therefore it can appear once in command line
// arguments.
// If the type is an array, the option can takes multiple option arguments,
// therefore it can appear multiple times in command line arguments.
//
// A struct tag can be specified an option names and default value(s).
// It has a special format like `opt:foo-bar,f=123`.
// This opt: is the struct tag key for the option configuration.
// The string following this key and rounded by double quotes is the content of
// the option configuration.
// The first part of the option configuration is an option names, which are
// separated by commas, and ends with "=" mark or end of string.
// If the option name is empty or no struct tag, the option's name becomes same
// with the field name of the option store.
//
// The string after the "=" mark is default value(s).
// If the type of the option is a boolean, the string after "=" mark is ignored
// because a boolean option takes no option argument.
// If the type of the option is a number or a string, the whole string after
// "=" mark is a default value.
// If the type of the option is an array, the string after "=" mark have to be
// rounded by square brackets and separate the elements with commas, like
// [elem1,elem2,elem3].
// The element separator can be used other than a comma by putting the
// separator before the open square bracket, like :[elem1:elem2:elem3].
// It's useful when some array elements include commas.
//
// NOTE: A default value of an empty string array option in a struct tag is [],
// like `opt:"name=[]"`, it doesn't represent an array which contains only one
// empty string but an empty array.
// If you want to specify an array which contains only one empty string, write
// nothing after "=" mark, like `opt:"name="`.
func (cmd *Cmd) ParseFor(optStore any) error {
cfgs, err := MakeOptCfgsFor(optStore)
if err != nil {
cmd.OptCfgs = cfgs
return err
}
return cmd.ParseWith(cfgs)
}
// ParseUntilSubCmdFor is the method to parse command line arguments until the first command
// argument and set their option values to the option store which is passed as an argument.
//
// This method creates and returns a new Cmd instance that holds the command line arguments
// starting from the first command argument.
//
// This method parses command line arguments in the same way as the Cmd#parse_for method,
// except that it only parses the command line arguments before the first command argument.
func (cmd *Cmd) ParseUntilSubCmdFor(optStore any) (Cmd, error) {
cfgs, err := MakeOptCfgsFor(optStore)
if err != nil {
cmd.OptCfgs = cfgs
return Cmd{}, err
}
return cmd.ParseUntilSubCmdWith(cfgs)
}
// MakeOptCfgsFor is a function to make a OptCfg array from fields of the option store which is
// the argument of this function.
func MakeOptCfgsFor(options any) ([]OptCfg, error) {
v := reflect.ValueOf(options)
if v.Kind() != reflect.Ptr {
return nil, errors.OptionStoreIsNotChangeable{}
}
v = v.Elem()
t := v.Type()
n := t.NumField()
optCfgs := make([]OptCfg, n)
for i := 0; i < n; i++ {
optCfgs[i] = newOptCfg(t.Field(i))
var optName string
if len(optCfgs[i].Names) > 0 {
optName = optCfgs[i].Names[0]
} else {
optName = optCfgs[i].StoreKey
}
setter, err := newValueSetter(optName, t.Field(i).Name, v.Field(i))
if err != nil {
return nil, err
}
optCfgs[i].onParsed = &setter
}
return optCfgs, nil
}
func newOptCfg(fld reflect.StructField) OptCfg {
storeKey := fld.Name
opt := fld.Tag.Get("optcfg")
arr := strings.SplitN(opt, "=", 2)
names := strings.Split(arr[0], ",")
if len(names) == 0 || len(names[0]) == 0 {
names = []string{}
}
isArray := false
hasArg := true
switch fld.Type.Kind() {
case reflect.Slice | reflect.Array:
isArray = true
case reflect.Bool:
hasArg = false
}
var defaults []string
if len(arr) > 1 && hasArg {
def := arr[1]
n := len(def)
if !isArray {
defaults = []string{def}
} else if n > 1 && def[0] == '[' && def[n-1] == ']' {
defs := def[1 : n-1]
if len(defs) > 0 {
defaults = strings.Split(defs, ",")
} else {
defaults = empty
}
} else if n > 2 && def[1] == '[' && def[n-1] == ']' {
defs := def[2 : n-1]
if len(defs) > 0 {
defaults = strings.Split(defs, def[0:1])
} else {
defaults = empty
}
} else {
defaults = []string{def}
}
}
var optArg string
if hasArg {
optArg = fld.Tag.Get("optarg")
}
desc := fld.Tag.Get("optdesc")
return OptCfg{
StoreKey: storeKey,
Names: names,
HasArg: hasArg,
IsArray: isArray,
Defaults: defaults,
Desc: desc,
ArgInHelp: optArg,
}
}
func newValueSetter(
optName string,
fldName string,
fld reflect.Value,
) (func([]string) error, error) {
t := fld.Type()
switch t.Kind() {
case reflect.Bool:
return newBoolSetter(optName, fldName, fld)
case reflect.Int:
return newIntSetter(optName, fldName, fld, strconv.IntSize)
case reflect.Int8:
return newIntSetter(optName, fldName, fld, 8)
case reflect.Int16:
return newIntSetter(optName, fldName, fld, 16)
case reflect.Int32:
return newIntSetter(optName, fldName, fld, 32)
case reflect.Int64:
return newIntSetter(optName, fldName, fld, 64)
case reflect.Uint:
return newUintSetter(optName, fldName, fld, strconv.IntSize)
case reflect.Uint8:
return newUintSetter(optName, fldName, fld, 8)
case reflect.Uint16:
return newUintSetter(optName, fldName, fld, 16)
case reflect.Uint32:
return newUintSetter(optName, fldName, fld, 32)
case reflect.Uint64:
return newUintSetter(optName, fldName, fld, 64)
case reflect.Float32:
return newFloatSetter(optName, fldName, fld, 32)
case reflect.Float64:
return newFloatSetter(optName, fldName, fld, 64)
case reflect.Array | reflect.Slice:
elm := t.Elem()
switch elm.Kind() {
case reflect.Int:
return newIntArraySetter(optName, fldName, fld, strconv.IntSize)
case reflect.Int8:
return newIntArraySetter(optName, fldName, fld, 8)
case reflect.Int16:
return newIntArraySetter(optName, fldName, fld, 16)
case reflect.Int32:
return newIntArraySetter(optName, fldName, fld, 32)
case reflect.Int64:
return newIntArraySetter(optName, fldName, fld, 64)
case reflect.Uint:
return newUintArraySetter(optName, fldName, fld, strconv.IntSize)
case reflect.Uint8:
return newUintArraySetter(optName, fldName, fld, 8)
case reflect.Uint16:
return newUintArraySetter(optName, fldName, fld, 16)
case reflect.Uint32:
return newUintArraySetter(optName, fldName, fld, 32)
case reflect.Uint64:
return newUintArraySetter(optName, fldName, fld, 64)
case reflect.Float32:
return newFloatArraySetter(optName, fldName, fld, 32)
case reflect.Float64:
return newFloatArraySetter(optName, fldName, fld, 64)
case reflect.String:
return newStringArraySetter(optName, fldName, fld)
default:
return newBadFieldTypeError(optName, fldName, t)
}
case reflect.String:
return newStringSetter(optName, fldName, fld)
default:
return newBadFieldTypeError(optName, fldName, t)
}
}
func newBadFieldTypeError(
optName string, fldName string, t reflect.Type,
) (func([]string) error, error) {
e := errors.BadFieldType{Option: optName, Field: fldName, Type: t}
return nil, e
}
func newBoolSetter(
optName string, fldName string, fld reflect.Value,
) (func([]string) error, error) {
fn := func(s []string) error {
fld.SetBool(true)
return nil
}
return fn, nil
}
func newIntSetter(
optName string, fldName string, fld reflect.Value, bitSize int,
) (func([]string) error, error) {
fn := func(s []string) error {
if len(s) == 0 {
return nil
}
n, e := strconv.ParseInt(s[0], 0, bitSize)
if e != nil {
return errors.OptionArgIsInvalid{
Option: optName, StoreKey: fldName, OptArg: s[0], TypeKind: fld.Type().Kind(), Cause: e}
}
fld.SetInt(n)
return nil
}
return fn, nil
}
func newUintSetter(
optName string, fldName string, fld reflect.Value, bitSize int,
) (func([]string) error, error) {
fn := func(s []string) error {
if len(s) == 0 {
return nil
}
n, e := strconv.ParseUint(s[0], 0, bitSize)
if e != nil {
return errors.OptionArgIsInvalid{
Option: optName, StoreKey: fldName, OptArg: s[0], TypeKind: fld.Type().Kind(), Cause: e}
}
fld.SetUint(n)
return nil
}
return fn, nil
}
func newFloatSetter(
optName string, fldName string, fld reflect.Value, bitSize int,
) (func([]string) error, error) {
fn := func(s []string) error {
if len(s) == 0 {
return nil
}
n, e := strconv.ParseFloat(s[0], bitSize)
if e != nil {
return errors.OptionArgIsInvalid{
Option: optName, StoreKey: fldName, OptArg: s[0], TypeKind: fld.Type().Kind(), Cause: e}
}
fld.SetFloat(n)
return nil
}
return fn, nil
}
func newStringSetter(
optName string, fldName string, fld reflect.Value,
) (func([]string) error, error) {
fn := func(s []string) error {
if len(s) == 0 {
return nil
}
fld.SetString(s[0])
return nil
}
return fn, nil
}
func newIntArraySetter(
optName string, fldName string, fld reflect.Value, bitSize int,
) (func([]string) error, error) {
fn := func(s []string) error {
if s == nil {
return nil
}
emp := reflect.MakeSlice(fld.Type(), 0, 0)
n := len(s)
if n == 0 {
fld.Set(emp)
return nil
}
t := fld.Type().Elem()
a := make([]reflect.Value, n)
for i := 0; i < n; i++ {
v, e := strconv.ParseInt(s[i], 0, bitSize)
if e != nil {
return errors.OptionArgIsInvalid{
Option: optName, StoreKey: fldName, OptArg: s[i], TypeKind: t.Kind(), Cause: e}
}
a[i] = reflect.ValueOf(v).Convert(t)
}
fld.Set(reflect.Append(emp, a...))
return nil
}
return fn, nil
}
func newUintArraySetter(
optName string, fldName string, fld reflect.Value, bitSize int,
) (func([]string) error, error) {
fn := func(s []string) error {
if s == nil {
return nil
}
emp := reflect.MakeSlice(fld.Type(), 0, 0)
n := len(s)
if n == 0 { // If "=[]" then n==0, else if "=" then n==1 and s[0]=""
fld.Set(emp)
return nil
}
t := fld.Type().Elem()
a := make([]reflect.Value, n)
for i := 0; i < n; i++ {
v, e := strconv.ParseUint(s[i], 0, bitSize)
if e != nil {
return errors.OptionArgIsInvalid{
Option: optName, StoreKey: fldName, OptArg: s[i], TypeKind: t.Kind(), Cause: e}
}
a[i] = reflect.ValueOf(v).Convert(t)
}
fld.Set(reflect.Append(emp, a...))
return nil
}
return fn, nil
}
func newFloatArraySetter(
optName string, fldName string, fld reflect.Value, bitSize int,
) (func([]string) error, error) {
fn := func(s []string) error {
if s == nil {
return nil
}
emp := reflect.MakeSlice(fld.Type(), 0, 0)
n := len(s)
if n == 0 { // If "=[]" then n==0, else if "=" then n==1 and s[0]=""
fld.Set(emp)
return nil
}
t := fld.Type().Elem()
a := make([]reflect.Value, n)
for i := 0; i < n; i++ {
v, e := strconv.ParseFloat(s[i], bitSize)
if e != nil {
return errors.OptionArgIsInvalid{
Option: optName, StoreKey: fldName, OptArg: s[i], TypeKind: t.Kind(), Cause: e}
}
a[i] = reflect.ValueOf(v).Convert(t)
}
fld.Set(reflect.Append(emp, a...))
return nil
}
return fn, nil
}
func newStringArraySetter(
optName string, fldName string, fld reflect.Value,
) (func([]string) error, error) {
fn := func(s []string) error {
if s == nil {
return nil
}
emp := reflect.MakeSlice(fld.Type(), 0, 0)
n := len(s)
if n == 0 { // If "=[]" then n==0, else if "=" then n==1 and s[0]=""
fld.Set(emp)
return nil
}
a := make([]reflect.Value, n)
for i := 0; i < n; i++ {
a[i] = reflect.ValueOf(s[i])
}
fld.Set(reflect.Append(emp, a...))
return nil
}
return fn, nil
}