/
parse.go
239 lines (220 loc) · 6.12 KB
/
parse.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
// Copyright (c) 2016 Palantir Technologies. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package cli
import (
"fmt"
"strings"
"syscall"
"github.com/palantir/pkg/cli/flag"
"golang.org/x/crypto/ssh/terminal"
)
func (app *App) parse(args []string) (Context, error) {
ctx := Context{
App: app,
Command: &app.Command,
IsTerminal: func() bool {
return terminal.IsTerminal(int(uintptr(syscall.Stdout)))
},
defaults: map[string]interface{}{},
specified: map[string]interface{}{},
allVals: map[string][]interface{}{},
}
args = args[1:] // skip name of binary
fillDefaults(&ctx)
for {
applyBackcompat(&ctx, &args)
more, err := parseNextFlag(&ctx, &args)
if err != nil {
return ctx, err
}
if len(ctx.Path) == 0 && ctx.Bool(versionFlag.MainName()) {
return ctx, nil // --version, stop parsing
}
if !more {
return ctx, checkRequiredFlags(&ctx)
}
}
}
func applyBackcompat(ctx *Context, args *[]string) {
if len(ctx.Path) > 0 {
return
}
for _, backcompat := range ctx.App.Backcompat {
if len(backcompat.Path) > len(*args) {
continue
}
startsWithPath := true
for i := range backcompat.Path {
if backcompat.Path[i] != (*args)[i] {
startsWithPath = false
break
}
}
if startsWithPath {
*args = (*args)[len(backcompat.Path):]
ctx.Command = &backcompat.Command
ctx.Path = backcompat.Path
fillDefaults(ctx)
return
}
}
}
func fillDefaults(ctx *Context) {
for _, f := range ctx.Command.Flags {
if !f.IsRequired() {
ctx.defaults[f.MainName()] = f.Default()
}
}
}
func parseNextFlag(ctx *Context, args *[]string) (more bool, err error) {
// TODO: split up enormous function
if len(*args) == 0 {
return false, nil
}
first := pop(args)
hasHyphen := strings.HasPrefix(first, "-")
// look for a flag
for _, f := range ctx.Command.Flags {
if !f.HasLeader() {
if hasHyphen || ctx.Has(f.MainName()) {
continue
}
if _, isSlice := f.(flag.StringSlice); isSlice {
strs := []string{first}
for len(*args) > 0 && !strings.HasPrefix((*args)[0], "-") {
strs = append(strs, pop(args))
}
ctx.specified[f.MainName()] = strs
} else {
ctx.specified[f.MainName()], err = f.Parse(first)
if err != nil {
return false, fmt.Errorf("%v: %v", f.MainName(), err)
}
}
return true, nil
}
for _, name := range f.FullNames() {
equalsIndex := -1
// if argument matches "flag=", parse it as a flag
if strings.HasPrefix(first, name+"=") {
equalsIndex = len(name)
// if '=' occurs in the flag, split on the first occurrence and use the value after the '=' as the next argument
next := first[equalsIndex+1:]
first = first[:equalsIndex]
if next == "" {
// if value after "=" is empty, treat it as a missing value
return false, fmt.Errorf("Missing value for flag %v", first)
}
push(next, args)
}
if first != name {
continue
}
if f, ok := f.(flag.BoolFlag); ok && equalsIndex == -1 {
// if the flag is a boolean flag without an '=', interpret as "true"
ctx.specified[f.MainName()] = true
return true, nil
}
if len(*args) == 0 {
return false, fmt.Errorf("Missing value for flag %v", first)
}
ctx.specified[f.MainName()], err = f.Parse(pop(args))
if err != nil {
return false, fmt.Errorf("%v: %v", name, err)
}
ctx.allVals[f.MainName()] = append(ctx.allVals[f.MainName()], ctx.specified[f.MainName()])
return true, nil
}
}
decisionFlag := ctx.Command.DecisionFlag
isDecision := decisionFlag != "" && first == flag.WithPrefix(decisionFlag)
if isDecision {
if len(*args) == 0 {
// be consistent with missing flag message above
return false, fmt.Errorf("Missing value for flag %v", first)
}
ctx.Path = append(ctx.Path, first)
first = pop(args)
} else if decisionFlag != "" {
push(first, args)
return defaultDecision(ctx, args)
} else if hasHyphen {
return false, fmt.Errorf("Unknown flag %v", first)
}
// look for a subcommand
for _, cmd := range ctx.Command.Subcommands {
for _, name := range cmd.Names() {
if first != name {
continue
}
ctx.Command = &cmd
ctx.Path = append(ctx.Path, cmd.Names()[0])
fillDefaults(ctx)
return true, nil
}
}
push(first, args)
if isDecision {
return badDecision(ctx, *args)
}
return matchedNothing(ctx, *args)
}
func badDecision(ctx *Context, args []string) (more bool, err error) {
var cmdNames []string
for _, cmd := range ctx.Command.Subcommands {
if len(cmd.Names()) > 0 {
cmdNames = append(cmdNames, cmd.Names()[0])
}
}
var expected string
if len(cmdNames) == 1 {
expected = fmt.Sprintf("%q", cmdNames[0])
} else {
expected = fmt.Sprintf("one of [%v]", strings.Join(cmdNames, ", "))
}
return false, fmt.Errorf("Value of %v must be %v, not %q",
flag.WithPrefix(ctx.Command.DecisionFlag), expected, args[0])
}
func matchedNothing(ctx *Context, args []string) (more bool, err error) {
if len(ctx.Command.Subcommands) > 0 {
return false, fmt.Errorf("Unknown command %q", args[0])
}
// TODO: conditionally quote the arguments like in cli/cli.go
return false, fmt.Errorf("Too many arguments: %v", strings.Join(args, " "))
}
func defaultDecision(ctx *Context, args *[]string) (more bool, err error) {
for _, cmd := range ctx.Command.Subcommands {
if cmd.Name == DefaultDecision {
ctx.Command = &cmd
fillDefaults(ctx)
return true, nil
}
}
// there is no default, decision flag is required
return false, fmt.Errorf("Expected %v after %q",
flag.WithPrefix(ctx.Command.DecisionFlag),
strings.Join(ctx.Path, " "))
}
func checkRequiredFlags(ctx *Context) error {
var missing []string
for _, f := range ctx.Command.Flags {
if f.IsRequired() && !ctx.Has(f.MainName()) {
missing = append(missing, f.FullNames()[0])
}
}
if len(missing) == 1 {
return fmt.Errorf("Flag %s is required.", missing[0])
} else if len(missing) > 1 {
return fmt.Errorf("Flags %s are required.", strings.Join(missing, ", "))
}
return nil
}
func pop(args *[]string) string {
first := (*args)[0]
*args = (*args)[1:]
return first
}
func push(arg string, args *[]string) {
*args = append([]string{arg}, *args...)
}