Fix error when printing usage for multi-value arguments

We try to compare []strings, which are uncomparable types:

`panic: runtime error: comparing uncomparable type []string`
This commit is contained in:
Alex Rakoczy 2015-12-04 09:59:13 -05:00
parent ce5525d776
commit e4e9e19427
2 changed files with 5 additions and 3 deletions

View File

@ -107,7 +107,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.Interface() != z.Interface() { if v.Type().Comparable() && z.Type().Comparable() && v.Interface() != z.Interface() {
fmt.Fprintf(w, " [default: %v]", v) fmt.Fprintf(w, " [default: %v]", v)
} }
} }

View File

@ -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] INPUT [OUTPUT [OUTPUT ...]]\n" expectedUsage := "usage: example [--name NAME] [--value VALUE] [--verbose] [--dataset DATASET] [--optimize OPTIMIZE] [--ids IDS] INPUT [OUTPUT [OUTPUT ...]]\n"
expectedHelp := `usage: example [--name NAME] [--value VALUE] [--verbose] [--dataset DATASET] [--optimize OPTIMIZE] INPUT [OUTPUT [OUTPUT ...]] expectedHelp := `usage: example [--name NAME] [--value VALUE] [--verbose] [--dataset DATASET] [--optimize OPTIMIZE] [--ids IDS] INPUT [OUTPUT [OUTPUT ...]]
positional arguments: positional arguments:
input input
@ -25,6 +25,7 @@ options:
--dataset DATASET dataset to use --dataset DATASET dataset to use
--optimize OPTIMIZE, -O OPTIMIZE --optimize OPTIMIZE, -O OPTIMIZE
optimization level optimization level
--ids IDS Ids
--help, -h display this help and exit --help, -h display this help and exit
` `
var args struct { var args struct {
@ -35,6 +36,7 @@ options:
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"`
} }
args.Name = "Foo Bar" args.Name = "Foo Bar"
args.Value = 42 args.Value = 42