Print defaults for multiples
Check if the default value supplied is a slice and not nil, if so print the list of values supplied. Test case for slice argument with and without default values. Default values for slices was not printed because slice is not comparable, but the zero value for slices is nil.
This commit is contained in:
parent
45474a9b25
commit
e71d6514f4
2
usage.go
2
usage.go
|
@ -117,7 +117,7 @@ func printOption(w io.Writer, spec *spec) {
|
|||
v := spec.dest
|
||||
if v.IsValid() {
|
||||
z := reflect.Zero(v.Type())
|
||||
if v.Type().Comparable() && z.Type().Comparable() && v.Interface() != z.Interface() {
|
||||
if (v.Type().Comparable() && z.Type().Comparable() && v.Interface() != z.Interface()) || v.Kind() == reflect.Slice && !v.IsNil() {
|
||||
fmt.Fprintf(w, " [default: %v]", v)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,9 +10,9 @@ import (
|
|||
)
|
||||
|
||||
func TestWriteUsage(t *testing.T) {
|
||||
expectedUsage := "usage: example [--name NAME] [--value VALUE] [--verbose] [--dataset DATASET] [--optimize OPTIMIZE] [--ids IDS] [--workers WORKERS] INPUT [OUTPUT [OUTPUT ...]]\n"
|
||||
expectedUsage := "usage: example [--name NAME] [--value VALUE] [--verbose] [--dataset DATASET] [--optimize OPTIMIZE] [--ids IDS] [--values VALUES] [--workers WORKERS] INPUT [OUTPUT [OUTPUT ...]]\n"
|
||||
|
||||
expectedHelp := `usage: example [--name NAME] [--value VALUE] [--verbose] [--dataset DATASET] [--optimize OPTIMIZE] [--ids IDS] [--workers WORKERS] INPUT [OUTPUT [OUTPUT ...]]
|
||||
expectedHelp := `usage: example [--name NAME] [--value VALUE] [--verbose] [--dataset DATASET] [--optimize OPTIMIZE] [--ids IDS] [--values VALUES] [--workers WORKERS] INPUT [OUTPUT [OUTPUT ...]]
|
||||
|
||||
positional arguments:
|
||||
input
|
||||
|
@ -26,6 +26,7 @@ options:
|
|||
--optimize OPTIMIZE, -O OPTIMIZE
|
||||
optimization level
|
||||
--ids IDS Ids
|
||||
--values VALUES Values [default: [3.14 42 256]]
|
||||
--workers WORKERS, -w WORKERS
|
||||
number of workers to start
|
||||
--help, -h display this help and exit
|
||||
|
@ -39,10 +40,12 @@ options:
|
|||
Dataset string `arg:"help:dataset to use"`
|
||||
Optimize int `arg:"-O,help:optimization level"`
|
||||
Ids []int64 `arg:"help:Ids"`
|
||||
Values []float64 `arg:"help:Values"`
|
||||
Workers int `arg:"-w,env:WORKERS,help:number of workers to start"`
|
||||
}
|
||||
args.Name = "Foo Bar"
|
||||
args.Value = 42
|
||||
args.Values = []float64{3.14, 42, 256}
|
||||
p, err := NewParser(Config{}, &args)
|
||||
require.NoError(t, err)
|
||||
|
||||
|
|
Loading…
Reference in New Issue