Skip to content
This repository was archived by the owner on Oct 17, 2021. It is now read-only.

Commit 5f1897e

Browse files
committed
move default value next to the flag name in help
1 parent 16565d5 commit 5f1897e

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

help.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import (
44
"flag"
55
"fmt"
66
"io"
7+
"strings"
78
"text/tabwriter"
9+
"unicode"
810
"unicode/utf8"
911
)
1012

@@ -15,6 +17,19 @@ func flagDashes(name string) string {
1517
return "-"
1618
}
1719

20+
func fmtDefValue(value interface{}) string {
21+
switch v := value.(type) {
22+
case string:
23+
if strings.IndexFunc(v, unicode.IsSpace) != -1 {
24+
return fmt.Sprintf(`"%v"`, v)
25+
}
26+
return v
27+
28+
default:
29+
return fmt.Sprintf("%v", v)
30+
}
31+
}
32+
1833
func renderFlagHelp(fl *flag.FlagSet, w io.Writer) {
1934
var count int
2035
fl.VisitAll(func(f *flag.Flag) {
@@ -26,7 +41,7 @@ func renderFlagHelp(fl *flag.FlagSet, w io.Writer) {
2641
if f.DefValue == "" {
2742
fmt.Fprintf(w, "\t%v%v\t%v\n", flagDashes(f.Name), f.Name, f.Usage)
2843
} else {
29-
fmt.Fprintf(w, "\t%v%v\t%v\t(%v)\n", flagDashes(f.Name), f.Name, f.Usage, f.DefValue)
44+
fmt.Fprintf(w, "\t%v%v=%v\t%v\n", flagDashes(f.Name), f.Name, fmtDefValue(f.DefValue), f.Usage)
3045
}
3146
})
3247
}

0 commit comments

Comments
 (0)