@@ -117,6 +117,12 @@ type DocGenerationFlag interface {
117117 // GetValue returns the flags value as string representation and an empty
118118 // string if the flag takes no value at all.
119119 GetValue () string
120+
121+ // GetDefaultText returns the default text for this flag
122+ GetDefaultText () string
123+
124+ // GetEnvVars returns the env vars for this flag
125+ GetEnvVars () []string
120126}
121127
122128// VisibleFlag is an interface that allows to check if a flag is visible
@@ -299,55 +305,29 @@ func formatDefault(format string) string {
299305}
300306
301307func stringifyFlag (f Flag ) string {
302- fv := flagValue (f )
303-
304- switch f := f .(type ) {
305- case * IntSliceFlag :
306- return withEnvHint (flagStringSliceField (f , "EnvVars" ),
307- stringifyIntSliceFlag (f ))
308- case * Int64SliceFlag :
309- return withEnvHint (flagStringSliceField (f , "EnvVars" ),
310- stringifyInt64SliceFlag (f ))
311- case * Float64SliceFlag :
312- return withEnvHint (flagStringSliceField (f , "EnvVars" ),
313- stringifyFloat64SliceFlag (f ))
314- case * StringSliceFlag :
315- return withEnvHint (flagStringSliceField (f , "EnvVars" ),
316- stringifyStringSliceFlag (f ))
308+ // enforce DocGeneration interface on flags to avoid reflection
309+ df , ok := f .(DocGenerationFlag )
310+ if ! ok {
311+ return ""
317312 }
318313
319- placeholder , usage := unquoteUsage (fv .FieldByName ("Usage" ).String ())
314+ placeholder , usage := unquoteUsage (df .GetUsage ())
315+ needsPlaceholder := df .TakesValue ()
320316
321- needsPlaceholder := false
322- defaultValueString := ""
323- val := fv .FieldByName ("Value" )
324- if val .IsValid () {
325- needsPlaceholder = val .Kind () != reflect .Bool
326- defaultValueString = fmt .Sprintf (formatDefault ("%v" ), val .Interface ())
327-
328- if val .Kind () == reflect .String && val .String () != "" {
329- defaultValueString = fmt .Sprintf (formatDefault ("%q" ), val .String ())
330- }
331- }
332-
333- helpText := fv .FieldByName ("DefaultText" )
334- if helpText .IsValid () && helpText .String () != "" {
335- needsPlaceholder = val .Kind () != reflect .Bool
336- defaultValueString = fmt .Sprintf (formatDefault ("%s" ), helpText .String ())
317+ if needsPlaceholder && placeholder == "" {
318+ placeholder = defaultPlaceholder
337319 }
338320
339- if defaultValueString == formatDefault ("" ) {
340- defaultValueString = ""
341- }
321+ defaultValueString := ""
342322
343- if needsPlaceholder && placeholder = = "" {
344- placeholder = defaultPlaceholder
323+ if s := df . GetDefaultText (); s ! = "" {
324+ defaultValueString = fmt . Sprintf ( formatDefault ( "%s" ), s )
345325 }
346326
347327 usageWithDefault := strings .TrimSpace (usage + defaultValueString )
348328
349- return withEnvHint (flagStringSliceField ( f , "EnvVars" ),
350- fmt .Sprintf ("%s\t %s" , prefixedNames (f .Names (), placeholder ), usageWithDefault ))
329+ return withEnvHint (df . GetEnvVars ( ),
330+ fmt .Sprintf ("%s\t %s" , prefixedNames (df .Names (), placeholder ), usageWithDefault ))
351331}
352332
353333func stringifyIntSliceFlag (f * IntSliceFlag ) string {
0 commit comments