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
|
v := spec.dest
|
||||||
if v.IsValid() {
|
if v.IsValid() {
|
||||||
z := reflect.Zero(v.Type())
|
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)
|
fmt.Fprintf(w, " [default: %v]", v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,9 +10,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestWriteUsage(t *testing.T) {
|
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:
|
positional arguments:
|
||||||
input
|
input
|
||||||
|
@ -26,23 +26,26 @@ options:
|
||||||
--optimize OPTIMIZE, -O OPTIMIZE
|
--optimize OPTIMIZE, -O OPTIMIZE
|
||||||
optimization level
|
optimization level
|
||||||
--ids IDS Ids
|
--ids IDS Ids
|
||||||
|
--values VALUES Values [default: [3.14 42 256]]
|
||||||
--workers WORKERS, -w WORKERS
|
--workers WORKERS, -w WORKERS
|
||||||
number of workers to start
|
number of workers to start
|
||||||
--help, -h display this help and exit
|
--help, -h display this help and exit
|
||||||
`
|
`
|
||||||
var args struct {
|
var args struct {
|
||||||
Input string `arg:"positional"`
|
Input string `arg:"positional"`
|
||||||
Output []string `arg:"positional,help:list of outputs"`
|
Output []string `arg:"positional,help:list of outputs"`
|
||||||
Name string `arg:"help:name to use"`
|
Name string `arg:"help:name to use"`
|
||||||
Value int `arg:"help:secret value"`
|
Value int `arg:"help:secret value"`
|
||||||
Verbose bool `arg:"-v,help:verbosity level"`
|
Verbose bool `arg:"-v,help:verbosity level"`
|
||||||
Dataset string `arg:"help:dataset to use"`
|
Dataset string `arg:"help:dataset to use"`
|
||||||
Optimize int `arg:"-O,help:optimization level"`
|
Optimize int `arg:"-O,help:optimization level"`
|
||||||
Ids []int64 `arg:"help:Ids"`
|
Ids []int64 `arg:"help:Ids"`
|
||||||
Workers int `arg:"-w,env:WORKERS,help:number of workers to start"`
|
Values []float64 `arg:"help:Values"`
|
||||||
|
Workers int `arg:"-w,env:WORKERS,help:number of workers to start"`
|
||||||
}
|
}
|
||||||
args.Name = "Foo Bar"
|
args.Name = "Foo Bar"
|
||||||
args.Value = 42
|
args.Value = 42
|
||||||
|
args.Values = []float64{3.14, 42, 256}
|
||||||
p, err := NewParser(Config{}, &args)
|
p, err := NewParser(Config{}, &args)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue