aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/spf13/pflag
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/spf13/pflag')
-rw-r--r--vendor/github.com/spf13/pflag/flag.go51
-rw-r--r--vendor/github.com/spf13/pflag/string_slice.go23
2 files changed, 45 insertions, 29 deletions
diff --git a/vendor/github.com/spf13/pflag/flag.go b/vendor/github.com/spf13/pflag/flag.go
index 965df13..eb143d7 100644
--- a/vendor/github.com/spf13/pflag/flag.go
+++ b/vendor/github.com/spf13/pflag/flag.go
@@ -419,20 +419,26 @@ func (f *FlagSet) PrintDefaults() {
fmt.Fprintf(f.out(), "%s", usages)
}
-// isZeroValue guesses whether the string represents the zero
-// value for a flag. It is not accurate but in practice works OK.
-func isZeroValue(value string) bool {
- switch value {
- case "false":
- return true
- case "<nil>":
- return true
- case "":
- return true
- case "0":
+// defaultIsZeroValue returns true if the default value for this flag represents
+// a zero value.
+func (f *Flag) defaultIsZeroValue() bool {
+ switch f.Value.(type) {
+ case boolFlag:
+ return f.DefValue == "false"
+ case *durationValue:
+ // Beginning in Go 1.7, duration zero values are "0s"
+ return f.DefValue == "0" || f.DefValue == "0s"
+ case *intValue, *int8Value, *int32Value, *int64Value, *uintValue, *uint8Value, *uint16Value, *uint32Value, *uint64Value, *countValue, *float32Value, *float64Value:
+ return f.DefValue == "0"
+ case *stringValue:
+ return f.DefValue == ""
+ case *ipValue, *ipMaskValue, *ipNetValue:
+ return f.DefValue == "<nil>"
+ case *intSliceValue, *stringSliceValue:
+ return f.DefValue == "[]"
+ default:
return true
}
- return false
}
// UnquoteUsage extracts a back-quoted name from the usage
@@ -455,22 +461,19 @@ func UnquoteUsage(flag *Flag) (name string, usage string) {
break // Only one back quote; use type name.
}
}
- // No explicit name, so use type if we can find one.
- name = "value"
- switch flag.Value.(type) {
- case boolFlag:
+
+ name = flag.Value.Type()
+ switch name {
+ case "bool":
name = ""
- case *durationValue:
- name = "duration"
- case *float64Value:
+ case "float64":
name = "float"
- case *intValue, *int64Value:
+ case "int64":
name = "int"
- case *stringValue:
- name = "string"
- case *uintValue, *uint64Value:
+ case "uint64":
name = "uint"
}
+
return
}
@@ -519,7 +522,7 @@ func (f *FlagSet) FlagUsages() string {
}
line += usage
- if !isZeroValue(flag.DefValue) {
+ if !flag.defaultIsZeroValue() {
if flag.Value.Type() == "string" {
line += fmt.Sprintf(" (default %q)", flag.DefValue)
} else {
diff --git a/vendor/github.com/spf13/pflag/string_slice.go b/vendor/github.com/spf13/pflag/string_slice.go
index b53648b..927a440 100644
--- a/vendor/github.com/spf13/pflag/string_slice.go
+++ b/vendor/github.com/spf13/pflag/string_slice.go
@@ -1,6 +1,7 @@
package pflag
import (
+ "bytes"
"encoding/csv"
"fmt"
"strings"
@@ -21,10 +22,17 @@ func newStringSliceValue(val []string, p *[]string) *stringSliceValue {
return ssv
}
-func (s *stringSliceValue) Set(val string) error {
+func readAsCSV(val string) ([]string, error) {
+ if val == "" {
+ return []string{}, nil
+ }
stringReader := strings.NewReader(val)
csvReader := csv.NewReader(stringReader)
- v, err := csvReader.Read()
+ return csvReader.Read()
+}
+
+func (s *stringSliceValue) Set(val string) error {
+ v, err := readAsCSV(val)
if err != nil {
return err
}
@@ -41,7 +49,13 @@ func (s *stringSliceValue) Type() string {
return "stringSlice"
}
-func (s *stringSliceValue) String() string { return "[" + strings.Join(*s.value, ",") + "]" }
+func (s *stringSliceValue) String() string {
+ b := &bytes.Buffer{}
+ w := csv.NewWriter(b)
+ w.Write(*s.value)
+ w.Flush()
+ return "[" + strings.TrimSuffix(b.String(), fmt.Sprintln()) + "]"
+}
func stringSliceConv(sval string) (interface{}, error) {
sval = strings.Trim(sval, "[]")
@@ -49,8 +63,7 @@ func stringSliceConv(sval string) (interface{}, error) {
if len(sval) == 0 {
return []string{}, nil
}
- v := strings.Split(sval, ",")
- return v, nil
+ return readAsCSV(sval)
}
// GetStringSlice return the []string value of a flag with the given name