-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser.go
274 lines (250 loc) · 5.89 KB
/
parser.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
package tagflag
import (
"fmt"
"reflect"
"strings"
"github.com/anacrolix/missinggo/v2/slices"
"github.com/huandu/xstrings"
"golang.org/x/xerrors"
)
type Parser struct {
// The value from which the Parser is built, and values are assigned.
cmd interface{}
// Disables the default handling of -h and -help.
noDefaultHelp bool
program string
description string
// Whether the first non-option argument requires that all further arguments are to be treated
// as positional.
parseIntermixed bool
// The Parser that preceded this one, such as in sub-command relationship.
parent *Parser
posArgs []arg
// Maps -K=V to map[K]arg(V)
flags map[string]arg
excess *ExcessArgs
// Count of positional arguments parsed so far. Used to locate the next
// positional argument where it's non-trivial (non-unity arity).
numPos int
}
func (p *Parser) hasOptions() bool {
return len(p.flags) != 0
}
func (p *Parser) parse(args []string) (err error) {
posOnly := false
for len(args) != 0 {
if p.excess != nil && p.nextPosArg() == nil {
*p.excess = args
return
}
a := args[0]
args = args[1:]
if !posOnly && a == "--" {
posOnly = true
continue
}
if !posOnly && isFlag(a) {
err = p.parseFlag(a[1:])
if err != nil {
err = xerrors.Errorf("parsing flag %q: %w", a[1:], err)
}
} else {
err = p.parsePos(a)
if !p.parseIntermixed {
posOnly = true
}
}
if err != nil {
return
}
}
if p.numPos < p.minPos() {
return userError{fmt.Sprintf("missing argument: %q", p.indexPosArg(p.numPos).name)}
}
return
}
func (p *Parser) minPos() (min int) {
for _, arg := range p.posArgs {
min += arg.arity.min
}
return
}
func newParser(cmd interface{}, opts ...parseOpt) (p *Parser, err error) {
p = &Parser{
cmd: cmd,
parseIntermixed: true,
}
for _, opt := range opts {
opt(p)
}
err = p.parseCmd()
return
}
func (p *Parser) parseCmd() error {
if p.cmd == nil {
return nil
}
s := reflect.ValueOf(p.cmd).Elem()
for s.Kind() == reflect.Interface {
s = s.Elem()
}
if s.Kind() != reflect.Struct {
return fmt.Errorf("expected struct got %s", s.Type())
}
return p.parseStruct(s, nil)
}
// Positional arguments are marked per struct.
func (p *Parser) parseStruct(st reflect.Value, path []flagNameComponent) (err error) {
posStarted := false
foreachStructField(st, func(f reflect.Value, sf reflect.StructField) (stop bool) {
if !posStarted && f.Type() == reflect.TypeOf(StartPos{}) {
posStarted = true
return false
}
if f.Type() == reflect.TypeOf(ExcessArgs{}) {
p.excess = f.Addr().Interface().(*ExcessArgs)
return false
}
if sf.PkgPath != "" {
return false
}
if p.excess != nil {
err = ErrFieldsAfterExcessArgs
return true
}
if canMarshal(f) {
if posStarted {
err = p.addPos(f, sf, path)
} else {
err = p.addFlag(f, sf, path)
if err != nil {
err = fmt.Errorf("error adding flag in %s: %s", st.Type(), err)
}
}
return err != nil
}
var parsed bool
parsed, err = p.parseEmbeddedStruct(f, sf, path)
if err != nil {
err = fmt.Errorf("parsing embedded struct: %w", err)
stop = true
return
}
if parsed {
return false
}
err = fmt.Errorf("field has bad type: %v", f.Type())
return true
})
return
}
func (p *Parser) parseEmbeddedStruct(f reflect.Value, sf reflect.StructField, path []flagNameComponent) (parsed bool, err error) {
if f.Kind() == reflect.Ptr {
f = f.Elem()
}
if f.Kind() != reflect.Struct {
return
}
if canMarshal(f.Addr()) {
err = fmt.Errorf("field %q has type %s, but %s is marshalable", sf.Name, f.Type(), f.Addr().Type())
return
}
parsed = true
if !sf.Anonymous {
path = append(path, structFieldFlagNameComponent(sf))
}
err = p.parseStruct(f, path)
return
}
func newArg(v reflect.Value, sf reflect.StructField, name string) arg {
return arg{
arity: fieldArity(v, sf),
value: v,
name: name,
help: sf.Tag.Get("help"),
}
}
func (p *Parser) addPos(f reflect.Value, sf reflect.StructField, path []flagNameComponent) error {
p.posArgs = append(p.posArgs, newArg(f, sf, strings.ToUpper(xstrings.ToSnakeCase(sf.Name))))
return nil
}
func flagName(comps []flagNameComponent) string {
var ss []string
slices.MakeInto(&ss, comps)
return strings.Join(ss, ".")
}
func (p *Parser) addFlag(f reflect.Value, sf reflect.StructField, path []flagNameComponent) error {
name := flagName(append(path, structFieldFlagNameComponent(sf)))
if _, ok := p.flags[name]; ok {
return fmt.Errorf("flag %q defined more than once", name)
}
if p.flags == nil {
p.flags = make(map[string]arg)
}
p.flags[name] = newArg(f, sf, name)
return nil
}
func isFlag(arg string) bool {
return len(arg) > 1 && arg[0] == '-'
}
func (p *Parser) parseFlag(s string) error {
i := strings.IndexByte(s, '=')
k := s
v := ""
if i != -1 {
k = s[:i]
v = s[i+1:]
}
flag, ok := p.flags[k]
if !ok {
if (k == "help" || k == "h") && !p.noDefaultHelp {
return ErrDefaultHelp
}
return userError{fmt.Sprintf("unknown flag: %q", k)}
}
err := flag.marshal(v, i != -1)
if err != nil {
return xerrors.Errorf("parsing value %q for flag %q: %w", v, k, err)
}
return nil
}
func (p *Parser) indexPosArg(i int) *arg {
for _, arg := range p.posArgs {
if i < arg.arity.max {
return &arg
}
i -= arg.arity.max
}
return nil
}
func (p *Parser) nextPosArg() *arg {
return p.indexPosArg(p.numPos)
}
func (p *Parser) parsePos(s string) (err error) {
arg := p.nextPosArg()
if arg == nil {
return userError{fmt.Sprintf("excess argument: %q", s)}
}
err = arg.marshal(s, true)
if err != nil {
return
}
p.numPos++
return
}
type flagNameComponent string
func structFieldFlagNameComponent(sf reflect.StructField) flagNameComponent {
name := sf.Tag.Get("name")
if name != "" {
return flagNameComponent(name)
}
return fieldFlagName(sf.Name)
}
func (p *Parser) posWithHelp() (ret []arg) {
for _, a := range p.posArgs {
if a.help != "" {
ret = append(ret, a)
}
}
return
}