Skip to content

Commit 18c557e

Browse files
authored
Merge pull request #2004 from dearchap/revert_get_value
Revert GetValue() removal
2 parents 83cb4dd + 23f3c5c commit 18c557e

File tree

5 files changed

+35
-0
lines changed

5 files changed

+35
-0
lines changed

flag.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ type DocGenerationFlag interface {
129129
// GetUsage returns the usage string for the flag
130130
GetUsage() string
131131

132+
// GetValue returns the flags value as string representation and an empty
133+
// string if the flag takes no value at all.
134+
GetValue() string
135+
132136
// GetDefaultText returns the default text for this flag
133137
GetDefaultText() string
134138

flag_impl.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ type FlagBase[T any, C any, VC ValueCreator[T, C]] struct {
8989
value Value // value representing this flag's value
9090
}
9191

92+
// GetValue returns the flags value as string representation and an empty
93+
// string if the flag takes no value at all.
94+
func (f *FlagBase[T, C, V]) GetValue() string {
95+
if !f.TakesValue() {
96+
return ""
97+
}
98+
return fmt.Sprintf("%v", f.Value)
99+
}
100+
92101
// Apply populates the flag given the flag set and environment
93102
func (f *FlagBase[T, C, V]) Apply(set *flag.FlagSet) error {
94103
tracef("apply (flag=%[1]q)", f.Name)

flag_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3127,3 +3127,9 @@ func TestEnvHintWindows(t *testing.T) {
31273127
assert.Equal(t, "something [%foo%, %bar%, %ss%]", withEnvHint([]string{"foo", "bar", "ss"}, "something"))
31283128
}
31293129
}
3130+
3131+
func TestDocGetValue(t *testing.T) {
3132+
assert.Equal(t, "", (&BoolFlag{Name: "foo", Value: true}).GetValue())
3133+
assert.Equal(t, "", (&BoolFlag{Name: "foo", Value: false}).GetValue())
3134+
assert.Equal(t, "bar", (&StringFlag{Name: "foo", Value: "bar"}).GetValue())
3135+
}

godoc-current.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,10 @@ type DocGenerationFlag interface {
560560
// GetUsage returns the usage string for the flag
561561
GetUsage() string
562562

563+
// GetValue returns the flags value as string representation and an empty
564+
// string if the flag takes no value at all.
565+
GetValue() string
566+
563567
// GetDefaultText returns the default text for this flag
564568
GetDefaultText() string
565569

@@ -695,6 +699,10 @@ func (f *FlagBase[T, C, V]) GetEnvVars() []string
695699
func (f *FlagBase[T, C, V]) GetUsage() string
696700
GetUsage returns the usage string for the flag
697701

702+
func (f *FlagBase[T, C, V]) GetValue() string
703+
GetValue returns the flags value as string representation and an empty
704+
string if the flag takes no value at all.
705+
698706
func (f *FlagBase[T, C, V]) IsDefaultVisible() bool
699707
IsDefaultVisible returns true if the flag is not hidden, otherwise false
700708

testdata/godoc-v3.x.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,10 @@ type DocGenerationFlag interface {
560560
// GetUsage returns the usage string for the flag
561561
GetUsage() string
562562

563+
// GetValue returns the flags value as string representation and an empty
564+
// string if the flag takes no value at all.
565+
GetValue() string
566+
563567
// GetDefaultText returns the default text for this flag
564568
GetDefaultText() string
565569

@@ -695,6 +699,10 @@ func (f *FlagBase[T, C, V]) GetEnvVars() []string
695699
func (f *FlagBase[T, C, V]) GetUsage() string
696700
GetUsage returns the usage string for the flag
697701

702+
func (f *FlagBase[T, C, V]) GetValue() string
703+
GetValue returns the flags value as string representation and an empty
704+
string if the flag takes no value at all.
705+
698706
func (f *FlagBase[T, C, V]) IsDefaultVisible() bool
699707
IsDefaultVisible returns true if the flag is not hidden, otherwise false
700708

0 commit comments

Comments
 (0)