Skip to content

Commit

Permalink
Merge pull request #2004 from dearchap/revert_get_value
Browse files Browse the repository at this point in the history
Revert GetValue() removal
  • Loading branch information
dearchap authored Nov 4, 2024
2 parents 83cb4dd + 23f3c5c commit 18c557e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions flag_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
8 changes: 8 additions & 0 deletions godoc-current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions testdata/godoc-v3.x.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 18c557e

Please sign in to comment.