-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelp.go
73 lines (64 loc) · 1.82 KB
/
help.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
package cli
import (
"bytes"
"fmt"
"github.com/ryanuber/columnize"
)
func ToHelpString(c Command, pathToCom []string) (help string) {
var helpBuf bytes.Buffer
config := columnize.DefaultConfig()
config.Prefix = " "
helpBuf.WriteString(fmt.Sprintf("%s: %s\n", c.Name, c.Description))
helpBuf.WriteString(fmt.Sprintf("Usage: %s\n\n", c.Usage))
if c.SubCommands != nil {
helpBuf.WriteString(" SubCommands:\n")
var subs []string
for _, s := range c.SubCommands {
subs = append(subs, fmt.Sprintf("%s:|%s", s.Name, s.Description))
}
helpBuf.WriteString(columnize.Format(subs, config))
helpBuf.WriteString("\n\n")
}
if c.Args != nil {
helpBuf.WriteString(" Arguments:\n")
var args []string
for _, a := range c.Args {
args = append(args, fmt.Sprintf("<%s>|%s", a.Name, a.Description))
}
helpBuf.WriteString(columnize.Format(args, config))
helpBuf.WriteString("\n\n")
}
if c.Flags != nil {
helpBuf.WriteString(" Flags:\n")
var flags []string
for _, f := range c.Flags {
flags = append(flags, toShortLongDescString(f.ShortName, f.LongName, f.Description))
}
helpBuf.WriteString(columnize.Format(flags, config))
helpBuf.WriteString("\n\n")
}
if c.Opts != nil {
helpBuf.WriteString(" Options:\n")
var opts []string
for _, o := range c.Opts {
opts = append(opts, toShortLongDescString(o.ShortName, o.LongName, o.Description))
}
helpBuf.WriteString(columnize.Format(opts, config))
helpBuf.WriteString("\n\n")
}
help = helpBuf.String()
return
}
func toShortLongDescString(short string, long string, description string) (str string) {
var buf bytes.Buffer
if short != "" {
buf.WriteString(fmt.Sprintf("-%s", short))
}
buf.WriteString("|")
if long != "" {
buf.WriteString(fmt.Sprintf("--%s", long))
}
buf.WriteString(fmt.Sprintf(",|%s", description))
str = buf.String()
return
}