Debian Codegraph @

group_private.go

raw symbols SCIP
1 package flags
2
3 import (
4 "reflect"
5 "sort"
6 "strings"
7 "unicode/utf8"
8 "unsafe"
9 )
10
11 func (g *Group) lookupByName(name string, ini bool) (*Option, string) {
12 name = strings.ToLower(name)
13
14 if ini {
15 if ret := g.IniNames[name]; ret != nil {
16 return ret, ret.tag.Get("ini-name")
17 }
18
19 if ret := g.Names[name]; ret != nil {
20 return ret, ret.Field.Name
21 }
22 }
23
24 if ret := g.LongNames[name]; ret != nil {
25 return ret, ret.LongName
26 }
27
28 if utf8.RuneCountInString(name) == 1 {
29 r, _ := utf8.DecodeRuneInString(name)
30
31 if ret := g.ShortNames[r]; ret != nil {
32 data := make([]byte, utf8.RuneLen(ret.ShortName))
33 utf8.EncodeRune(data, ret.ShortName)
34
35 return ret, string(data)
36 }
37 }
38
39 return nil, ""
40 }
41
42 func (g *Group) storeDefaults() {
43 for _, option := range g.Options {
44 // First. empty out the value
45 if len(option.Default) > 0 {
46 option.clear()
47 }
48
49 for _, d := range option.Default {
50 option.Set(&d)
51 }
52
53 if !option.Value.CanSet() {
54 continue
55 }
56
57 option.defaultValue = reflect.ValueOf(option.Value.Interface())
58 }
59 }
60
61 func (g *Group) scanStruct(realval reflect.Value, sfield *reflect.StructField) error {
62 stype := realval.Type()
63
64 if sfield != nil {
65 mtag := newMultiTag(string(sfield.Tag))
66
67 groupName := mtag.Get("group")
68 commandName := mtag.Get("command")
69 name := mtag.Get("name")
70 description := mtag.Get("description")
71
72 iscommand := false
73
74 if len(commandName) != 0 {
75 iscommand = true
76
77 if len(name) != 0 {
78 groupName = name
79 } else if len(commandName) != 0 {
80 groupName = commandName
81 }
82 }
83
84 if len(groupName) != 0 {
85 ptrval := reflect.NewAt(realval.Type(), unsafe.Pointer(realval.UnsafeAddr()))
86 newGroup := NewGroup(groupName, ptrval.Interface())
87
88 if iscommand {
89 newGroup.IsCommand = true
90 g.Commands[commandName] = newGroup
91 }
92
93 newGroup.LongDescription = description
94
95 g.EmbeddedGroups = append(g.EmbeddedGroups, newGroup)
96 return g.Error
97 }
98 }
99
100 for i := 0; i < stype.NumField(); i++ {
101 field := stype.Field(i)
102
103 // PkgName is set only for non-exported fields, which we ignore
104 if field.PkgPath != "" {
105 continue
106 }
107
108 mtag := newMultiTag(string(field.Tag))
109
110 // Skip fields with the no-flag tag
111 if mtag.Get("no-flag") != "" {
112 continue
113 }
114
115 // Dive deep into structs or pointers to structs
116 kind := field.Type.Kind()
117
118 if kind == reflect.Struct {
119 err := g.scanStruct(realval.Field(i), &field)
120
121 if err != nil {
122 return err
123 }
124
125 } else if kind == reflect.Ptr &&
126 field.Type.Elem().Kind() == reflect.Struct &&
127 !realval.Field(i).IsNil() {
128 err := g.scanStruct(reflect.Indirect(realval.Field(i)),
129 &field)
130
131 if err != nil {
132 return err
133 }
134 }
135
136 longname := mtag.Get("long")
137 shortname := mtag.Get("short")
138
139 if longname == "" && shortname == "" {
140 continue
141 }
142
143 short := rune(0)
144 rc := utf8.RuneCountInString(shortname)
145
146 if rc > 1 {
147 return ErrShortNameTooLong
148 } else if rc == 1 {
149 short, _ = utf8.DecodeRuneInString(shortname)
150 }
151
152 description := mtag.Get("description")
153 def := mtag.GetMany("default")
154 optionalValue := mtag.GetMany("optional-value")
155 valueName := mtag.Get("value-name")
156 defaultMask := mtag.Get("default-mask")
157
158 optional := (mtag.Get("optional") != "")
159 required := (mtag.Get("required") != "")
160
161 option := &Option{
162 Description: description,
163 ShortName: short,
164 LongName: longname,
165 Default: def,
166 OptionalArgument: optional,
167 OptionalValue: optionalValue,
168 Required: required,
169 Field: field,
170 Value: realval.Field(i),
171 ValueName: valueName,
172 tag: mtag,
173 defaultMask: defaultMask,
174 }
175
176 g.Options = append(g.Options, option)
177
178 if option.ShortName != 0 {
179 g.ShortNames[option.ShortName] = option
180 }
181
182 if option.LongName != "" {
183 g.LongNames[strings.ToLower(option.LongName)] = option
184 }
185
186 g.Names[strings.ToLower(field.Name)] = option
187
188 ininame := mtag.Get("ini-name")
189
190 if len(ininame) != 0 {
191 g.IniNames[strings.ToLower(ininame)] = option
192 }
193 }
194
195 return nil
196 }
197
198 func (g *Group) each(index int, cb func(int, *Group)) int {
199 cb(index, g)
200 index++
201
202 for _, group := range g.EmbeddedGroups {
203 group.each(index, cb)
204 index++
205 }
206
207 return index
208 }
209
210 func (g *Group) eachCommand(cb func(string, *Group)) {
211 cmds := make([]string, len(g.Commands))
212 i := 0
213
214 for k, _ := range g.Commands {
215 cmds[i] = k
216 }
217
218 sort.Strings(cmds)
219
220 for _, k := range cmds {
221 cb(k, g.Commands[k])
222 }
223 }
224
225 func (g *Group) scan() error {
226 // Get all the public fields in the data struct
227 ptrval := reflect.ValueOf(g.data)
228
229 if ptrval.Type().Kind() != reflect.Ptr {
230 panic(ErrNotPointerToStruct)
231 }
232
233 stype := ptrval.Type().Elem()
234
235 if stype.Kind() != reflect.Struct {
236 panic(ErrNotPointerToStruct)
237 }
238
239 realval := reflect.Indirect(ptrval)
240 return g.scanStruct(realval, nil)
241 }