Fix:(issue_1500). Fix slice flag value duplication issue

This commit is contained in:
Naveen Gogineni
2022-09-29 10:33:48 -04:00
committed by Dan Buch
parent ea2893084b
commit 924ebdaab2
3 changed files with 59 additions and 4 deletions

27
flag.go
View File

@@ -126,6 +126,21 @@ type Flag interface {
RunAction(*Context) error
}
// DocGenerationFlag is an interface that allows documentation generation for the flag
type DocGenerationFlag interface {
Flag
// TakesValue returns true if the flag takes a value, otherwise false
TakesValue() bool
// GetValue returns the flags value as string representation and an empty
// string if the flag takes no value at all.
GetValue() string
// GetEnvVars returns the env vars for this flag
GetEnvVars() []string
}
// DocGenerationSliceFlag extends DocGenerationFlag for slice-based flags.
type DocGenerationSliceFlag interface {
DocGenerationFlag
@@ -293,8 +308,14 @@ func formatDefault(format string) string {
}
func stringifyFlag(f Flag) string {
placeholder, usage := unquoteUsage(f.GetUsage())
needsPlaceholder := f.TakesValue()
// enforce DocGeneration interface on flags to avoid reflection
df, ok := f.(DocGenerationFlag)
if !ok {
return ""
}
placeholder, usage := unquoteUsage(df.GetUsage())
needsPlaceholder := df.TakesValue()
if needsPlaceholder && placeholder == "" {
placeholder = defaultPlaceholder
@@ -302,7 +323,7 @@ func stringifyFlag(f Flag) string {
defaultValueString := ""
if s := f.GetDefaultText(); s != "" {
if s := df.GetDefaultText(); s != "" {
defaultValueString = fmt.Sprintf(formatDefault("%s"), s)
}