-
Notifications
You must be signed in to change notification settings - Fork 0
/
fmt.go
74 lines (60 loc) · 1.29 KB
/
fmt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package quack
import (
"fmt"
"io"
"strings"
"text/tabwriter"
"github.com/spf13/pflag"
)
func fmtUsage(w io.Writer, fs *pflag.FlagSet) {
tw := tabwriter.NewWriter(w, 2, 2, 1, ' ', 0)
fs.SortFlags = true
var flags []string
var options []string
fs.VisitAll(func(f *pflag.Flag) {
var line strings.Builder
if f.Shorthand != "" {
fmt.Fprintf(&line, "\t-%s,\t--%s", f.Shorthand, f.Name)
} else {
fmt.Fprintf(&line, "\t\t--%s", f.Name)
}
line.WriteByte('\t')
valType := f.Value.Type()
typeName, usage := pflag.UnquoteUsage(f)
if typeName != "" {
line.WriteString(typeName)
}
line.WriteByte('\t')
if f.DefValue != "" {
switch valType {
case "string":
fmt.Fprintf(&line, "(default='%s')", f.DefValue)
case "bool":
fmt.Fprintf(&line, "(default=%s)", f.DefValue)
default:
fmt.Fprintf(&line, "(default=%s)", f.DefValue)
}
}
line.WriteByte('\t')
line.WriteString(usage)
switch valType {
case "bool":
flags = append(flags, line.String())
default:
options = append(options, line.String())
}
})
for i, f := range flags {
if i == 0 {
fmt.Fprintln(tw, "Flags:\t\t\t\t\t")
}
fmt.Fprintln(tw, f)
}
for i, o := range options {
if i == 0 {
fmt.Fprintln(tw, "Options:\t\t\t\t\t")
}
fmt.Fprintln(tw, o)
}
tw.Flush()
}