Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert GetValue() removal #2004

Merged
merged 3 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading