forked from jessevdk/go-flags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
group_private.go
140 lines (105 loc) · 2.58 KB
/
group_private.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
package flags
import (
"reflect"
"unicode/utf8"
)
func (option *Option) canArgument() bool {
if option.isBool() {
return false
}
if option.isFunc() {
return (option.value.Type().NumIn() > 0)
}
return true
}
func (option *Option) isBool() bool {
tp := option.value.Type()
switch tp.Kind() {
case reflect.Bool:
return true
case reflect.Slice:
return (tp.Elem().Kind() == reflect.Bool)
}
return false
}
func (option *Option) isFunc() bool {
return option.value.Type().Kind() == reflect.Func
}
func (option *Option) call(value *string) error {
var retval []reflect.Value
if value == nil {
retval = option.value.Call(nil)
} else {
tp := option.value.Type().In(0)
val := reflect.New(tp)
val = reflect.Indirect(val)
if err := convert(*value, val, option.options); err != nil {
return err
}
retval = option.value.Call([]reflect.Value{val})
}
if len(retval) == 1 && retval[0].Type() == reflect.TypeOf((*error)(nil)).Elem() {
return retval[0].Interface().(error)
}
return nil
}
func (g *Group) scan() error {
// Get all the public fields in the data struct
ptrval := reflect.ValueOf(g.data)
if ptrval.Type().Kind() != reflect.Ptr {
panic(ErrNotPointerToStruct)
}
stype := ptrval.Type().Elem()
if stype.Kind() != reflect.Struct {
panic(ErrNotPointerToStruct)
}
realval := reflect.Indirect(ptrval)
for i := 0; i < stype.NumField(); i++ {
field := stype.Field(i)
// PkgName is set only for non-exported fields, which we ignore
if field.PkgPath != "" {
continue
}
// Skip anonymous fields.
// TODO: would be useful to support
if field.Anonymous {
continue
}
// Skip fields with the no-flag tag
if field.Tag.Get("no-flag") != "" {
continue
}
longname := field.Tag.Get("long")
shortname := field.Tag.Get("short")
if longname == "" && shortname == "" {
continue
}
short := rune(0)
rc := utf8.RuneCountInString(shortname)
if rc > 1 {
return ErrShortNameTooLong
} else if rc == 1 {
short, _ = utf8.DecodeRuneInString(shortname)
}
description := field.Tag.Get("description")
def := field.Tag.Get("default")
optional := (field.Tag.Get("optional") != "")
option := &Option{
Description: description,
ShortName: short,
LongName: longname,
Default: def,
OptionalArgument: optional,
value: realval.Field(i),
options: field.Tag,
}
g.Options = append(g.Options, option)
if option.ShortName != 0 {
g.ShortNames[option.ShortName] = option
}
if option.LongName != "" {
g.LongNames[option.LongName] = option
}
}
return nil
}