-
Notifications
You must be signed in to change notification settings - Fork 56
/
option.go
199 lines (169 loc) · 4.06 KB
/
option.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
package matcher
import (
"strings"
"github.com/jawher/mow.cli/internal/container"
"github.com/jawher/mow.cli/internal/values"
)
// NewOpt create an option matcher that can consume short and long options
func NewOpt(o *container.Container, index map[string]*container.Container) Matcher {
return &opt{
theOne: o,
index: index,
}
}
type opt struct {
theOne *container.Container
index map[string]*container.Container
}
func (*opt) Priority() int {
return 1
}
func (o *opt) String() string {
return o.theOne.Names[0]
}
func (o *opt) Match(args []string, c *ParseContext) (bool, []string) {
if len(args) == 0 || c.RejectOptions {
return o.theOne.ValueSetFromEnv, args
}
idx := 0
for idx < len(args) {
arg := args[idx]
switch {
case arg == "-":
idx++
case arg == "--":
return o.theOne.ValueSetFromEnv, args
case strings.HasPrefix(arg, "--"):
matched, consumed, nargs := o.matchLongOpt(args, idx, c)
if matched {
return true, nargs
}
if consumed == 0 {
return o.theOne.ValueSetFromEnv, args
}
idx += consumed
case strings.HasPrefix(arg, "-"):
matched, consumed, nargs := o.matchShortOpt(args, idx, c)
if matched {
return true, nargs
}
if consumed == 0 {
return o.theOne.ValueSetFromEnv, args
}
idx += consumed
default:
return o.theOne.ValueSetFromEnv, args
}
}
return o.theOne.ValueSetFromEnv, args
}
func (o *opt) matchLongOpt(args []string, idx int, c *ParseContext) (bool, int, []string) {
arg := args[idx]
kv := strings.SplitN(arg, "=", 2)
name := kv[0]
opt, found := o.index[name]
if !found {
return false, 0, args
}
switch {
case len(kv) == 2:
if opt != o.theOne {
return false, 1, args
}
value := kv[1]
if value == "" {
return false, 0, args
}
c.Opts[o.theOne] = append(c.Opts[o.theOne], value)
return true, 1, removeStringAt(idx, args)
case values.IsBool(opt.Value):
if opt != o.theOne {
return false, 1, args
}
c.Opts[o.theOne] = append(c.Opts[o.theOne], "true")
return true, 1, removeStringAt(idx, args)
default:
if len(args[idx:]) < 2 {
return false, 0, args
}
if opt != o.theOne {
return false, 2, args
}
value := args[idx+1]
if strings.HasPrefix(value, "-") {
return false, 0, args
}
c.Opts[o.theOne] = append(c.Opts[o.theOne], value)
return true, 2, removeStringsBetween(idx, idx+1, args)
}
}
func (o *opt) matchShortOpt(args []string, idx int, c *ParseContext) (bool, int, []string) {
arg := args[idx]
if len(arg) < 2 {
return false, 0, args
}
if strings.HasPrefix(arg[2:], "=") {
name := arg[0:2]
opt, _ := o.index[name]
if opt != o.theOne {
return false, 1, args
}
value := arg[3:]
if value == "" {
return false, 0, args
}
c.Opts[o.theOne] = append(c.Opts[o.theOne], value)
return true, 1, removeStringAt(idx, args)
}
rem := arg[1:]
remIdx := 0
for len(rem[remIdx:]) > 0 {
name := "-" + rem[remIdx:remIdx+1]
opt, found := o.index[name]
if !found {
return false, 0, args
}
if values.IsBool(opt.Value) {
if opt != o.theOne {
remIdx++
continue
}
c.Opts[o.theOne] = append(c.Opts[o.theOne], "true")
newRem := rem[:remIdx] + rem[remIdx+1:]
if newRem == "" {
return true, 1, removeStringAt(idx, args)
}
return true, 0, replaceStringAt(idx, "-"+newRem, args)
}
value := rem[remIdx+1:]
if value == "" {
if len(args[idx+1:]) == 0 {
return false, 0, args
}
if opt != o.theOne {
return false, 2, args
}
value = args[idx+1]
if strings.HasPrefix(value, "-") {
return false, 0, args
}
c.Opts[o.theOne] = append(c.Opts[o.theOne], value)
newRem := rem[:remIdx]
if newRem == "" {
return true, 2, removeStringsBetween(idx, idx+1, args)
}
nargs := replaceStringAt(idx, "-"+newRem, args)
return true, 1, removeStringAt(idx+1, nargs)
}
if opt != o.theOne {
return false, 1, args
}
c.Opts[o.theOne] = append(c.Opts[o.theOne], value)
newRem := rem[:remIdx]
if newRem == "" {
return true, 1, removeStringAt(idx, args)
}
return true, 0, replaceStringAt(idx, "-"+newRem, args)
}
return false, 1, args
}