-
Notifications
You must be signed in to change notification settings - Fork 2
/
flags_usage.go
57 lines (49 loc) · 1.19 KB
/
flags_usage.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
package flags
import "strings"
func (c *flagContext) ShowUsage(leadingSpace int) string {
output := ""
if len(c.cmdFlags) != 0 {
//find longest name length
l := 0
for n, f := range c.cmdFlags {
shortName := f.GetShortName()
if shortName != "" {
n = n + ", -" + shortName
}
if len(n) > l {
l = len(n)
}
}
//print non-bool flags first
for n, f := range c.cmdFlags {
if len(n) > 1 {
n = "-" + n
}
shortName := f.GetShortName()
if shortName != "" {
n = n + ", -" + shortName
}
switch f.GetValue().(type) {
case bool:
default:
output += strings.Repeat(" ", leadingSpace) + "-" + n + strings.Repeat(" ", 7+(l-len(n))) + f.String() + "\n"
}
}
//then bool flags
for n, f := range c.cmdFlags {
shortName := f.GetShortName()
if shortName != "" {
n = n + ", -" + shortName
}
switch f.GetValue().(type) {
case bool:
if len(f.GetName()) == 1 {
output += strings.Repeat(" ", leadingSpace) + "-" + n + strings.Repeat(" ", 7+(l-len(n))) + f.String() + "\n"
} else {
output += strings.Repeat(" ", leadingSpace) + "--" + n + strings.Repeat(" ", 6+(l-len(n))) + f.String() + "\n"
}
}
}
}
return output
}