aboutsummaryrefslogtreecommitdiff
path: root/flag_test.go
diff options
context:
space:
mode:
authorEric Paris <[email protected]>2016-08-16 14:05:11 -0400
committerGitHub <[email protected]>2016-08-16 14:05:11 -0400
commit4f9190456aed1c2113ca51ea9b89219747458dc1 (patch)
tree91ff7dd057731d7010d888fb5395d5899c59e380 /flag_test.go
parent01665e1eb3d0533dca0e5acffdff9eec1fcb886f (diff)
parentb2cdba01d8c7de00a8924812f774f33ee97f709a (diff)
Merge pull request #78 from moorereason/go1.7-zerovalues
Fix default value detection for Go 1.7
Diffstat (limited to 'flag_test.go')
-rw-r--r--flag_test.go51
1 files changed, 40 insertions, 11 deletions
diff --git a/flag_test.go b/flag_test.go
index 0ae2e4f..86b37e5 100644
--- a/flag_test.go
+++ b/flag_test.go
@@ -13,6 +13,7 @@ import (
"os"
"reflect"
"sort"
+ "strconv"
"strings"
"testing"
"time"
@@ -873,19 +874,38 @@ func TestHiddenFlagUsage(t *testing.T) {
}
}
-const defaultOutput = ` --A for bootstrapping, allow 'any' type
- --Alongflagname disable bounds checking
- -C, --CCC a boolean defaulting to true (default true)
- --D path set relative path for local imports
- --F number a non-zero number (default 2.7)
- --G float a float that defaults to zero
- --N int a non-zero int (default 27)
- --ND1 string[="bar"] a string with NoOptDefVal (default "foo")
- --ND2 num[=4321] a num with NoOptDefVal (default 1234)
- --Z int an int that defaults to zero
- --maxT timeout set timeout for dial
+const defaultOutput = ` --A for bootstrapping, allow 'any' type
+ --Alongflagname disable bounds checking
+ -C, --CCC a boolean defaulting to true (default true)
+ --D path set relative path for local imports
+ --F number a non-zero number (default 2.7)
+ --G float a float that defaults to zero
+ --IP ip IP address with no default
+ --IPMask ipMask Netmask address with no default
+ --IPNet ipNet IP network with no default
+ --Ints intSlice int slice with zero default
+ --N int a non-zero int (default 27)
+ --ND1 string[="bar"] a string with NoOptDefVal (default "foo")
+ --ND2 num[=4321] a num with NoOptDefVal (default 1234)
+ --Strings stringSlice string slice with zero default
+ --Z int an int that defaults to zero
+ --custom custom custom Value implementation
+ --maxT timeout set timeout for dial
`
+// Custom value that satisfies the Value interface.
+type customValue int
+
+func (cv *customValue) String() string { return fmt.Sprintf("%v", *cv) }
+
+func (cv *customValue) Set(s string) error {
+ v, err := strconv.ParseInt(s, 0, 64)
+ *cv = customValue(v)
+ return err
+}
+
+func (cv *customValue) Type() string { return "custom" }
+
func TestPrintDefaults(t *testing.T) {
fs := NewFlagSet("print defaults test", ContinueOnError)
var buf bytes.Buffer
@@ -897,12 +917,21 @@ func TestPrintDefaults(t *testing.T) {
fs.Float64("F", 2.7, "a non-zero `number`")
fs.Float64("G", 0, "a float that defaults to zero")
fs.Int("N", 27, "a non-zero int")
+ fs.IntSlice("Ints", []int{}, "int slice with zero default")
+ fs.IP("IP", nil, "IP address with no default")
+ fs.IPMask("IPMask", nil, "Netmask address with no default")
+ fs.IPNet("IPNet", net.IPNet{}, "IP network with no default")
fs.Int("Z", 0, "an int that defaults to zero")
fs.Duration("maxT", 0, "set `timeout` for dial")
fs.String("ND1", "foo", "a string with NoOptDefVal")
fs.Lookup("ND1").NoOptDefVal = "bar"
fs.Int("ND2", 1234, "a `num` with NoOptDefVal")
fs.Lookup("ND2").NoOptDefVal = "4321"
+ fs.StringSlice("Strings", []string{}, "string slice with zero default")
+
+ var cv customValue
+ fs.Var(&cv, "custom", "custom Value implementation")
+
fs.PrintDefaults()
got := buf.String()
if got != defaultOutput {