Skip to content

Commit 19c9c40

Browse files
authored
Merge pull request #422 from MidnightRocket/fix-default-is-zerovalue-for-generic-value-type
Fix defaultIsZeroValue check for generic Value types
2 parents 5ca8134 + c963093 commit 19c9c40

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

flag.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ func (f *Flag) defaultIsZeroValue() bool {
551551
case *intSliceValue, *stringSliceValue, *stringArrayValue:
552552
return f.DefValue == "[]"
553553
default:
554-
switch f.Value.String() {
554+
switch f.DefValue {
555555
case "false":
556556
return true
557557
case "<nil>":

flag_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,7 @@ const defaultOutput = ` --A for bootstrapping, allo
11831183
--StringSlice strings string slice with zero default
11841184
--Z int an int that defaults to zero
11851185
--custom custom custom Value implementation
1186+
--custom-with-val custom custom value which has been set from command line while help is shown
11861187
--customP custom a VarP with default (default 10)
11871188
--maxT timeout set timeout for dial
11881189
-v, --verbose count verbosity
@@ -1234,12 +1235,18 @@ func TestPrintDefaults(t *testing.T) {
12341235
cv2 := customValue(10)
12351236
fs.VarP(&cv2, "customP", "", "a VarP with default")
12361237

1238+
// Simulate case where a value has been provided and the help screen is shown
1239+
var cv3 customValue
1240+
fs.Var(&cv3, "custom-with-val", "custom value which has been set from command line while help is shown")
1241+
err := fs.Parse([]string{"--custom-with-val", "3"})
1242+
if err != nil {
1243+
t.Error("Parsing flags failed:", err)
1244+
}
1245+
12371246
fs.PrintDefaults()
12381247
got := buf.String()
12391248
if got != defaultOutput {
1240-
fmt.Println("\n" + got)
1241-
fmt.Printf("\n" + defaultOutput)
1242-
t.Errorf("got %q want %q\n", got, defaultOutput)
1249+
t.Errorf("\n--- Got:\n%s--- Wanted:\n%s\n", got, defaultOutput)
12431250
}
12441251
}
12451252

0 commit comments

Comments
 (0)