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..06ca8573c4 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 !f.TakesValue() { + 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/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()) +} 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