-
Notifications
You must be signed in to change notification settings - Fork 5
/
gocmd_test.go
497 lines (441 loc) · 12 KB
/
gocmd_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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
// gocmd
// For the full copyright and license information, please view the LICENSE.txt file.
package gocmd_test
import (
"errors"
"fmt"
"io"
"log"
"math"
"os"
"strings"
"testing"
"github.com/devfacet/gocmd/v3"
. "github.com/smartystreets/goconvey/convey"
)
var osArgs []string
func init() {
osArgs = make([]string, len(os.Args))
copy(osArgs, os.Args)
}
func resetArgs() {
os.Args = make([]string, len(osArgs))
copy(os.Args, osArgs)
}
func TestNew(t *testing.T) {
Convey("should create a new command", t, func() {
resetArgs()
os.Args = append(os.Args, "-f=true", "--bar=test", "-b", "baz", "test", "-f=false", "-b=bar")
flags := struct {
Foo bool `short:"f"`
Bar string `long:"bar"`
Baz string `short:"b" long:"baz"`
Command struct {
Foo bool `short:"f"`
Bar string `short:"b"`
} `command:"test"`
}{}
cmd, err := gocmd.New(gocmd.Options{
Name: "test",
Version: "1.0.0",
Description: "Test",
Flags: &flags,
})
So(err, ShouldBeNil)
So(cmd, ShouldNotBeNil)
So(cmd.Name(), ShouldEqual, "test")
So(cmd.Version(), ShouldEqual, "1.0.0")
So(cmd.Description(), ShouldEqual, "Test")
So(flags.Foo, ShouldEqual, true)
So(flags.Bar, ShouldEqual, "test")
So(flags.Baz, ShouldEqual, "baz")
So(flags.Command.Foo, ShouldEqual, false)
So(flags.Command.Bar, ShouldEqual, "bar")
resetArgs()
})
Convey("should fail to create a new command", t, func() {
resetArgs()
cmd, err := gocmd.New(gocmd.Options{
Name: "test",
Version: "1.0.0",
Description: "Test",
Flags: &struct {
Foo bool `short:"foo"`
}{},
})
So(err, ShouldNotBeNil)
So(err, ShouldBeError, errors.New("short argument foo in Foo field must be one character long"))
So(cmd, ShouldBeNil)
cmd, err = gocmd.New(gocmd.Options{
Name: "test",
Version: "1.0.0",
Description: "Test",
Flags: &struct {
Foo bool `short:"f" required:"true"`
}{},
AnyError: true,
})
So(err, ShouldNotBeNil)
So(err, ShouldBeError, errors.New("argument -f is required"))
So(cmd, ShouldBeNil)
cmd, err = gocmd.New(gocmd.Options{
Name: "test",
Version: "1.0.0",
Description: "Test",
Flags: &struct {
Foo bool `short:"f"`
Bar bool `short:"f"`
}{},
Logger: log.New(io.Discard, "", 0),
ExitOnError: true,
})
So(err, ShouldNotBeNil)
So(err, ShouldBeError, errors.New("short argument f in Bar field is already defined in Foo field"))
So(cmd, ShouldBeNil)
cmd, err = gocmd.New(gocmd.Options{
Name: "test",
Version: "1.0.0",
Description: "Test",
Flags: &struct {
Foo bool `short:"f" required:"true"`
}{},
Logger: log.New(io.Discard, "", 0),
ExitOnError: true,
})
So(err, ShouldNotBeNil)
So(err, ShouldBeError, errors.New("argument -f is required"))
So(cmd, ShouldBeNil)
})
}
func TestCmd_Name(t *testing.T) {
Convey("should return the correct command name", t, func() {
cmd, err := gocmd.New(gocmd.Options{
Name: "test",
})
So(err, ShouldBeNil)
So(cmd, ShouldNotBeNil)
So(cmd.Name(), ShouldEqual, "test")
})
}
func TestCmd_Version(t *testing.T) {
Convey("should return the correct command version", t, func() {
cmd, err := gocmd.New(gocmd.Options{
Version: "1.0.0",
})
So(err, ShouldBeNil)
So(cmd, ShouldNotBeNil)
So(cmd.Version(), ShouldEqual, "1.0.0")
})
}
func TestCmd_Description(t *testing.T) {
Convey("should return the correct command description", t, func() {
cmd, err := gocmd.New(gocmd.Options{
Description: "Test",
})
So(err, ShouldBeNil)
So(cmd, ShouldNotBeNil)
So(cmd.Description(), ShouldEqual, "Test")
})
}
func TestCmd_LookupFlag(t *testing.T) {
Convey("should lookup a flag", t, func() {
resetArgs()
os.Args = append(os.Args, "-f=true")
cmd, err := gocmd.New(gocmd.Options{
Flags: &struct {
Foo bool `short:"f" default:"true"`
}{},
})
So(err, ShouldBeNil)
So(cmd, ShouldNotBeNil)
v, ok := cmd.LookupFlag("Foo")
So(v, ShouldContain, "true")
So(ok, ShouldEqual, true)
v, ok = cmd.LookupFlag("Bar")
So(v, ShouldBeNil)
So(ok, ShouldEqual, false)
resetArgs()
})
}
func TestCmd_FlagValue(t *testing.T) {
Convey("should return the correct flag value", t, func() {
resetArgs()
os.Args = append(os.Args, "-f=true")
cmd, err := gocmd.New(gocmd.Options{
Flags: &struct {
Foo bool `short:"f" default:"true"`
}{},
})
So(err, ShouldBeNil)
So(cmd, ShouldNotBeNil)
v, ok := cmd.FlagValue("Foo").(bool)
So(v, ShouldEqual, true)
So(ok, ShouldEqual, true)
v, ok = cmd.FlagValue("Bar").(bool)
So(v, ShouldEqual, false)
So(ok, ShouldEqual, false)
resetArgs()
})
}
func TestCmd_FlagArgs(t *testing.T) {
Convey("should return the correct flag args", t, func() {
resetArgs()
os.Args = append(os.Args, "-f=true")
cmd, err := gocmd.New(gocmd.Options{
Flags: &struct {
Foo bool `short:"f" default:"true"`
}{},
})
So(err, ShouldBeNil)
So(cmd, ShouldNotBeNil)
args := cmd.FlagArgs("Foo")
So(args, ShouldHaveLength, 1)
args = cmd.FlagArgs("Bar")
So(args, ShouldHaveLength, 0)
resetArgs()
})
}
func TestCmd_FlagErrors(t *testing.T) {
Convey("should return the flag errors", t, func() {
cmd, err := gocmd.New(gocmd.Options{
Flags: &struct {
Foo struct {
} `command:"foo" global:"true"`
}{},
})
So(err, ShouldBeNil)
So(cmd, ShouldNotBeNil)
flagErrors := cmd.FlagErrors()
So(flagErrors, ShouldNotBeNil)
So(flagErrors, ShouldContain, errors.New("command foo can't be global"))
})
}
func TestHandleFlag(t *testing.T) {
Convey("should fail to add flag handler", t, func() {
fh, err := gocmd.HandleFlag("", func(cmd *gocmd.Cmd, args []string) error {
return nil
})
So(err, ShouldNotBeNil)
So(err, ShouldBeError, errors.New("invalid flag name"))
So(fh, ShouldBeNil)
})
Convey("should add flag handler", t, func() {
resetArgs()
os.Args = []string{"gocmd.test", "fh1", "fh2"}
fhCnt := 0
fh, _ := gocmd.HandleFlag("FH1", func(cmd *gocmd.Cmd, args []string) error {
fhCnt++
return errors.New("handler error")
})
fh.SetPriority(1)
fh.SetExitOnError(true)
gocmd.HandleFlag("FH2", func(cmd *gocmd.Cmd, args []string) error {
fhCnt++
return nil
})
fh.SetPriority(2)
cmd, err := gocmd.New(gocmd.Options{
Name: "test",
Version: "1.0.0",
Description: "Test",
Flags: &struct {
FH1 struct{} `command:"fh1"`
FH2 struct{} `command:"fh2"`
}{},
Logger: log.New(io.Discard, "", 0),
})
So(err, ShouldNotBeNil)
So(err, ShouldBeError, errors.New("handler error"))
So(cmd, ShouldBeNil)
So(fhCnt, ShouldEqual, 2)
resetArgs()
})
}
func ExampleNew_usage() {
os.Args = []string{"gocmd.test"}
gocmd.New(gocmd.Options{
Name: "basic",
Version: "1.0.0",
Description: "A basic app",
Flags: &struct {
Help bool `short:"h" long:"help" description:"Display usage" global:"true"`
Version bool `short:"v" long:"version" description:"Display version"`
VersionEx bool `long:"vv" description:"Display version (extended)"`
Echo struct {
Settings bool `settings:"true" allow-unknown-arg:"true"`
} `command:"echo" description:"Print arguments"`
Math struct {
Sqrt struct {
Number float64 `short:"n" long:"number" required:"true" description:"Number"`
} `command:"sqrt" description:"Calculate square root"`
Pow struct {
Base float64 `short:"b" long:"base" required:"true" description:"Base"`
Exponent float64 `short:"e" long:"exponent" required:"true" description:"Exponent"`
} `command:"pow" description:"Calculate base exponential"`
} `command:"math" description:"Math functions"`
}{},
ConfigType: gocmd.ConfigTypeAuto,
})
// Output:
// Usage: basic [options...] COMMAND [options...]
//
// A basic app
//
// Options:
// -h, --help Display usage
// -v, --version Display version
// --vv Display version (extended)
//
// Commands:
// echo Print arguments
// math Math functions
// sqrt Calculate square root
// -n, --number Number
// pow Calculate base exponential
// -b, --base Base
// -e, --exponent Exponent
resetArgs()
}
func ExampleNew_usage_h() {
os.Args = []string{"gocmd.test", "-h"}
gocmd.New(gocmd.Options{
Name: "basic",
Version: "1.0.0",
Description: "A basic app",
Flags: &struct {
Help bool `short:"h" long:"help" description:"Display usage" global:"true"`
}{},
ConfigType: gocmd.ConfigTypeAuto,
})
// Output:
// Usage: basic [options...]
//
// A basic app
//
// Options:
// -h, --help Display usage
resetArgs()
}
func ExampleNew_usage_help() {
os.Args = []string{"gocmd.test", "--help"}
gocmd.New(gocmd.Options{
Name: "basic",
Version: "1.0.0",
Description: "A basic app",
Flags: &struct {
Help bool `long:"help" description:"Display usage" global:"true"`
}{},
ConfigType: gocmd.ConfigTypeAuto,
})
// Output:
// Usage: basic [options...]
//
// A basic app
//
// Options:
// --help Display usage
resetArgs()
}
func ExampleNew_version() {
os.Args = []string{"gocmd.test", "-vv"}
gocmd.New(gocmd.Options{
Name: "basic",
Version: "1.0.0",
Description: "A basic app",
Flags: &struct {
Version bool `short:"v" long:"version" description:"Display version"`
VersionEx bool `long:"vv" description:"Display version (extended)"`
}{},
ConfigType: gocmd.ConfigTypeAuto,
})
// Output:
// App name : basic
// App version : 1.0.0
// Go version : vTest
resetArgs()
}
func ExampleNew_version_v() {
os.Args = []string{"gocmd.test", "-v"}
gocmd.New(gocmd.Options{
Name: "basic",
Version: "1.0.0",
Description: "A basic app",
Flags: &struct {
Version bool `short:"v" long:"version" description:"Display version"`
}{},
ConfigType: gocmd.ConfigTypeAuto,
})
// Output:
// 1.0.0
resetArgs()
}
func ExampleNew_version_version() {
os.Args = []string{"gocmd.test", "--version"}
gocmd.New(gocmd.Options{
Name: "basic",
Version: "1.0.0",
Description: "A basic app",
Flags: &struct {
Version bool `long:"version" description:"Display version"`
}{},
ConfigType: gocmd.ConfigTypeAuto,
})
// Output:
// 1.0.0
resetArgs()
}
func ExampleNew_command() {
os.Args = []string{"gocmd.test", "math", "sqrt", "-n=9"}
flags := struct {
Help bool `short:"h" long:"help" description:"Display usage" global:"true"`
Version bool `short:"v" long:"version" description:"Display version"`
VersionEx bool `long:"vv" description:"Display version (extended)"`
Echo struct {
Settings bool `settings:"true" allow-unknown-arg:"true"`
} `command:"echo" description:"Print arguments"`
Math struct {
Sqrt struct {
Number float64 `short:"n" long:"number" required:"true" description:"Number"`
} `command:"sqrt" description:"Calculate square root"`
Pow struct {
Base float64 `short:"b" long:"base" required:"true" description:"Base"`
Exponent float64 `short:"e" long:"exponent" required:"true" description:"Exponent"`
} `command:"pow" description:"Calculate base exponential"`
} `command:"math" description:"Math functions" nonempty:"true"`
}{}
// Echo command
gocmd.HandleFlag("Echo", func(cmd *gocmd.Cmd, args []string) error {
fmt.Printf("%s\n", strings.Join(cmd.FlagArgs("Echo")[1:], " "))
return nil
})
// Math commands
gocmd.HandleFlag("Math.Sqrt", func(cmd *gocmd.Cmd, args []string) error {
fmt.Println(math.Sqrt(flags.Math.Sqrt.Number))
return nil
})
gocmd.HandleFlag("Math.Pow", func(cmd *gocmd.Cmd, args []string) error {
fmt.Println(math.Pow(flags.Math.Pow.Base, flags.Math.Pow.Exponent))
return nil
})
// Init the app
gocmd.New(gocmd.Options{
Name: "basic",
Version: "1.0.0",
Description: "A basic app",
Flags: &flags,
ConfigType: gocmd.ConfigTypeAuto,
})
// Output:
// 3
resetArgs()
}
func ExampleCmd_PrintVersion() {
cmd, err := gocmd.New(gocmd.Options{
Version: "1.0.0",
ConfigType: gocmd.ConfigTypeAuto,
})
if err == nil {
cmd.PrintVersion(false)
}
// Output: 1.0.0
}