-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples_test.go
More file actions
675 lines (581 loc) · 16.7 KB
/
examples_test.go
File metadata and controls
675 lines (581 loc) · 16.7 KB
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
package cli_test
import (
"fmt"
"image"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/steverusso/cli"
)
func ExampleCommandInfo_Arg() {
c := cli.New().
Arg(cli.NewArg("name")).
ParseTheseOrExit("alice")
fmt.Println(cli.Get[string](c, "name"))
// Output:
// alice
}
func ExampleCommandInfo_ExtraHelp() {
in := cli.New("example").
Help("an example command").
ExtraHelp("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.")
_, err := in.ParseThese("--help")
fmt.Println(err)
// Output:
// example - an example command
//
// overview:
// Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
// incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
// exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
//
// usage:
// example [options]
//
// options:
// -h, --help
// Show this help message and exit.
}
func ExampleCommandInfo_Opt() {
c := cli.New().
Opt(cli.NewOpt("a")).
ParseTheseOrExit("-a", "hello")
fmt.Println(cli.Get[string](c, "a"))
// Output:
// hello
}
func ExampleCommandInfo_SubcmdOptional() {
// Simple command-with-subcommand structure. Parsing the top-level
// command will return an error if a subcommand isn't provided.
scReq := cli.New("example").
Help("an example program").
Subcmd(cli.NewCmd("cmd1"))
// Same command structure as above, except parsing will
// NOT return an error if a subcommand isn't provided.
scOpt := cli.New("example").
Help("an example program").
Subcmd(cli.NewCmd("cmd1")).
SubcmdOptional()
c, _ := scReq.ParseThese("cmd1")
fmt.Printf("have subcommand %q\n", c.Subcmd.Name)
_, err := scReq.ParseThese()
fmt.Println("err:", err)
c, err = scOpt.ParseThese()
fmt.Printf("err: %v, c.Subcmd: %v\n", err, c.Subcmd) // using c.Subcmd.Name here would panic
// Output:
// have subcommand "cmd1"
// err: missing subcommand
// err: <nil>, c.Subcmd: <nil>
}
func ExampleCommandInfo_Usage() {
in := cli.New("example").
Help("an example command").
Usage(
"example [--aa <arg>]",
"example [-h]",
).
Opt(cli.NewOpt("aa").Help("an option"))
_, err := in.ParseThese("--help")
fmt.Println(err)
// Output:
// example - an example command
//
// usage:
// example [--aa <arg>]
// example [-h]
//
// options:
// --aa <arg>
// an option
//
// -h, --help
// Show this help message and exit.
}
func ExampleDefaultFullHelp() {
in := cli.New("example").
Help("an example command").
Opt(cli.NewOpt("aa").Env("AA").Default("def").Help("an option")).
Opt(cli.NewOpt("bb").
Help("another option that is required and has a really long blurb to show how it will be wrapped").
Required()).
Arg(cli.NewArg("cc").Help("a positional argument")).
Opt(cli.NewBoolOpt("h").
Help("will show how this command looks in the default full help message").
WithHelpGen(func(_ cli.Input, c *cli.CommandInfo) string {
return cli.DefaultFullHelp(c)
}))
_, err := in.ParseThese("-h")
fmt.Println(err)
// Output:
// example - an example command
//
// usage:
// example [options] [arguments]
//
// options:
// --aa <arg>
// an option
//
// [default: def]
// [env: AA]
//
// --bb <arg> (required)
// another option that is required and has a really long blurb to show how it will be
// wrapped
//
// -h
// will show how this command looks in the default full help message
//
// arguments:
// [cc]
// a positional argument
}
func ExampleDefaultShortHelp_complex() {
in := cli.New("example").
Help("an example command").
Opt(cli.NewOpt("aa").Env("AA").Default("def").Help("an option")).
Opt(cli.NewOpt("bb").
Help("another option that is required and has a really long blurb to show how it will be wrapped").
Required()).
Opt(cli.NewOpt("kind-of-a-long-name").
Help("due to this option's name, the blurbs for each option on this command " +
"will begin on their own non-indented lines")).
Arg(cli.NewArg("posarg1").Required().Help("a required positional argument")).
Arg(cli.NewArg("posarg2").Env("PA2").Help("an optional positional argument")).
Opt(cli.NewBoolOpt("h").
Help("will show the default short help message").
WithHelpGen(func(_ cli.Input, c *cli.CommandInfo) string {
return cli.DefaultShortHelp(c)
}))
_, err := in.ParseThese("-h")
fmt.Println(err)
// Output:
// example - an example command
//
// usage:
// example [options] [arguments]
//
// options:
// --aa <arg>
// an option (default: def) [$AA]
// --bb <arg>
// another option that is required and has a really long blurb to show how it will be
// wrapped (required)
// -h
// will show the default short help message
// --kind-of-a-long-name <arg>
// due to this option's name, the blurbs for each option on this command will begin on
// their own non-indented lines
//
// arguments:
// <posarg1> a required positional argument (required)
// [posarg2] an optional positional argument [$PA2]
}
func ExampleDefaultShortHelp_simple() {
in := cli.New("example").
Help("an example command").
Opt(cli.NewOpt("aa").Short('a').Env("AA").Default("def").Help("an option")).
Opt(cli.NewOpt("bb").Short('b').Required().Help("another option")).
Arg(cli.NewArg("cc").Required().Help("a required positional argument")).
Arg(cli.NewArg("dd").Env("PA2").Help("an optional positional argument")).
Opt(cli.NewBoolOpt("h").
Help("will show the default short help message").
WithHelpGen(func(_ cli.Input, c *cli.CommandInfo) string {
return cli.DefaultShortHelp(c)
}))
_, err := in.ParseThese("-h")
fmt.Println(err)
// Output:
// example - an example command
//
// usage:
// example [options] [arguments]
//
// options:
// -a, --aa <arg> an option (default: def) [$AA]
// -b, --bb <arg> another option (required)
// -h will show the default short help message
//
// arguments:
// <cc> a required positional argument (required)
// [dd] an optional positional argument [$PA2]
}
func ExampleDefaultShortHelp_simpleWithSubcommands() {
in := cli.New("example").
Help("an example command").
Opt(cli.NewOpt("aa").Short('a').Env("AA").Default("def").Help("an option")).
Opt(cli.NewOpt("bb").Short('b').Required().Help("another option")).
Subcmd(cli.NewCmd("subcommand1").Help("a subcommand")).
Subcmd(cli.NewCmd("subcommand2").Help("another subcommand")).
Opt(cli.NewBoolOpt("h").
Help("will show the default short help message").
WithHelpGen(func(_ cli.Input, c *cli.CommandInfo) string {
return cli.DefaultShortHelp(c)
}))
_, err := in.ParseThese("-h")
fmt.Println(err)
// Output:
// example - an example command
//
// usage:
// example [options] <command>
//
// options:
// -a, --aa <arg> an option (default: def) [$AA]
// -b, --bb <arg> another option (required)
// -h will show the default short help message
//
// commands:
// subcommand1 a subcommand
// subcommand2 another subcommand
}
func ExampleGet_option() {
c := cli.New().
Opt(cli.NewOpt("a")).
Opt(cli.NewOpt("b")).
ParseTheseOrExit("-b=hello")
// The following line would panic because 'a' isn't present.
// a := cli.Get[string](c, "a")
b := cli.Get[string](c, "b")
fmt.Printf("b: %q\n", b)
// Output:
// b: "hello"
}
func ExampleGet_positionalArgs() {
c := cli.New().
Arg(cli.NewArg("a")).
Arg(cli.NewArg("b")).
ParseTheseOrExit("hello")
// The following line would panic because 'a' isn't present.
// b := cli.Get[string](c, "b")
a := cli.Get[string](c, "a")
fmt.Printf("a: %q\n", a)
// Output:
// a: "hello"
}
func ExampleGetAll() {
c := cli.New().
Opt(cli.NewIntOpt("a").Default("0")).
ParseTheseOrExit("-a", "1", "-a", "2", "-a", "3")
a := cli.GetAll[int](c, "a")
fmt.Printf("%+#v\n", a)
// Output:
// []int{0, 1, 2, 3}
}
func ExampleGetAllSeq() {
c := cli.New().
Opt(cli.NewOpt("a").Default("Lorem")).
ParseTheseOrExit(
"-a", "ipsum",
"-a", "dolor",
"-a", "sit",
"-a", "amet")
for str := range cli.GetAllSeq[string](c, "a") {
fmt.Printf("%v\n", str)
}
// Output:
// Lorem
// ipsum
// dolor
// sit
// amet
}
func ExampleGetOr_option() {
c := cli.New().
Opt(cli.NewOpt("a")).
Opt(cli.NewOpt("b")).
ParseTheseOrExit("-a=hello")
a := cli.Get[string](c, "a")
b := cli.GetOr(c, "b", "world")
fmt.Printf("a: %q\n", a)
fmt.Printf("b: %q\n", b)
// Output:
// a: "hello"
// b: "world"
}
func ExampleGetOr_positionlArgs() {
c := cli.New().
Arg(cli.NewArg("a")).
Arg(cli.NewArg("b")).
ParseTheseOrExit("hello")
a := cli.Get[string](c, "a")
b := cli.GetOr(c, "b", "world")
fmt.Printf("a: %q\n", a)
fmt.Printf("b: %q\n", b)
// Output:
// a: "hello"
// b: "world"
}
func ExampleGetOrFunc() {
c := cli.New().
Opt(cli.NewOpt("a")).
Opt(cli.NewOpt("b")).
ParseTheseOrExit("-a", "hello")
a := cli.GetOrFunc(c, "a", func() string { return "bye" })
b := cli.GetOrFunc(c, "b", func() string { return "world" })
fmt.Printf("a: %q\n", a)
fmt.Printf("b: %q\n", b)
// Output:
// a: "hello"
// b: "world"
}
func ExampleInputInfo_Default() {
in := cli.New().Opt(cli.NewIntOpt("flag").Default("1234"))
c1 := in.ParseTheseOrExit()
fmt.Println(cli.Get[int](c1, "flag")) // Default value present for 'flag' even though no CLI arg was provided.
c2 := in.ParseTheseOrExit("--flag", "5678")
fmt.Println(cli.Get[int](c2, "flag"))
// Output:
// 1234
// 5678
}
func ExampleInputInfo_Env() {
os.Setenv("FLAG", "hello")
c := cli.New().
Opt(cli.NewOpt("flag").Env("FLAG")).
ParseTheseOrExit()
fmt.Println(cli.Get[string](c, "flag"))
// Output:
// hello
}
func ExampleInputInfo_Help() {
in := cli.New("example").
Help("example program").
Opt(cli.NewOpt("aa").Help("a short one or two sentence blurb"))
_, err := in.ParseThese("-h")
fmt.Println(err)
// Output:
// example - example program
//
// usage:
// example [options]
//
// options:
// --aa <arg> a short one or two sentence blurb
// -h, --help Show this help message and exit.
}
func ExampleInputInfo_Long() {
in := cli.New("example").
Help("example program").
Opt(cli.NewOpt("id1").Help("long name is the id by default")).
Opt(cli.NewOpt("id2").Long("long-name").Help("long name is set to something other than the id"))
_, err := in.ParseThese("-h")
fmt.Println(err)
// Output:
// example - example program
//
// usage:
// example [options]
//
// options:
// -h, --help Show this help message and exit.
// --id1 <arg> long name is the id by default
// --long-name <arg> long name is set to something other than the id
}
func ExampleInputInfo_Required_option() {
in := cli.New().
Opt(cli.NewOpt("a")).
Opt(cli.NewOpt("b").Required())
c, _ := in.ParseThese("-a", "hello", "-b", "world")
fmt.Println(
cli.Get[string](c, "a"),
cli.Get[string](c, "b"),
)
_, err := in.ParseThese()
fmt.Println(err)
// Output:
// hello world
// cli.test: missing the following required options: -b
}
func ExampleInputInfo_Required_postionalArgument() {
in := cli.New().
Arg(cli.NewArg("a").Required()).
Arg(cli.NewArg("b"))
c, _ := in.ParseThese("hello", "world")
fmt.Println(
cli.Get[string](c, "a"),
cli.Get[string](c, "b"),
)
_, err := in.ParseThese()
fmt.Println(err)
// Output:
// hello world
// cli.test: missing the following required arguments: a
}
func ExampleInputInfo_Short() {
in := cli.New().
Opt(cli.NewOpt("flag").Short('f'))
c1 := in.ParseTheseOrExit("-f", "hello")
fmt.Println(cli.Get[string](c1, "flag"))
c2 := in.ParseTheseOrExit("--flag", "world")
fmt.Println(cli.Get[string](c2, "flag"))
// Output:
// hello
// world
}
func ExampleInputInfo_ShortOnly() {
in := cli.New().
Opt(cli.NewOpt("flag").ShortOnly('f'))
c := in.ParseTheseOrExit("-f", "hello")
fmt.Println(cli.Get[string](c, "flag"))
_, err := in.ParseThese("--flag", "helloworld")
fmt.Println(err)
// Output:
// hello
// cli.test: unknown option '--flag'
}
func ExampleInputInfo_WithHelpGen() {
in := cli.New("example").
Help("an example program").
Opt(cli.NewOpt("a")).
Opt(cli.NewOpt("b")).
Opt(cli.NewBoolOpt("h").
Help("will show a custom help message").
WithHelpGen(func(o cli.Input, c *cli.CommandInfo) string {
return "totally custom help message\n" +
"use data on the provided CommandInfo to form a custom help message\n" +
fmt.Sprintf("for example, this command has %d options including this one", len(c.Opts))
}))
_, err := in.ParseThese("-h")
fmt.Println(err)
// Output:
// totally custom help message
// use data on the provided CommandInfo to form a custom help message
// for example, this command has 3 options including this one
}
func ExampleInputInfo_WithParser() {
// This is a horrible parsing function for coordinates.
// It's just for example purposes.
pointParser := func(s string) (any, error) {
xStr, yStr, _ := strings.Cut(s, ",")
x, _ := strconv.Atoi(xStr)
y, _ := strconv.Atoi(yStr)
return image.Point{X: x, Y: y}, nil
}
c := cli.New().
Opt(cli.NewOpt("aa").WithParser(pointParser)).
ParseTheseOrExit("--aa", "3,7")
fmt.Printf("%+#v\n", cli.Get[image.Point](c, "aa"))
// Output:
// image.Point{X:3, Y:7}
}
func ExampleInputInfo_WithValueName_option() {
in := cli.New("example").
Help("example program").
Opt(cli.NewOpt("aa").WithValueName("str").Help("it says '<str>' above instead of '<arg>'"))
_, err := in.ParseThese("--help")
fmt.Println(err)
// Output:
// example - example program
//
// usage:
// example [options]
//
// options:
// --aa <str>
// it says '<str>' above instead of '<arg>'
//
// -h, --help
// Show this help message and exit.
}
func ExampleInputInfo_WithValueName_positionalArgument() {
in := cli.New("example").
Help("example program").
Arg(cli.NewArg("aa").WithValueName("filename").Help("it says '[filename]' above instead of '[aa]'"))
_, err := in.ParseThese("--help")
fmt.Println(err)
// Output:
// example - example program
//
// usage:
// example [options] [arguments]
//
// options:
// -h, --help
// Show this help message and exit.
//
// arguments:
// [filename]
// it says '[filename]' above instead of '[aa]'
}
func ExampleLookup_option() {
c := cli.New().
Opt(cli.NewOpt("a")).
Opt(cli.NewOpt("b")).
ParseTheseOrExit("-b=hello")
a, hasA := cli.Lookup[string](c, "a")
b, hasB := cli.Lookup[string](c, "b")
fmt.Printf("a: %q, %v\n", a, hasA)
fmt.Printf("b: %q, %v\n", b, hasB)
// Output:
// a: "", false
// b: "hello", true
}
func ExampleLookup_positionalArgs() {
c := cli.New().
Arg(cli.NewArg("a")).
Arg(cli.NewArg("b")).
ParseTheseOrExit("hello")
a, hasA := cli.Lookup[string](c, "a")
b, hasB := cli.Lookup[string](c, "b")
fmt.Printf("a: %q, %v\n", a, hasA)
fmt.Printf("b: %q, %v\n", b, hasB)
// Output:
// a: "hello", true
// b: "", false
}
func ExampleNewFileParser() {
in := cli.New().
Opt(cli.NewOpt("i").WithParser(cli.NewFileParser(cli.ParseInt))).
Opt(cli.NewOpt("s").WithParser(cli.NewFileParser(nil)))
c, _ := in.ParseThese(
"-i", "testdata/sample_int",
"-s", "testdata/sample_int",
)
fmt.Println(cli.Get[int](c, "i"))
fmt.Printf("%q\n", cli.Get[string](c, "s"))
_, err := in.ParseThese("-i", "testdata/sample_empty")
fmt.Println(err)
_, err = in.ParseThese("-i", "path_that_doesnt_exist")
fmt.Println(err)
// Output:
// 12345
// "12345"
// parsing option 'i': invalid syntax
// parsing option 'i': open path_that_doesnt_exist: no such file or directory
}
func ExampleNewTimeParser() {
in := cli.New().
Opt(cli.NewOpt("t").WithParser(cli.NewTimeParser("2006-01-02")))
c := in.ParseTheseOrExit("-t", "2025-04-12")
fmt.Println(cli.Get[time.Time](c, "t"))
_, err := in.ParseThese("-t", "hello")
fmt.Println(err)
// Output:
// 2025-04-12 00:00:00 +0000 UTC
// parsing option 't': parsing time "hello" as "2006-01-02": cannot parse "hello" as "2006"
}
func ExampleParseDuration() {
in := cli.New().
Opt(cli.NewOpt("d").WithParser(cli.ParseDuration))
c := in.ParseTheseOrExit("-d", "1h2m3s")
fmt.Println(cli.Get[time.Duration](c, "d"))
_, err := in.ParseThese("-d", "not_a_duration")
fmt.Println(err)
// Output:
// 1h2m3s
// parsing option 'd': time: invalid duration "not_a_duration"
}
func ExampleParseURL() {
in := cli.New().
Opt(cli.NewOpt("u").WithParser(cli.ParseURL))
c := in.ParseTheseOrExit("-u", "https://pkg.go.dev/github.com/steverusso/cli#ParseURL")
fmt.Println(cli.Get[*url.URL](c, "u"))
_, err := in.ParseThese("-u", "ftp://usr:pass[wd]@svr.com")
fmt.Println(err)
// Output:
// https://pkg.go.dev/github.com/steverusso/cli#ParseURL
// parsing option 'u': parse "ftp://usr:pass[wd]@svr.com": net/url: invalid userinfo
}