@@ -60,6 +60,7 @@ func (c *Config) Vars() map[string]string {
60
60
return vars
61
61
}
62
62
63
+ // EnvVars returns a map with environment variables for all parameters
63
64
func (c * Config ) EnvVars () map [string ]string {
64
65
vars := make (map [string ]string , len (c .Params ))
65
66
for _ , p := range c .Params {
@@ -69,6 +70,7 @@ func (c *Config) EnvVars() map[string]string {
69
70
return vars
70
71
}
71
72
73
+ // Args returns the list of arguments for all parameters
72
74
func (c * Config ) Args () []string {
73
75
args := []string {}
74
76
for _ , p := range c .Params {
@@ -134,15 +136,21 @@ func SetParam(p Param, s kingpin.Settings) {
134
136
s .SetValue (p )
135
137
}
136
138
139
+ // Param is the command line parameter
137
140
type Param interface {
141
+ // Name is the parameter's name
138
142
Name () string
143
+ // CLIName specifies the name of the parameter as given on command line
139
144
CLIName () string
145
+ // Description specifies human-friendly parameter's description
140
146
Description () string
141
147
Check () string
148
+ // Required is whether the parameter is required
142
149
Required () bool
150
+ // Default returns the default value for the parameter
143
151
Default () string
144
152
145
- // New returns a new instance of the param identical to this
153
+ // New clones this parameter
146
154
New () Param
147
155
148
156
// Set is required to set parameters from command line string
@@ -153,12 +161,14 @@ type Param interface {
153
161
// Args returns argument strings in cli format
154
162
Args () []string
155
163
156
- // Values returns a tuple with environment variable name and value
164
+ // EnvVars returns a tuple with environment variable name and value
157
165
EnvVars () (string , string )
158
166
159
167
// Vars returns a tuple with the variable name and value
160
168
Vars () (string , string )
161
169
170
+ // EnvName specifies the name of the environment variable that specifies
171
+ // the value for the parameter
162
172
EnvName () string
163
173
}
164
174
@@ -336,22 +346,27 @@ type PathParam struct {
336
346
val * string
337
347
}
338
348
349
+ // New clones this parameter
339
350
func (p * PathParam ) New () Param {
340
351
return & PathParam {p .paramCommon , nil }
341
352
}
342
353
354
+ // Args returns argument strings in cli format
343
355
func (p * PathParam ) Args () []string {
344
356
return []string {fmt .Sprintf ("--%v" , p .CLIName ()), p .String ()}
345
357
}
346
358
359
+ // EnvVars returns a tuple with environment variable name and value
347
360
func (p * PathParam ) EnvVars () (string , string ) {
348
361
return p .EnvName (), p .String ()
349
362
}
350
363
364
+ // Vars returns a tuple with the variable name and value
351
365
func (p * PathParam ) Vars () (string , string ) {
352
366
return p .Name (), p .String ()
353
367
}
354
368
369
+ // Set is required to set parameters from command line string
355
370
func (p * PathParam ) Set (s string ) error {
356
371
p .val = & s
357
372
return nil
@@ -369,10 +384,12 @@ type StringParam struct {
369
384
val * string
370
385
}
371
386
387
+ // New clones this parameter
372
388
func (p * StringParam ) New () Param {
373
389
return & StringParam {p .paramCommon , nil }
374
390
}
375
391
392
+ // Set is required to set parameters from command line string
376
393
func (p * StringParam ) Set (s string ) error {
377
394
p .val = & s
378
395
return nil
@@ -385,14 +402,17 @@ func (p *StringParam) String() string {
385
402
return * p .val
386
403
}
387
404
405
+ // Args returns argument strings in cli format
388
406
func (p * StringParam ) Args () []string {
389
407
return []string {fmt .Sprintf ("--%v" , p .CLIName ()), p .String ()}
390
408
}
391
409
410
+ // EnvVars returns a tuple with environment variable name and value
392
411
func (p * StringParam ) EnvVars () (string , string ) {
393
412
return p .EnvName (), p .String ()
394
413
}
395
414
415
+ // Vars returns a tuple with the variable name and value
396
416
func (p * StringParam ) Vars () (string , string ) {
397
417
return p .Name (), p .String ()
398
418
}
@@ -402,14 +422,17 @@ type BoolParam struct {
402
422
val * bool
403
423
}
404
424
425
+ // New clones this parameter
405
426
func (p * BoolParam ) New () Param {
406
427
return & BoolParam {p .paramCommon , nil }
407
428
}
408
429
430
+ // Vars returns a tuple with the variable name and value
409
431
func (p * BoolParam ) Vars () (string , string ) {
410
432
return p .Name (), p .String ()
411
433
}
412
434
435
+ // Set is required to set parameters from command line string
413
436
func (p * BoolParam ) Set (s string ) error {
414
437
v , err := strconv .ParseBool (s )
415
438
if err != nil {
@@ -426,10 +449,12 @@ func (p *BoolParam) String() string {
426
449
return fmt .Sprintf ("%v" , * p .val )
427
450
}
428
451
452
+ // Args returns argument strings in cli format
429
453
func (p * BoolParam ) Args () []string {
430
454
return []string {fmt .Sprintf ("--%v" , p .CLIName ()), p .String ()}
431
455
}
432
456
457
+ // EnvVars returns a tuple with environment variable name and value
433
458
func (p * BoolParam ) EnvVars () (string , string ) {
434
459
return p .EnvName (), p .String ()
435
460
}
@@ -439,6 +464,7 @@ type IntParam struct {
439
464
val * int64
440
465
}
441
466
467
+ // Set is required to set parameters from command line string
442
468
func (p * IntParam ) Set (s string ) error {
443
469
v , err := strconv .ParseInt (s , 0 , 64 )
444
470
if err != nil {
@@ -455,36 +481,45 @@ func (p *IntParam) String() string {
455
481
return fmt .Sprintf ("%v" , * p .val )
456
482
}
457
483
484
+ // New clones this parameter
458
485
func (p * IntParam ) New () Param {
459
486
return & IntParam {p .paramCommon , nil }
460
487
}
461
488
489
+ // Args returns argument strings in cli format
462
490
func (p * IntParam ) Args () []string {
463
491
return []string {fmt .Sprintf ("--%v" , p .CLIName ()), p .String ()}
464
492
}
465
493
494
+ // EnvVars returns a tuple with environment variable name and value
466
495
func (p * IntParam ) EnvVars () (string , string ) {
467
496
return p .EnvName (), p .String ()
468
497
}
469
498
499
+ // Vars returns a tuple with the variable name and value
470
500
func (p * IntParam ) Vars () (string , string ) {
471
501
return p .Name (), p .String ()
472
502
}
473
503
504
+ // ListParam defines a flag that accumulates multiple values
474
505
type ListParam struct {
475
506
paramCommon
476
507
el Param
477
508
values []Param
478
509
}
479
510
511
+ // CLIName specifies the name of the parameter as given on command line
480
512
func (p * ListParam ) CLIName () string {
481
513
return p .el .CLIName ()
482
514
}
483
515
516
+ // EnvName specifies the name of the environment variable that specifies
517
+ // the value for the parameter
484
518
func (p * ListParam ) EnvName () string {
485
519
return p .el .EnvName ()
486
520
}
487
521
522
+ // Set is required to set parameters from command line string
488
523
func (p * ListParam ) Set (s string ) error {
489
524
// this is to support setting from environment variables
490
525
values := cstrings .Split (',' , '\\' , s )
@@ -498,6 +533,7 @@ func (p *ListParam) Set(s string) error {
498
533
return nil
499
534
}
500
535
536
+ // New clones this parameter
501
537
func (p * ListParam ) New () Param {
502
538
return & ListParam {p .paramCommon , p .el , nil }
503
539
}
@@ -513,6 +549,7 @@ func (p *ListParam) String() string {
513
549
return fmt .Sprintf ("[%v]" , strings .Join (out , "," ))
514
550
}
515
551
552
+ // Args returns argument strings in cli format
516
553
func (p * ListParam ) Args () []string {
517
554
if len (p .values ) == 0 {
518
555
return []string {}
@@ -524,6 +561,7 @@ func (p *ListParam) Args() []string {
524
561
return out
525
562
}
526
563
564
+ // EnvVars returns a tuple with environment variable name and value
527
565
func (p * ListParam ) EnvVars () (string , string ) {
528
566
if len (p .values ) == 0 {
529
567
return p .EnvName (), p .Default ()
@@ -535,6 +573,7 @@ func (p *ListParam) EnvVars() (string, string) {
535
573
return p .el .EnvName (), strings .Join (out , "," )
536
574
}
537
575
576
+ // Vars returns a tuple with the variable name and value
538
577
func (p * ListParam ) Vars () (string , string ) {
539
578
if len (p .values ) == 0 {
540
579
return p .Name (), p .Default ()
@@ -546,6 +585,12 @@ func (p *ListParam) Vars() (string, string) {
546
585
return p .Name (), strings .Join (out , "," )
547
586
}
548
587
588
+ // IsCumulative determines whether the flag is cumulative - i.e. can be specified multiple times.
589
+ // Implements kingpin.repeatableFlag
590
+ func (p * ListParam ) IsCumulative () bool {
591
+ return true
592
+ }
593
+
549
594
type KVParam struct {
550
595
paramCommon
551
596
separator string
@@ -560,6 +605,7 @@ func (p *KVParam) sep() string {
560
605
return ""
561
606
}
562
607
608
+ // Set is required to set parameters from command line string
563
609
func (p * KVParam ) Set (s string ) error {
564
610
sep := p .sep ()
565
611
@@ -592,6 +638,7 @@ func (p *KVParam) String() string {
592
638
return fmt .Sprintf ("{%v}" , strings .Join (out , p .sep ()))
593
639
}
594
640
641
+ // New clones this parameter
595
642
func (p * KVParam ) New () Param {
596
643
keys := make ([]Param , len (p .keys ))
597
644
for i , k := range p .keys {
@@ -600,6 +647,7 @@ func (p *KVParam) New() Param {
600
647
return & KVParam {p .paramCommon , p .separator , keys , nil }
601
648
}
602
649
650
+ // Args returns argument strings in cli format
603
651
func (p * KVParam ) Args () []string {
604
652
if len (p .values ) == 0 {
605
653
return []string {}
@@ -612,6 +660,7 @@ func (p *KVParam) Args() []string {
612
660
fmt .Sprintf ("--%v" , p .CLIName ()), strings .Join (vals , p .sep ())}
613
661
}
614
662
663
+ // EnvVars returns a tuple with environment variable name and value
615
664
func (p * KVParam ) EnvVars () (string , string ) {
616
665
if len (p .values ) == 0 {
617
666
return p .EnvName (), p .Default ()
@@ -640,6 +689,7 @@ type EnumParam struct {
640
689
value * string
641
690
}
642
691
692
+ // Set is required to set parameters from command line string
643
693
func (p * EnumParam ) Set (s string ) error {
644
694
found := false
645
695
for _ , v := range p .values {
@@ -664,17 +714,20 @@ func (p *EnumParam) String() string {
664
714
return * p .value
665
715
}
666
716
717
+ // New clones this parameter
667
718
func (p * EnumParam ) New () Param {
668
719
return & EnumParam {p .paramCommon , p .values , nil }
669
720
}
670
721
722
+ // Args returns argument strings in cli format
671
723
func (p * EnumParam ) Args () []string {
672
724
if p .value == nil {
673
725
return []string {}
674
726
}
675
727
return []string {fmt .Sprintf ("--%v" , p .CLIName ()), * p .value }
676
728
}
677
729
730
+ // EnvVars returns a tuple with environment variable name and value
678
731
func (p * EnumParam ) EnvVars () (string , string ) {
679
732
return p .EnvName (), p .String ()
680
733
}
0 commit comments