]> Cypherpunks repositories - gostls13.git/commitdiff
flag: correct zero values when printing defaults
authorjimmyfrasche <soapboxcicero@gmail.com>
Sun, 1 Apr 2018 18:15:17 +0000 (11:15 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Sun, 1 Apr 2018 20:17:22 +0000 (20:17 +0000)
When the flag package first begin printing nonzero defaults, the test
was against a fixed set of string representations of zero values.
This worked until the string representation of a time.Duration
changed from "0" to "0s", causing the zero Duration to register as
nonzero. The flag package then added reflect-based code that fell back
to the old test. This failed to work when a nonzero default for a flag
happened to be the string representation of one the original fixed set
of zero values in the original test. This change removes the original
test, allowing the reflect-based code to be the only deciding factor.

Fixes #23543

Change-Id: I582ce554d6729e336fdd96fb27340674c15350d8
Reviewed-on: https://go-review.googlesource.com/103867
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/flag/flag.go
src/flag/flag_test.go

index 358402345cba92b839bdf314180c1a94971bb611..f613144a7e35be3e1ade269a32ca6e697474c2aa 100644 (file)
@@ -395,8 +395,8 @@ func Set(name, value string) error {
        return CommandLine.Set(name, value)
 }
 
-// isZeroValue guesses whether the string represents the zero
-// value for a flag. It is not accurate but in practice works OK.
+// isZeroValue determines whether the string represents the zero
+// value for a flag.
 func isZeroValue(flag *Flag, value string) bool {
        // Build a zero value of the flag's Value type, and see if the
        // result of calling its String method equals the value passed in.
@@ -408,15 +408,7 @@ func isZeroValue(flag *Flag, value string) bool {
        } else {
                z = reflect.Zero(typ)
        }
-       if value == z.Interface().(Value).String() {
-               return true
-       }
-
-       switch value {
-       case "false", "", "0":
-               return true
-       }
-       return false
+       return value == z.Interface().(Value).String()
 }
 
 // UnquoteUsage extracts a back-quoted name from the usage
index 67c409f29ba2f64f3ed147087884dfef34773cc8..c7f0c07d4489b3b6ff515e1cd4fea636a6d44973 100644 (file)
@@ -386,6 +386,8 @@ const defaultOutput = `  -A for bootstrapping, allow 'any' type
   -C   a boolean defaulting to true (default true)
   -D path
        set relative path for local imports
+  -E string
+       issue 23543 (default "0")
   -F number
        a non-zero number (default 2.7)
   -G float
@@ -412,6 +414,7 @@ func TestPrintDefaults(t *testing.T) {
        fs.Bool("Alongflagname", false, "disable bounds checking")
        fs.Bool("C", true, "a boolean defaulting to true")
        fs.String("D", "", "set relative `path` for local imports")
+       fs.String("E", "0", "issue 23543")
        fs.Float64("F", 2.7, "a non-zero `number`")
        fs.Float64("G", 0, "a float that defaults to zero")
        fs.String("M", "", "a multiline\nhelp\nstring")