Skip to content

Commit e409044

Browse files
committed
Make ListParam cumulative in kingpin's context
1 parent 4e0f2df commit e409044

File tree

1 file changed

+55
-2
lines changed

1 file changed

+55
-2
lines changed

schema/schema.go

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func (c *Config) Vars() map[string]string {
6060
return vars
6161
}
6262

63+
// EnvVars returns a map with environment variables for all parameters
6364
func (c *Config) EnvVars() map[string]string {
6465
vars := make(map[string]string, len(c.Params))
6566
for _, p := range c.Params {
@@ -69,6 +70,7 @@ func (c *Config) EnvVars() map[string]string {
6970
return vars
7071
}
7172

73+
// Args returns the list of arguments for all parameters
7274
func (c *Config) Args() []string {
7375
args := []string{}
7476
for _, p := range c.Params {
@@ -134,15 +136,21 @@ func SetParam(p Param, s kingpin.Settings) {
134136
s.SetValue(p)
135137
}
136138

139+
// Param is the command line parameter
137140
type Param interface {
141+
// Name is the parameter's name
138142
Name() string
143+
// CLIName specifies the name of the parameter as given on command line
139144
CLIName() string
145+
// Description specifies human-friendly parameter's description
140146
Description() string
141147
Check() string
148+
// Required is whether the parameter is required
142149
Required() bool
150+
// Default returns the default value for the parameter
143151
Default() string
144152

145-
// New returns a new instance of the param identical to this
153+
// New clones this parameter
146154
New() Param
147155

148156
// Set is required to set parameters from command line string
@@ -153,12 +161,14 @@ type Param interface {
153161
// Args returns argument strings in cli format
154162
Args() []string
155163

156-
// Values returns a tuple with environment variable name and value
164+
// EnvVars returns a tuple with environment variable name and value
157165
EnvVars() (string, string)
158166

159167
// Vars returns a tuple with the variable name and value
160168
Vars() (string, string)
161169

170+
// EnvName specifies the name of the environment variable that specifies
171+
// the value for the parameter
162172
EnvName() string
163173
}
164174

@@ -336,22 +346,27 @@ type PathParam struct {
336346
val *string
337347
}
338348

349+
// New clones this parameter
339350
func (p *PathParam) New() Param {
340351
return &PathParam{p.paramCommon, nil}
341352
}
342353

354+
// Args returns argument strings in cli format
343355
func (p *PathParam) Args() []string {
344356
return []string{fmt.Sprintf("--%v", p.CLIName()), p.String()}
345357
}
346358

359+
// EnvVars returns a tuple with environment variable name and value
347360
func (p *PathParam) EnvVars() (string, string) {
348361
return p.EnvName(), p.String()
349362
}
350363

364+
// Vars returns a tuple with the variable name and value
351365
func (p *PathParam) Vars() (string, string) {
352366
return p.Name(), p.String()
353367
}
354368

369+
// Set is required to set parameters from command line string
355370
func (p *PathParam) Set(s string) error {
356371
p.val = &s
357372
return nil
@@ -369,10 +384,12 @@ type StringParam struct {
369384
val *string
370385
}
371386

387+
// New clones this parameter
372388
func (p *StringParam) New() Param {
373389
return &StringParam{p.paramCommon, nil}
374390
}
375391

392+
// Set is required to set parameters from command line string
376393
func (p *StringParam) Set(s string) error {
377394
p.val = &s
378395
return nil
@@ -385,14 +402,17 @@ func (p *StringParam) String() string {
385402
return *p.val
386403
}
387404

405+
// Args returns argument strings in cli format
388406
func (p *StringParam) Args() []string {
389407
return []string{fmt.Sprintf("--%v", p.CLIName()), p.String()}
390408
}
391409

410+
// EnvVars returns a tuple with environment variable name and value
392411
func (p *StringParam) EnvVars() (string, string) {
393412
return p.EnvName(), p.String()
394413
}
395414

415+
// Vars returns a tuple with the variable name and value
396416
func (p *StringParam) Vars() (string, string) {
397417
return p.Name(), p.String()
398418
}
@@ -402,14 +422,17 @@ type BoolParam struct {
402422
val *bool
403423
}
404424

425+
// New clones this parameter
405426
func (p *BoolParam) New() Param {
406427
return &BoolParam{p.paramCommon, nil}
407428
}
408429

430+
// Vars returns a tuple with the variable name and value
409431
func (p *BoolParam) Vars() (string, string) {
410432
return p.Name(), p.String()
411433
}
412434

435+
// Set is required to set parameters from command line string
413436
func (p *BoolParam) Set(s string) error {
414437
v, err := strconv.ParseBool(s)
415438
if err != nil {
@@ -426,10 +449,12 @@ func (p *BoolParam) String() string {
426449
return fmt.Sprintf("%v", *p.val)
427450
}
428451

452+
// Args returns argument strings in cli format
429453
func (p *BoolParam) Args() []string {
430454
return []string{fmt.Sprintf("--%v", p.CLIName()), p.String()}
431455
}
432456

457+
// EnvVars returns a tuple with environment variable name and value
433458
func (p *BoolParam) EnvVars() (string, string) {
434459
return p.EnvName(), p.String()
435460
}
@@ -439,6 +464,7 @@ type IntParam struct {
439464
val *int64
440465
}
441466

467+
// Set is required to set parameters from command line string
442468
func (p *IntParam) Set(s string) error {
443469
v, err := strconv.ParseInt(s, 0, 64)
444470
if err != nil {
@@ -455,36 +481,45 @@ func (p *IntParam) String() string {
455481
return fmt.Sprintf("%v", *p.val)
456482
}
457483

484+
// New clones this parameter
458485
func (p *IntParam) New() Param {
459486
return &IntParam{p.paramCommon, nil}
460487
}
461488

489+
// Args returns argument strings in cli format
462490
func (p *IntParam) Args() []string {
463491
return []string{fmt.Sprintf("--%v", p.CLIName()), p.String()}
464492
}
465493

494+
// EnvVars returns a tuple with environment variable name and value
466495
func (p *IntParam) EnvVars() (string, string) {
467496
return p.EnvName(), p.String()
468497
}
469498

499+
// Vars returns a tuple with the variable name and value
470500
func (p *IntParam) Vars() (string, string) {
471501
return p.Name(), p.String()
472502
}
473503

504+
// ListParam defines a flag that accumulates multiple values
474505
type ListParam struct {
475506
paramCommon
476507
el Param
477508
values []Param
478509
}
479510

511+
// CLIName specifies the name of the parameter as given on command line
480512
func (p *ListParam) CLIName() string {
481513
return p.el.CLIName()
482514
}
483515

516+
// EnvName specifies the name of the environment variable that specifies
517+
// the value for the parameter
484518
func (p *ListParam) EnvName() string {
485519
return p.el.EnvName()
486520
}
487521

522+
// Set is required to set parameters from command line string
488523
func (p *ListParam) Set(s string) error {
489524
// this is to support setting from environment variables
490525
values := cstrings.Split(',', '\\', s)
@@ -498,6 +533,7 @@ func (p *ListParam) Set(s string) error {
498533
return nil
499534
}
500535

536+
// New clones this parameter
501537
func (p *ListParam) New() Param {
502538
return &ListParam{p.paramCommon, p.el, nil}
503539
}
@@ -513,6 +549,7 @@ func (p *ListParam) String() string {
513549
return fmt.Sprintf("[%v]", strings.Join(out, ","))
514550
}
515551

552+
// Args returns argument strings in cli format
516553
func (p *ListParam) Args() []string {
517554
if len(p.values) == 0 {
518555
return []string{}
@@ -524,6 +561,7 @@ func (p *ListParam) Args() []string {
524561
return out
525562
}
526563

564+
// EnvVars returns a tuple with environment variable name and value
527565
func (p *ListParam) EnvVars() (string, string) {
528566
if len(p.values) == 0 {
529567
return p.EnvName(), p.Default()
@@ -535,6 +573,7 @@ func (p *ListParam) EnvVars() (string, string) {
535573
return p.el.EnvName(), strings.Join(out, ",")
536574
}
537575

576+
// Vars returns a tuple with the variable name and value
538577
func (p *ListParam) Vars() (string, string) {
539578
if len(p.values) == 0 {
540579
return p.Name(), p.Default()
@@ -546,6 +585,12 @@ func (p *ListParam) Vars() (string, string) {
546585
return p.Name(), strings.Join(out, ",")
547586
}
548587

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+
549594
type KVParam struct {
550595
paramCommon
551596
separator string
@@ -560,6 +605,7 @@ func (p *KVParam) sep() string {
560605
return ""
561606
}
562607

608+
// Set is required to set parameters from command line string
563609
func (p *KVParam) Set(s string) error {
564610
sep := p.sep()
565611

@@ -592,6 +638,7 @@ func (p *KVParam) String() string {
592638
return fmt.Sprintf("{%v}", strings.Join(out, p.sep()))
593639
}
594640

641+
// New clones this parameter
595642
func (p *KVParam) New() Param {
596643
keys := make([]Param, len(p.keys))
597644
for i, k := range p.keys {
@@ -600,6 +647,7 @@ func (p *KVParam) New() Param {
600647
return &KVParam{p.paramCommon, p.separator, keys, nil}
601648
}
602649

650+
// Args returns argument strings in cli format
603651
func (p *KVParam) Args() []string {
604652
if len(p.values) == 0 {
605653
return []string{}
@@ -612,6 +660,7 @@ func (p *KVParam) Args() []string {
612660
fmt.Sprintf("--%v", p.CLIName()), strings.Join(vals, p.sep())}
613661
}
614662

663+
// EnvVars returns a tuple with environment variable name and value
615664
func (p *KVParam) EnvVars() (string, string) {
616665
if len(p.values) == 0 {
617666
return p.EnvName(), p.Default()
@@ -640,6 +689,7 @@ type EnumParam struct {
640689
value *string
641690
}
642691

692+
// Set is required to set parameters from command line string
643693
func (p *EnumParam) Set(s string) error {
644694
found := false
645695
for _, v := range p.values {
@@ -664,17 +714,20 @@ func (p *EnumParam) String() string {
664714
return *p.value
665715
}
666716

717+
// New clones this parameter
667718
func (p *EnumParam) New() Param {
668719
return &EnumParam{p.paramCommon, p.values, nil}
669720
}
670721

722+
// Args returns argument strings in cli format
671723
func (p *EnumParam) Args() []string {
672724
if p.value == nil {
673725
return []string{}
674726
}
675727
return []string{fmt.Sprintf("--%v", p.CLIName()), *p.value}
676728
}
677729

730+
// EnvVars returns a tuple with environment variable name and value
678731
func (p *EnumParam) EnvVars() (string, string) {
679732
return p.EnvName(), p.String()
680733
}

0 commit comments

Comments
 (0)