-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommand_test.go
349 lines (319 loc) · 8.87 KB
/
command_test.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package xflags
import (
"flag"
"fmt"
"os/exec"
"strings"
"testing"
)
func TestSubcommands(t *testing.T) {
// ranCommands is a bit mask to identify which subcommand handlers were
// invoked
var ranCommands uint64
var setFlags uint64
// newCommand is a function to recursively create subcommands
var newCommand func(n, of uint64) Commander
newCommand = func(n, of uint64) Commander {
c := NewCommand(fmt.Sprintf("command%02d", n), "").
Flags(
BitField(
&setFlags,
uint64(1)<<(n-1),
fmt.Sprintf("x%02d", n),
false,
"",
),
).
HandleFunc(func(args []string) int {
ranCommands |= 1 << (n - 1)
return 0
})
if n < of {
c.Subcommands(newCommand(n+1, of))
}
return c
}
// call each subcommand
cmdDepth := uint64(64)
cmd := NewCommand("test", "").
Subcommands(newCommand(1, cmdDepth)).
Must()
for i := uint64(0); i < cmdDepth; i++ {
// build args to call subcommand i
ranCommands = 0
args := make([]string, 0)
for j := uint64(0); j < i+1; j++ {
args = append(
args,
fmt.Sprintf("command%02d", j+1), fmt.Sprintf("--x%02d", j+1),
)
}
// invoke the subcommand handler
subcommand, err := cmd.Parse(args)
if err != nil {
t.Error(err)
return
}
subcommand.HandlerFunc(nil)
// check which commands run and flags were set
assertUint64(t, 1<<i, ranCommands)
expectFlags := uint64(0)
for j := uint64(0); j < i+1; j++ {
expectFlags |= 1 << j
}
assertUint64(t, expectFlags, setFlags)
}
}
// TestPosFlagOrdering enforces the rule that no positional arguments may be
// specified after another variable length positional argument as this would
// create ambiguity as to which flag a given argument belongs to. Fixed length
// positional arguments do not exhibit this problem.
func TestPosFlagOrdering(t *testing.T) {
var sink string
getFixture := func(flags ...Flagger) *CommandBuilder {
return NewCommand("test", "").Flags(flags...)
}
successCases := []*CommandBuilder{
getFixture(
String(&sink, "one", "", "").Positional(),
),
getFixture(
String(&sink, "one", "", "").Positional(),
String(&sink, "two", "", "").Positional(),
),
getFixture(
String(&sink, "one", "", "").Positional().NArgs(0, 1),
String(&sink, "two", "", "").Positional(),
),
getFixture(
String(&sink, "one", "", "").Positional().NArgs(1, 1),
String(&sink, "two", "", "").Positional(),
),
getFixture(
String(&sink, "one", "", "").Positional().NArgs(1, 1),
String(&sink, "two", "", "").Positional().NArgs(2, 2),
String(&sink, "three", "", "").Positional().NArgs(3, 3),
String(&sink, "four", "", "").Positional(),
),
}
for i, builder := range successCases {
t.Run(fmt.Sprintf("SuccessCase%02d", i+1), func(t *testing.T) {
if _, err := builder.Command(); err != nil {
t.Errorf("expected nil error, got: %v", err)
}
})
}
errorCases := []*CommandBuilder{
getFixture(
String(&sink, "one", "", "").Positional().NArgs(0, 0),
String(&sink, "two", "", "").Positional(),
),
getFixture(
String(&sink, "one", "", "").Positional().NArgs(1, 0),
String(&sink, "two", "", "").Positional(),
),
}
for i, builder := range errorCases {
t.Run(fmt.Sprintf("ErrorCase%02d", i+1), func(t *testing.T) {
if _, err := builder.Command(); err == nil {
t.Errorf("expected error, got nil")
}
})
}
}
func TestPositionalFlags(t *testing.T) {
var foo, bar string
var baz, qux []string
cmd := NewCommand("test", "").Flags(
String(&foo, "foo", "", "").Positional().Required(),
String(&bar, "bar", "", "").Positional().Required(),
Strings(&baz, "baz", nil, "").Positional().NArgs(2, 2),
Strings(&qux, "qux", nil, "").Positional().NArgs(0, 0),
).Must()
_, err := cmd.Parse([]string{"one", "two", "three", "four", "five", "six"})
if err != nil {
t.Error(err)
return
}
assertString(t, "one", foo)
assertString(t, "two", bar)
assertStrings(t, []string{"three", "four"}, baz)
assertStrings(t, []string{"five", "six"}, qux)
}
func TestFlagSet(t *testing.T) {
var foo, bar string
var baz, qux bool
flagSet := flag.NewFlagSet("native", flag.ContinueOnError)
flagSet.StringVar(&foo, "foo", "", "")
flagSet.BoolVar(&baz, "baz", false, "")
c := NewCommand("test", "").
Flags(
String(&bar, "bar", "", ""),
Bool(&qux, "qux", false, ""),
).
FlagSet(flagSet).
Must()
_, err := c.Parse([]string{"--foo", "foo", "--bar", "bar", "--baz", "--qux"})
if err != nil {
t.Fatal(err)
}
assertString(t, "foo", foo)
assertString(t, "bar", bar)
assertBool(t, true, baz)
assertBool(t, true, qux)
}
func TestCommandLineage(t *testing.T) {
a, b, c := NewCommand("a", ""), NewCommand("b", ""), NewCommand("c", "")
a.Subcommands(b)
b.Subcommands(c)
cmd := a.Must()
assertString(t, "a", cmd.Name)
assertString(t, "b", cmd.Subcommands[0].Name)
assertString(t, "a", cmd.Subcommands[0].Parent.Name)
assertString(t, "c", cmd.Subcommands[0].Subcommands[0].Name)
assertString(t, "b", cmd.Subcommands[0].Subcommands[0].Parent.Name)
}
func ExampleCommandBuilder_FlagGroup() {
var n int
var rightToLeft bool
var endcoding string
cmd := NewCommand("helloworld", "").
// n flag defines how many times to print "Hello, World!".
Flags(Int(&n, "n", 1, "Print n times")).
// Create a flag group for language-related flags.
FlagGroup(
"language",
"Language options",
String(&endcoding, "encoding", "utf-8", "Text encoding"),
Bool(&rightToLeft, "rtl", false, "Print right-to-left"),
)
// Print the help page
RunWithArgs(cmd, "--help")
// Output:
// Usage: helloworld [OPTIONS]
//
// Options:
// -n Print n times
//
// Language options:
// --encoding Text encoding
// --rtl Print right-to-left
}
func ExampleCommandBuilder_FlagSet() {
// create a Go-native flag set
flagSet := flag.NewFlagSet("native", flag.ExitOnError)
message := flagSet.String("m", "Hello, World!", "Message to print")
// import the flagset into an xflags command
cmd := NewCommand("helloworld", "").
FlagSet(flagSet).
HandleFunc(func(args []string) (exitCode int) {
fmt.Println(*message)
return
})
// Print the help page
fmt.Println("+ helloworld --help")
RunWithArgs(cmd, "--help")
// Run the command
fmt.Println()
fmt.Println("+ helloworld")
RunWithArgs(cmd)
// Output:
// + helloworld --help
// Usage: helloworld [OPTIONS]
//
// Options:
// -m Message to print
//
// + helloworld
// Hello, World!
}
func ExampleCommandBuilder_Subcommands() {
var n int
// configure a "create" subcommand
create := NewCommand("create", "Make new widgets").
HandleFunc(func(args []string) (exitCode int) {
fmt.Printf("Created %d widget(s)\n", n)
return
})
// configure a "destroy" subcommand
destroy := NewCommand("destroy", "Destroy widgets").
HandleFunc(func(args []string) (exitCode int) {
fmt.Printf("Destroyed %d widget(s)\n", n)
return
})
// configure the main command with two subcommands and a global "n" flag.
cmd := NewCommand("widgets", "").
Flags(Int(&n, "n", 1, "Affect n widgets")).
Subcommands(create, destroy)
// Print the help page
fmt.Println("+ widgets --help")
RunWithArgs(cmd, "--help")
// Invoke the "create" subcommand
fmt.Println()
fmt.Println("+ widgets create -n=3")
RunWithArgs(cmd, "create", "-n=3")
// Output:
// + widgets --help
// Usage: widgets [OPTIONS] COMMAND
//
// Options:
// -n Affect n widgets
//
// Commands:
// create Make new widgets
// destroy Destroy widgets
//
// + widgets create -n=3
// Created 3 widget(s)
}
func ExampleCommandBuilder_Synopsis() {
var n int
cmd := NewCommand("helloworld", "Say \"Hello, World!\"").
// Configure a synopsis to print detailed usage information on the help
// page.
Synopsis(
"This utility prints \"Hello, World!\" to the standard output.\n" +
"Print more than once with -n.",
).
Flags(Int(&n, "n", 1, "Print n times"))
// Print the help page
RunWithArgs(cmd, "--help")
// Output:
// Usage: helloworld [OPTIONS]
//
// Say "Hello, World!"
//
// Options:
// -n Print n times
//
// This utility prints "Hello, World!" to the standard output.
// Print more than once with -n.
}
func ExampleCommandBuilder_WithTerminator() {
var verbose bool
// create a command that passes arguments to /bin/echo
cmd := NewCommand("echo_wrapper", "calls /bin/echo").
Flags(
Bool(&verbose, "v", false, "Print verbose output"),
).
WithTerminator(). // enable the "--" terminator
HandleFunc(func(args []string) (exitCode int) {
// read verbose argument which was parsed by xflags
if verbose {
fmt.Printf("+ /bin/echo %s\n", strings.Join(args, " "))
}
// pass unparsed arguments after the "--" terminator to /bin/echo
output, err := exec.Command("/bin/echo", args...).Output()
if err != nil {
fmt.Println(err)
return 1
}
fmt.Println(string(output))
return
})
// run in verbose mode and pass ["Hello", "World!"] to /bin/echo.
RunWithArgs(cmd, "-v", "--", "Hello,", "World!")
// Output:
// + /bin/echo Hello, World!
// Hello, World!
}