MCPcopy
hub / github.com/urfave/cli / stringifyFlag

Function stringifyFlag

docs.go:88–128  ·  docs.go::stringifyFlag
(f Flag)

Source from the content-addressed store, hash-verified

86}
87
88func stringifyFlag(f Flag) string {
89 // enforce DocGeneration interface on flags to avoid reflection
90 df, ok := f.(DocGenerationFlag)
91 if !ok {
92 return ""
93 }
94 placeholder, usage := unquoteUsage(df.GetUsage())
95 needsPlaceholder := df.TakesValue()
96 // if needsPlaceholder is true, placeholder is empty
97 if needsPlaceholder && placeholder == "" {
98 // try to get type from flag
99 if tname := df.TypeName(); tname != "" {
100 placeholder = tname
101 } else {
102 placeholder = defaultPlaceholder
103 }
104 }
105
106 defaultValueString := ""
107
108 // don't print default text for required flags
109 if rf, ok := f.(RequiredFlag); !ok || !rf.IsRequired() {
110 if df.IsDefaultVisible() {
111 if s := df.GetDefaultText(); s != "" {
112 defaultValueString = fmt.Sprintf(formatDefault("%s"), s)
113 } else if df.TakesValue() && df.GetValue() != "" {
114 defaultValueString = fmt.Sprintf(formatDefault("%s"), df.GetValue())
115 }
116 }
117 }
118
119 usageWithDefault := strings.TrimSpace(usage + defaultValueString)
120
121 pn := prefixedNames(f.Names(), placeholder)
122 sliceFlag, ok := f.(DocGenerationMultiValueFlag)
123 if ok && sliceFlag.IsMultiValueFlag() {
124 pn = pn + " [ " + pn + " ]"
125 }
126
127 return withEnvHint(df.GetEnvVars(), fmt.Sprintf("%s\t%s", pn, usageWithDefault))
128}

Callers 1

TestFlagStringifyingFunction · 0.85

Calls 14

unquoteUsageFunction · 0.85
formatDefaultFunction · 0.85
prefixedNamesFunction · 0.85
withEnvHintFunction · 0.85
GetUsageMethod · 0.65
TakesValueMethod · 0.65
TypeNameMethod · 0.65
IsRequiredMethod · 0.65
IsDefaultVisibleMethod · 0.65
GetDefaultTextMethod · 0.65
GetValueMethod · 0.65
NamesMethod · 0.65

Tested by 1

TestFlagStringifyingFunction · 0.68