From: jimmyfrasche Date: Sun, 1 Apr 2018 18:15:17 +0000 (-0700) Subject: flag: correct zero values when printing defaults X-Git-Tag: go1.11beta1~1018 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=3c588b3fe7e1394f7df981284f1dc4c0a2b32b14;p=gostls13.git flag: correct zero values when printing defaults 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 Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- diff --git a/src/flag/flag.go b/src/flag/flag.go index 358402345c..f613144a7e 100644 --- a/src/flag/flag.go +++ b/src/flag/flag.go @@ -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 diff --git a/src/flag/flag_test.go b/src/flag/flag_test.go index 67c409f29b..c7f0c07d44 100644 --- a/src/flag/flag_test.go +++ b/src/flag/flag_test.go @@ -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")