From 0e129f90251af77fc7018068a488eaf87bcf5f30 Mon Sep 17 00:00:00 2001 From: Naveen Gogineni Date: Mon, 4 Nov 2024 16:07:28 -0500 Subject: [PATCH 1/2] Revert GetValue() removal --- flag.go | 4 ++++ flag_impl.go | 9 +++++++++ godoc-current.txt | 8 ++++++++ testdata/godoc-v3.x.txt | 8 ++++++++ 4 files changed, 29 insertions(+) diff --git a/flag.go b/flag.go index 11b13662c5..420ea5e939 100644 --- a/flag.go +++ b/flag.go @@ -129,6 +129,10 @@ type DocGenerationFlag interface { // GetUsage returns the usage string for the flag GetUsage() string + // GetValue returns the flags value as string representation and an empty + // string if the flag takes no value at all. + GetValue() string + // GetDefaultText returns the default text for this flag GetDefaultText() string diff --git a/flag_impl.go b/flag_impl.go index ad6678bfc7..22bafe23ed 100644 --- a/flag_impl.go +++ b/flag_impl.go @@ -89,6 +89,15 @@ type FlagBase[T any, C any, VC ValueCreator[T, C]] struct { value Value // value representing this flag's value } +// GetValue returns the flags value as string representation and an empty +// string if the flag takes no value at all. +func (f *FlagBase[T, C, V]) GetValue() string { + if reflect.TypeOf(f.Value).Kind() == reflect.Bool { + return "" + } + return fmt.Sprintf("%v", f.Value) +} + // Apply populates the flag given the flag set and environment func (f *FlagBase[T, C, V]) Apply(set *flag.FlagSet) error { tracef("apply (flag=%[1]q)", f.Name) diff --git a/godoc-current.txt b/godoc-current.txt index dd9a89076c..a49558e1c3 100644 --- a/godoc-current.txt +++ b/godoc-current.txt @@ -560,6 +560,10 @@ type DocGenerationFlag interface { // GetUsage returns the usage string for the flag GetUsage() string + // GetValue returns the flags value as string representation and an empty + // string if the flag takes no value at all. + GetValue() string + // GetDefaultText returns the default text for this flag GetDefaultText() string @@ -695,6 +699,10 @@ func (f *FlagBase[T, C, V]) GetEnvVars() []string func (f *FlagBase[T, C, V]) GetUsage() string GetUsage returns the usage string for the flag +func (f *FlagBase[T, C, V]) GetValue() string + GetValue returns the flags value as string representation and an empty + string if the flag takes no value at all. + func (f *FlagBase[T, C, V]) IsDefaultVisible() bool IsDefaultVisible returns true if the flag is not hidden, otherwise false diff --git a/testdata/godoc-v3.x.txt b/testdata/godoc-v3.x.txt index dd9a89076c..a49558e1c3 100644 --- a/testdata/godoc-v3.x.txt +++ b/testdata/godoc-v3.x.txt @@ -560,6 +560,10 @@ type DocGenerationFlag interface { // GetUsage returns the usage string for the flag GetUsage() string + // GetValue returns the flags value as string representation and an empty + // string if the flag takes no value at all. + GetValue() string + // GetDefaultText returns the default text for this flag GetDefaultText() string @@ -695,6 +699,10 @@ func (f *FlagBase[T, C, V]) GetEnvVars() []string func (f *FlagBase[T, C, V]) GetUsage() string GetUsage returns the usage string for the flag +func (f *FlagBase[T, C, V]) GetValue() string + GetValue returns the flags value as string representation and an empty + string if the flag takes no value at all. + func (f *FlagBase[T, C, V]) IsDefaultVisible() bool IsDefaultVisible returns true if the flag is not hidden, otherwise false From 23f3c5ccde6f0e24e25ce5f87338ad3f8aa03d6e Mon Sep 17 00:00:00 2001 From: Naveen Gogineni Date: Mon, 4 Nov 2024 16:50:21 -0500 Subject: [PATCH 2/2] Add tests --- flag_impl.go | 2 +- flag_test.go | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/flag_impl.go b/flag_impl.go index 22bafe23ed..06ca8573c4 100644 --- a/flag_impl.go +++ b/flag_impl.go @@ -92,7 +92,7 @@ type FlagBase[T any, C any, VC ValueCreator[T, C]] struct { // GetValue returns the flags value as string representation and an empty // string if the flag takes no value at all. func (f *FlagBase[T, C, V]) GetValue() string { - if reflect.TypeOf(f.Value).Kind() == reflect.Bool { + if !f.TakesValue() { return "" } return fmt.Sprintf("%v", f.Value) diff --git a/flag_test.go b/flag_test.go index 40f4fb1264..bffac56913 100644 --- a/flag_test.go +++ b/flag_test.go @@ -3127,3 +3127,9 @@ func TestEnvHintWindows(t *testing.T) { assert.Equal(t, "something [%foo%, %bar%, %ss%]", withEnvHint([]string{"foo", "bar", "ss"}, "something")) } } + +func TestDocGetValue(t *testing.T) { + assert.Equal(t, "", (&BoolFlag{Name: "foo", Value: true}).GetValue()) + assert.Equal(t, "", (&BoolFlag{Name: "foo", Value: false}).GetValue()) + assert.Equal(t, "bar", (&StringFlag{Name: "foo", Value: "bar"}).GetValue()) +}