This repository has been archived by the owner on Feb 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
struct.go
195 lines (164 loc) · 3.5 KB
/
struct.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
package flag
import (
"fmt"
"os"
"reflect"
"strconv"
"strings"
"time"
)
func parseFlags(s string) (f Flags) {
fs := strings.Split(s, "|")
for _, v := range fs {
switch v {
case "posix", "Posix":
f |= PosixShort
case "greedy", "Greedy":
f |= GreedyMode
case "notValue", "NotValue":
f |= NotValue
}
}
return
}
func parseByte(s string) (b byte, err error) {
switch s {
case `\a`:
b = '\a'
case `\b`:
b = '\b'
case `\e`:
b = '\x1B'
case `\f`:
b = '\f'
case `\n`:
b = '\n'
case `\r`:
b = '\r'
case `\v`:
b = '\v'
default:
var u64 uint64
switch {
case strings.HasPrefix(s, "x"):
if u64, err = strconv.ParseUint(s[1:], 16, 16); err != nil {
return 0, err
}
case strings.HasPrefix(s, "0"):
if u64, err = strconv.ParseUint(s[1:], 8, 16); err != nil {
return 0, err
}
default:
if u64, err = strconv.ParseUint(s[1:], 10, 16); err != nil {
return 0, err
}
}
b = byte(u64)
}
return
}
func parseDefValue(v reflect.Value, defValue string, sep string) (rv interface{}) {
var err error
switch v.Kind() {
case reflect.Slice:
if sep == "" {
sep = ","
}
switch v.Type() {
case stringSliceType:
rv = strings.Split(defValue, sep)
case int64SliceType:
rs := strings.Split(defValue, sep)
int64s := make([]int64, len(rs))
for k, v := range rs {
i64, err := strconv.ParseInt(v, 10, 0)
if err != nil {
panic(err.Error())
}
int64s[k] = i64
}
rv = int64s
default:
panic(fmt.Sprintf("unkown slice type:%v #support []stirng and []int64 types", v.Type()))
}
case reflect.Uint, reflect.Uint64:
n := uint64(0)
n, err = strconv.ParseUint(defValue, 10, 0)
rv = n
if v.Kind() == reflect.Uint {
rv = uint(n)
}
case reflect.Int:
rv, err = strconv.Atoi(defValue)
case reflect.Int64:
if v.Type() == durationType {
rv, err = time.ParseDuration(defValue)
} else {
rv, err = strconv.ParseInt(defValue, 10, 0)
}
case reflect.Float64:
rv, err = strconv.ParseFloat(defValue, 0)
case reflect.Bool:
rv, err = strconv.ParseBool(defValue)
case reflect.String:
rv = defValue
case reflect.Uint8:
if reflect.TypeOf(byte(0)) == v.Type() {
rv, err = parseByte(defValue)
} else {
panic("invalid type")
}
default:
panic("invalid type")
}
if err != nil {
panic(err.Error())
}
return
}
func (f *FlagSet) parseStruct(v reflect.Value) bool {
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
st := v.Type()
for i := 0; i < st.NumField(); i++ {
sf := st.Field(i)
if sf.PkgPath != "" && !sf.Anonymous {
continue
}
sv := v.Field(i)
if sv.Kind() == reflect.Struct {
f.parseStruct(sv)
continue
}
opt := sf.Tag.Get("opt")
usage := sf.Tag.Get("usage")
defValue := sf.Tag.Get("defValue")
flags := sf.Tag.Get("flags")
if opt == "" || usage == "" {
continue
}
if defValue != "" {
f.Opt(opt, usage).
Flags(parseFlags(flags)).
DefaultVar(sv.Addr().Interface(), parseDefValue(sv, defValue, sf.Tag.Get("sep")))
} else {
f.Opt(opt, usage).
Flags(parseFlags(flags)).
Var(sv.Addr().Interface())
}
}
return true
}
func (f *FlagSet) ParseStruct(arguments []string, s interface{}) error {
v := reflect.ValueOf(s)
if v.Kind() != reflect.Ptr || v.IsNil() || v.Elem().Kind() != reflect.Struct {
panic("The argument to the function must be a structure pointer")
}
f.parseStruct(v)
return f.Parse(arguments)
}
func ParseStruct(s interface{}) {
// Ignore errors; CommandLine is set for ExitOnError
CommandLine.ParseStruct(os.Args[1:], s)
}