Skip to content

Commit 45c278a

Browse files
committed
Merge pull request #22 from gamegos/master
Change PrintDefaults format to be similar to Go 1.5 style
2 parents 6f7159c + 82f365f commit 45c278a

File tree

1 file changed

+81
-11
lines changed

1 file changed

+81
-11
lines changed

flag.go

Lines changed: 81 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -237,21 +237,87 @@ func Set(name, value string) error {
237237
return CommandLine.Set(name, value)
238238
}
239239

240-
// PrintDefaults prints, to standard error unless configured
241-
// otherwise, the default values of all defined flags in the set.
240+
// isZeroValue guesses whether the string represents the zero
241+
// value for a flag. It is not accurate but in practice works OK.
242+
func isZeroValue(value string) bool {
243+
switch value {
244+
case "false":
245+
return true
246+
case "":
247+
return true
248+
case "0":
249+
return true
250+
}
251+
return false
252+
}
253+
254+
// UnquoteUsage extracts a back-quoted name from the usage
255+
// string for a flag and returns it and the un-quoted usage.
256+
// Given "a `name` to show" it returns ("name", "a name to show").
257+
// If there are no back quotes, the name is an educated guess of the
258+
// type of the flag's value, or the empty string if the flag is boolean.
259+
func UnquoteUsage(flag *Flag) (name string, usage string) {
260+
// Look for a back-quoted name, but avoid the strings package.
261+
usage = flag.Usage
262+
for i := 0; i < len(usage); i++ {
263+
if usage[i] == '`' {
264+
for j := i + 1; j < len(usage); j++ {
265+
if usage[j] == '`' {
266+
name = usage[i+1 : j]
267+
usage = usage[:i] + name + usage[j+1:]
268+
return name, usage
269+
}
270+
}
271+
break // Only one back quote; use type name.
272+
}
273+
}
274+
// No explicit name, so use type if we can find one.
275+
name = "value"
276+
switch flag.Value.(type) {
277+
case boolFlag:
278+
name = ""
279+
case *durationValue:
280+
name = "duration"
281+
case *float64Value:
282+
name = "float"
283+
case *intValue, *int64Value:
284+
name = "int"
285+
case *stringValue:
286+
name = "string"
287+
case *uintValue, *uint64Value:
288+
name = "uint"
289+
}
290+
return
291+
}
292+
293+
// PrintDefaults prints to standard error the default values of all
294+
// defined command-line flags in the set. See the documentation for
295+
// the global function PrintDefaults for more information.
242296
func (f *FlagSet) PrintDefaults() {
243297
f.VisitAll(func(flag *Flag) {
244-
format := "--%s=%s: %s\n"
245-
if _, ok := flag.Value.(*stringValue); ok {
246-
// put quotes on the value
247-
format = "--%s=%q: %s\n"
248-
}
298+
s := ""
249299
if len(flag.Shorthand) > 0 {
250-
format = " -%s, " + format
300+
s = fmt.Sprintf(" -%s, --%s", flag.Shorthand, flag.Name)
251301
} else {
252-
format = " %s " + format
302+
s = fmt.Sprintf(" --%s", flag.Name)
253303
}
254-
fmt.Fprintf(f.out(), format, flag.Shorthand, flag.Name, flag.DefValue, flag.Usage)
304+
305+
name, usage := UnquoteUsage(flag)
306+
if len(name) > 0 {
307+
s += " " + name
308+
}
309+
310+
s += "\n \t"
311+
s += usage
312+
if !isZeroValue(flag.DefValue) {
313+
if _, ok := flag.Value.(*stringValue); ok {
314+
// put quotes on the value
315+
s += fmt.Sprintf(" (default %q)", flag.DefValue)
316+
} else {
317+
s += fmt.Sprintf(" (default %v)", flag.DefValue)
318+
}
319+
}
320+
fmt.Fprint(f.out(), s, "\n")
255321
})
256322
}
257323

@@ -262,7 +328,11 @@ func PrintDefaults() {
262328

263329
// defaultUsage is the default function to print a usage message.
264330
func defaultUsage(f *FlagSet) {
265-
fmt.Fprintf(f.out(), "Usage of %s:\n", f.name)
331+
if f.name == "" {
332+
fmt.Fprintf(f.out(), "Usage:\n")
333+
} else {
334+
fmt.Fprintf(f.out(), "Usage of %s:\n", f.name)
335+
}
266336
f.PrintDefaults()
267337
}
268338

0 commit comments

Comments
 (0)