From 5b54501df936f43e0d858576a9285b65f30407fa Mon Sep 17 00:00:00 2001 From: Cameron Moore Date: Fri, 9 Sep 2016 08:42:53 -0500 Subject: Fix default values for custom fields Fallback to the original default detection logic if we have an unknown type. Fixes #91 --- flag.go | 12 +++++++++++- flag_test.go | 4 ++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/flag.go b/flag.go index c28f3bd..b0b0d46 100644 --- a/flag.go +++ b/flag.go @@ -437,7 +437,17 @@ func (f *Flag) defaultIsZeroValue() bool { case *intSliceValue, *stringSliceValue, *stringArrayValue: return f.DefValue == "[]" default: - return true + switch f.Value.String() { + case "false": + return true + case "": + return true + case "": + return true + case "0": + return true + } + return false } } diff --git a/flag_test.go b/flag_test.go index 1befb94..b294fc7 100644 --- a/flag_test.go +++ b/flag_test.go @@ -891,6 +891,7 @@ const defaultOutput = ` --A for bootstrapping, allo --StringSlice stringSlice string slice with zero default --Z int an int that defaults to zero --custom custom custom Value implementation + --customP custom a VarP with default (default 10) --maxT timeout set timeout for dial ` @@ -934,6 +935,9 @@ func TestPrintDefaults(t *testing.T) { var cv customValue fs.Var(&cv, "custom", "custom Value implementation") + cv2 := customValue(10) + fs.VarP(&cv2, "customP", "", "a VarP with default") + fs.PrintDefaults() got := buf.String() if got != defaultOutput { -- cgit v1.2.3