forked from multimfi/archivegen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag.go
55 lines (47 loc) · 921 Bytes
/
flag.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
package main
import (
"flag"
"reflect"
"strings"
"github.com/tlahdekorpi/archivegen/config"
)
func set(name, desc string, v reflect.Value) {
switch x := v.Interface().(type) {
case *int:
flag.IntVar(x, name, *x, desc)
case *string:
flag.StringVar(x, name, *x, desc)
case *bool:
flag.BoolVar(x, name, *x, desc)
case *config.PathVar:
flag.Var(x, name, desc)
default:
panic("flag")
}
}
func buildflags(opts interface{}, prefix string) {
y := reflect.ValueOf(opts).Elem()
if y.Kind() != reflect.Struct {
return
}
t := y.Type()
for i := 0; i < y.NumField(); i++ {
if !y.Field(i).CanSet() {
continue
}
f := t.Field(i)
n, ok := f.Tag.Lookup("flag")
if !ok {
n = prefix + strings.ToLower(f.Name)
}
if f.Type.Kind() == reflect.Struct {
buildflags(y.Field(i).Addr().Interface(), n+".")
} else {
set(
n,
f.Tag.Get("desc"),
y.Field(i).Addr(),
)
}
}
}