From 9edfc8d416bea8aecfbdf0790a838f95ae133078 Mon Sep 17 00:00:00 2001 From: MidnightRocket Date: Tue, 18 Mar 2025 14:07:18 +0100 Subject: [PATCH 1/3] Fix defaultIsZeroValue check for generic Value type --- flag.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flag.go b/flag.go index 7c058de3..4bdbd0c0 100644 --- a/flag.go +++ b/flag.go @@ -551,7 +551,7 @@ func (f *Flag) defaultIsZeroValue() bool { case *intSliceValue, *stringSliceValue, *stringArrayValue: return f.DefValue == "[]" default: - switch f.Value.String() { + switch f.DefValue { case "false": return true case "": From edb16648484c5752d70fd84f08b3aecc46e61339 Mon Sep 17 00:00:00 2001 From: MidnightRocket Date: Fri, 21 Mar 2025 18:02:55 +0100 Subject: [PATCH 2/3] Add better test for defaultIsZeroValue for generic Value type --- flag_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/flag_test.go b/flag_test.go index 643f0999..344eb074 100644 --- a/flag_test.go +++ b/flag_test.go @@ -1183,6 +1183,7 @@ const defaultOutput = ` --A for bootstrapping, allo --StringSlice strings string slice with zero default --Z int an int that defaults to zero --custom custom custom Value implementation + --custom-with-val custom custom value which has been set from command line while help is shown --customP custom a VarP with default (default 10) --maxT timeout set timeout for dial -v, --verbose count verbosity @@ -1234,6 +1235,14 @@ func TestPrintDefaults(t *testing.T) { cv2 := customValue(10) fs.VarP(&cv2, "customP", "", "a VarP with default") + // Simulate case where a value has been provided and the help screen is shown + var cv3 customValue + fs.Var(&cv3, "custom-with-val", "custom value which has been set from command line while help is shown") + err := fs.Parse([]string{"--custom-with-val", "3"}) + if err != nil { + t.Error("Parsing flags failed:", err) + } + fs.PrintDefaults() got := buf.String() if got != defaultOutput { From c96309303407244840d6f2bc00ae32dc95390c25 Mon Sep 17 00:00:00 2001 From: MidnightRocket Date: Fri, 21 Mar 2025 18:03:46 +0100 Subject: [PATCH 3/3] Improve readability for error in TestPrintDefaults --- flag_test.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/flag_test.go b/flag_test.go index 344eb074..0dbe8741 100644 --- a/flag_test.go +++ b/flag_test.go @@ -1246,9 +1246,7 @@ func TestPrintDefaults(t *testing.T) { fs.PrintDefaults() got := buf.String() if got != defaultOutput { - fmt.Println("\n" + got) - fmt.Printf("\n" + defaultOutput) - t.Errorf("got %q want %q\n", got, defaultOutput) + t.Errorf("\n--- Got:\n%s--- Wanted:\n%s\n", got, defaultOutput) } }