/
flagged.go
485 lines (440 loc) · 12.1 KB
/
flagged.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// `flagged` utilizes struct-tags to register flags. It can default the variable explicitly
// based on the `value:` struct-tag as well as from an environment variable based on the
// `env:` struct-tag. If there isn't at least a `usage:` struct-tag, the element is ignored.
// The flag can be named explicitly using the `flag:` struct-tag.
package flagged
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"reflect"
"regexp"
"strconv"
"strings"
"sync"
"unsafe"
)
var (
// Show some detailed output about the registered flags.
FlaggedDebugging = false
// A global prefix.
Prefix = ""
// The separator to use.
Separator = "."
)
var selfz, selfd, selfu = os.Args[0], "", ""
func init() {
selfd, selfu = "", filepath.Base(selfz)
if dir, _ := filepath.Split(selfz); dir == "" {
if path, err := exec.LookPath(selfz); err != nil {
log.Println(err)
} else {
selfd = filepath.Base(path)
}
} else if ln, err := filepath.EvalSymlinks(selfz); err != nil {
log.Println(err)
} else {
if dir, err := filepath.Abs(filepath.Dir(ln)); err != nil {
log.Println(err)
} else {
selfd = dir
}
}
}
// Returns the executable's basename and absolute path.
func Program() (string, string) {
return selfu, selfd
}
type options uint8
const (
// Pass flagged.Continue to flagged.Parse functions if you want to register different structs.
// Not passing Continue will cause the flagged.Parse functions to call flag.Parse, meaning that
// any subsequent calls to flagged.Parse will not have any impact.
Continue options = iota
)
//
type environment map[string]string
//
func (env environment) get(key string, def string) string {
if item, ok := env[key]; ok {
return item
} else {
return def
}
}
//
func getenvironment(data []string, getkeyval func(item string) (key, val string)) environment {
environment := make(map[string]string)
for _, item := range data {
key, val := getkeyval(item)
environment[key] = val
}
return environment
}
//
var (
re_deCamel = regexp.MustCompile(`([A-Z])`)
Usage = usage()
internal func()
)
//
func usage() func() {
var once sync.Once
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: %[1]s [flags]\n", selfu)
fmt.Fprintf(os.Stderr, "flags:\n")
flag.PrintDefaults()
}
return func() {
once.Do(func() {
if internal == nil {
internal = flag.Usage
}
flag.Usage = internal
})
internal()
}
}
// Pass a pointer to the flagged-tagged struct and Parse will register all the flags.
// Given a tagged struct:
//
// var setting struct {
// aString string `usage:"A String."`
// }
//
// Register the flags and view the usage:
//
// flagged.Parse(&setting)
func Parse(value_ interface{}, options_ ...options) {
ParseWithPrefix(value_, Prefix, options_...)
}
//
func unpack(s []string, vars ...positional) error {
i, ls, lv := 0, len(s), len(vars)
for ; i < lv; i++ {
param := vars[i]
value := ""
if i < ls {
value = s[i]
} else if param.optional {
value = param.value
} else {
return fmt.Errorf("positional parameter %d, %s, is not optional, has no default, and was not provided", i+1, param.name)
continue
}
if param.parameter != nil {
v := param
switch p := (v.parameter).(type) {
case *string:
*p = value
case *float64:
if f, err := strconv.ParseFloat(value, 64); err != nil {
return fmt.Errorf("positional parameter %d is not of type %s: %+v", i+1, v.type_, value)
} else {
*p = f
}
case *int64, *uint64, *int, *uint:
var default_ interface{}
switch p.(type) {
case *int64, *int:
if i, err := strconv.ParseInt(value, 10, 64); err == nil {
default_ = i
}
case *uint64, *uint:
if i, err := strconv.ParseUint(value, 10, 64); err == nil {
default_ = i
}
}
if default_ == nil {
return fmt.Errorf("positional parameter %d is not of type %s: %+v", i+1, v.type_, value)
}
switch p := p.(type) {
case *int64:
*p = default_.(int64)
case *int:
*p = int(default_.(int64))
case *uint64:
*p = default_.(uint64)
case *uint:
*p = uint(default_.(uint64))
}
}
}
}
// TODO support a slice as the last _positional so that the usage can indicate "...".
// if i < ls {
// fmt.Fprintf(os.Stderr, "Additional positional arguments ignored: %+v\n", s[i:])
// }
return nil
}
// Pass a pointer to the flagged-tagged struct and a prefix string and ParseWithPrefix will
// register all the flags prefixed with `prefix`.
func ParseWithPrefix(value_ interface{}, prefix string, options_ ...options) {
// Just so that this package does not keep the environment itself.
var env = getenvironment(os.Environ(), func(item string) (key, val string) {
splits := strings.Split(item, "=")
key = splits[0]
val = splits[1]
return
})
positionals := parser(value_, prefix, parental{}, env)
parse := true
for _, o := range options_ {
switch o {
case Continue:
parse = false
}
}
if parse {
if positionals == nil || len(positionals) == 0 {
flag.Parse()
} else {
sp := ""
oa := []string{}
for _, p := range positionals {
if !p.optional {
sp = fmt.Sprintf("%s %s", sp, p.name)
} else {
oa = append([]string{p.name}, oa...)
}
}
so := ""
for _, p := range oa {
so = fmt.Sprintf(" [%s%s]", p, so)
}
internal = func() {
fmt.Fprintf(os.Stderr, "usage: %[1]s [flags]%s%s\n", selfu, sp, so)
fmt.Fprintf(os.Stderr, "positional:\n")
for _, p := range positionals {
fmt.Fprintf(os.Stderr, " %s", p.name)
if p.optional && p.value != "" {
switch p.parameter.(type) {
case *string:
fmt.Fprintf(os.Stderr, `="%s"`, p.value)
default:
fmt.Fprintf(os.Stderr, `=%s`, p.value)
}
}
fmt.Fprintf(os.Stderr, " (%s)", p.type_)
fmt.Fprintf(os.Stderr, " - %s\n", p.usage)
}
fmt.Fprintf(os.Stderr, "flags:\n")
flag.PrintDefaults()
}
flag.Parse()
if err := unpack(flag.Args(), positionals...); err != nil {
fmt.Fprintln(os.Stderr, err)
if Usage != nil {
Usage()
os.Exit(1)
}
}
}
}
}
// TODO optional can probably be enhanced to support optional based on type.
type positional struct {
name, value, usage string
parameter interface{}
optional bool // Once a positional is optional, all subsequent are optional.
type_ reflect.Kind
}
//
type parental struct {
name, usage string
positional bool
}
//
func parser(value_ interface{}, prefix string, parent parental, env environment) (ps []positional) {
// defer func() { debug.PrintStack() }()
switch t := value_.(type) {
case reflect.Value:
switch t.Kind() {
case reflect.Ptr:
ps = parser(t.Elem(), prefix, parent, env)
case reflect.Struct:
optional := false
for f, fs := 0, t.NumField(); f < fs; f++ {
field := t.Field(f)
switch kind := field.Kind(); kind {
case reflect.Ptr:
case reflect.Struct:
t_ := t.Type()
f_ := t_.Field(f)
name := strings.ToLower(prefix)
if name == "" {
name = f_.Name
} else {
name = fmt.Sprintf("%s%s%s", name, Separator, f_.Name)
}
if parent.positional {
log.Printf("WARNING structs within _positional is unsupported: %s", name)
return
}
p := parental{
name: fmt.Sprintf("%s.%s", parent.name, f_.Name),
usage: fmt.Sprintf("%s%s", parent.usage, f_.Tag.Get("usage")),
positional: f_.Name == "_positional",
}
if ps_ := parser(field, strings.ToLower(name), p, env); p.positional && len(ps_) > 0 {
ps = ps_
}
default:
if field.CanAddr() {
t_ := t.Type()
f_ := t_.Field(f)
tag := f_.Tag
if tag == "" {
continue
}
usage := tag.Get("usage")
if usage == "" {
continue
}
name := tag.Get("flag")
value := tag.Get("value")
var p interface{}
switch kind {
case reflect.Bool:
p = (*bool)(unsafe.Pointer(field.Addr().Pointer()))
case reflect.String:
p = (*string)(unsafe.Pointer(field.Addr().Pointer()))
case reflect.Float64:
p = (*float64)(unsafe.Pointer(field.Addr().Pointer()))
case reflect.Int64:
p = (*int64)(unsafe.Pointer(field.Addr().Pointer()))
case reflect.Int:
p = (*int)(unsafe.Pointer(field.Addr().Pointer()))
case reflect.Uint64:
p = (*uint64)(unsafe.Pointer(field.Addr().Pointer()))
case reflect.Uint:
p = (*uint)(unsafe.Pointer(field.Addr().Pointer()))
}
if parent.positional {
optional = optional || tag.Get("value") != ""
if optional && value == "" {
switch p.(type) {
case *string:
case *float64:
value = "0.0"
default:
value = "0"
}
}
ps = append(ps, positional{
name: f_.Name,
value: value,
usage: usage,
type_: kind,
parameter: p,
optional: optional,
})
continue
}
envrionment_ := tag.Get("env")
if envrionment_ != "" {
value = env.get(envrionment_, value)
usage = fmt.Sprintf("%s (%s)", usage, envrionment_)
}
destination := fmt.Sprintf("%s.%s", parent.name, f_.Name)
aliased := ""
names := strings.Split(name, ",")
{
previous := ""
for i, name := range names {
name = strings.TrimSpace(name)
if name == "_" || (i == 0 && name == "") {
name = f_.Name
name = fmt.Sprintf("%s%s", strings.ToLower(name[:1]), strings.ToLower(re_deCamel.ReplaceAllString(name[1:], `.$1`)))
if prefix != "" {
name = fmt.Sprintf("%s%s%s", prefix, Separator, name)
}
} else if i > 0 && aliased == "" {
aliased = previous
}
if aliased == "" {
aliased = name
}
previous = name
names[i] = name
}
}
for _, name := range names {
if name == "" {
continue
}
description := usage
if aliased != "" && name != aliased {
description = fmt.Sprintf("alias for -%s: %s", aliased, usage)
} else {
description = fmt.Sprintf("%s%s", parent.usage, description)
}
var default_ interface{} = value
if err := func() (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("%+v", r)
}
}()
switch p := p.(type) {
case *bool:
default_ = value == "true"
flag.BoolVar(p, name, default_.(bool), description)
case *string:
flag.StringVar(p, name, value, description)
case *float64:
if f, err := strconv.ParseFloat(value, 64); err != nil {
default_ = field.Float()
} else {
default_ = f
}
flag.Float64Var(p, name, default_.(float64), description)
case *int64, *uint64, *int, *uint:
switch p.(type) {
case *int64, *int:
if i, err := strconv.ParseInt(value, 10, 64); err != nil {
default_ = field.Int()
} else {
default_ = i
}
case *uint64, *uint:
if i, err := strconv.ParseUint(value, 10, 64); err != nil {
default_ = field.Uint()
} else {
default_ = i
}
}
switch p := p.(type) {
case *int64:
flag.Int64Var(p, name, default_.(int64), description)
case *int:
flag.IntVar(p, name, int(default_.(int64)), description)
case *uint64:
flag.Uint64Var(p, name, default_.(uint64), description)
case *uint:
flag.UintVar(p, name, uint(default_.(uint64)), description)
}
default:
return fmt.Errorf("%T flags are not currently supported", field.Interface())
}
if FlaggedDebugging {
fmt.Printf("%8s -%-40s env:%-20s value:%-20v usage:%s (%s)\n", field.Kind(), name, envrionment_, default_, description, destination)
}
return nil
}(); err != nil {
log.Println(err)
continue
}
}
}
}
}
}
default:
ps = parser(reflect.ValueOf(value_), prefix, parent, env)
}
return
}