Commit 3c588b3f authored by jimmyfrasche's avatar jimmyfrasche Committed by Brad Fitzpatrick

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/103867Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent e55475ca
......@@ -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
......
......@@ -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")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment