aboutsummaryrefslogtreecommitdiff
path: root/flag_test.go
diff options
context:
space:
mode:
authorCameron Moore <[email protected]>2016-07-11 16:00:10 -0500
committerCameron Moore <[email protected]>2016-08-16 12:20:59 -0500
commitf90e8bbe4975ae9216a06b7d6f8d33590628e4ab (patch)
tree4e958ba273e2c0b767f73131d061f8595f982861 /flag_test.go
parent01665e1eb3d0533dca0e5acffdff9eec1fcb886f (diff)
Refactor default value detection
In Go 1.7 the default value for a time.Duration will change from "0" to "0s". This commit replaces the simplistic `isZeroValue` with a more intelligent implementation that checks for zero values based upon the specific type of the flag.
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 {