-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
201 lines (170 loc) · 3.66 KB
/
context.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
package argparse
import (
"fmt"
"strings"
)
type Context struct {
parser *ArgParser
// positional index
pindex int
// current option
opt *Option
args []string
abort bool
err error
}
func (c *Context) Abort() {
c.abort = true
}
func (c *Context) AbortWithError(err error) {
c.Abort()
c.err = err
}
func (c *Context) Skip(n int) {
if c.Remaining() < n {
panic("Skip() out of range")
}
c.args = c.args[n:]
}
func (c *Context) Peek() string {
if c.Remaining() == 0 {
panic("Peek() with nothing left")
}
return c.args[0]
}
func (c *Context) Next() string {
if c.Remaining() == 0 {
panic("Peek() with nothing left")
}
r := c.args[0]
c.args = c.args[1:]
return r
}
func (c *Context) NextN(n int) []string {
if c.Remaining() < n {
panic("NextN() out of range")
}
tmp := make([]string, 0, n)
tmp = append(tmp, c.args[:n]...)
c.args = c.args[n:]
return tmp
}
func (c *Context) parse() error {
for c.Remaining() > 0 {
if c.abort {
break
}
if tmp := c.expand(c.args[0]); len(tmp) > 1 {
c.args = append(tmp, c.args[1:]...)
}
opt, err := c.getOption(c.Peek())
if err != nil {
if c.parser.unparceable != nil {
c.parser.unparceable(c, c.Peek(), err)
c.Skip(1)
continue
}
return err
}
if opt == nil {
c.parser.SubParserName = c.Peek()
c.parser.SubParser = c.parser.subparsers[c.Peek()]
c.Skip(1)
return c.parser.SubParser.Parse(c.Remain()...)
}
if !opt.Positional {
c.Skip(1)
}
nargs := opt.Nargs
if nargs < 0 {
nargs = 1
}
if c.Remaining() < nargs {
var suffix string
if nargs == 1 {
suffix = "an argument"
} else {
suffix = fmt.Sprintf("%d arguments", nargs)
}
return fmt.Errorf("option %q requires %s", opt.String(), suffix)
}
if opt.Callback != nil {
c.opt = opt
opt.Callback(c, c.NextN(nargs)...)
c.opt = nil
} else {
c.Skip(nargs)
}
opt.set = true
if c.err != nil {
break
}
}
return c.err
}
func (c *Context) getOption(val string) (*Option, error) {
if strings.HasPrefix(val, "--") && len(val) > 2 {
optname := val[2:]
opt, ok := c.parser.opts[optname]
if !ok || len(opt.Name) == 1 {
return nil, fmt.Errorf("unknown option %q", optname)
}
return opt, nil
} else if strings.HasPrefix(val, "-") && len(val) > 1 {
optname := val[1:]
opt, ok := c.parser.opts[optname]
if !ok {
return nil, fmt.Errorf("unknown option %q", optname)
}
return opt, nil
}
if _, ok := c.parser.subparsers[val]; ok {
return nil, nil
}
if c.pindex < len(c.parser.pos) {
opt := c.parser.pos[c.pindex]
c.pindex++
return opt, nil
} else if len(c.parser.pos) > 0 && c.parser.pos[len(c.parser.pos)-1].Nargs < 0 {
return c.parser.pos[len(c.parser.pos)-1], nil
}
if strings.HasPrefix(val, "-") {
return nil, fmt.Errorf("unknown option %q", val)
} else {
return nil, fmt.Errorf("unexpected operand %q", val)
}
}
func (c *Context) Remain() []string {
tmp := make([]string, 0, c.Remaining())
tmp = append(tmp, c.args...)
return tmp
}
func (c *Context) Remaining() int {
return len(c.args)
}
// return current option
func (c *Context) Option() *Option {
return c.opt
}
func (c *Context) expand(val string) []string {
r := make([]string, 0)
if strings.HasPrefix(val, "--") && len(val) > 2 && val[2] != '=' {
if tmp := strings.SplitN(val, "=", 2); len(tmp) > 1 {
return tmp
}
} else if strings.HasPrefix(val, "-") && len(val) > 1 {
for i := 1; i < len(val); i++ {
optname := val[i : i+1]
r = append(r, "-"+optname)
opt, ok := c.parser.opts[optname]
if ((ok && opt.Nargs > 0) || optname == "-") && i != len(val)-1 {
r = append(r, val[i+1:])
return r
}
}
}
if len(r) == 0 {
r = append(r, val)
}
return r
}