forked from ukautz/clif
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdescriber.go
167 lines (152 loc) · 4.27 KB
/
describer.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package clif
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
)
// DescribeCli command implements the string rendering of a cli which help uses.
// Can be overwritten at users discretion.
var DescribeCli = func(c *Cli) string {
// name + version
line := "<headline>" + c.Name + "<reset>"
if c.Version != "" {
line += " <debug>(" + c.Version + ")<reset>"
}
lines := []string{line}
// description
if c.Description != "" {
lines = append(lines, "<info>"+c.Description+"<reset>\n")
}
// usage
prog := filepath.Base(os.Args[0])
lines = append(lines, fmt.Sprintf("<subline>Usage:<reset>\n %s command [arg ..] [--opt val ..]\n", prog))
// commands
lines = append(lines, "<subline>Available commands:<reset>")
max := 0
ordered := make(map[string][]*Command)
prefices := make([]string, 0)
for _, cmd := range c.Commands {
if l := len(cmd.Name); l > max {
max = l
}
prefix := ""
if i := strings.Index(cmd.Name, ":"); i > -1 {
prefix = cmd.Name[0:i]
}
if ordered[prefix] == nil {
prefices = append(prefices, prefix)
ordered[prefix] = make([]*Command, 0)
}
ordered[prefix] = append(ordered[prefix], cmd)
}
sort.Strings(prefices)
for _, prefix := range prefices {
if prefix != "" {
lines = append(lines, fmt.Sprintf(" <subline>%s<reset>", prefix))
}
sort.Sort(CommandsSort(ordered[prefix]))
for _, cmd := range ordered[prefix] {
lines = append(lines, fmt.Sprintf(" <info>%-"+fmt.Sprintf("%d", max)+"s<reset> %s", cmd.Name, cmd.Usage))
}
}
return strings.Join(lines, "\n") + "\n"
}
// DescribeCommand implements the string rendering of a command which help uses.
// Can be overwritten at users discretion.
var DescribeCommand = func(c *Command) string {
lines := []string{"Command: <headline>" + c.Name + "<reset>"}
if c.Description != "" {
lines = append(lines, []string{"<info>" + c.Description + "<reset>", ""}...)
} else if c.Usage != "" {
lines = append(lines, []string{"<info>" + c.Usage + "<reset>", ""}...)
}
lines = append(lines, "<subline>Usage:<reset>")
usage := []string{c.Name}
args := make([][]string, 0)
argMax := 0
opts := make([][]string, 0)
optMax := 0
for _, p := range c.Arguments {
var short string
usg := p.Usage
short = p.Name
usgInfo := []string{}
if p.Multiple {
short = short + " ..."
usgInfo = append(usgInfo, `<debug>mult<reset>`)
}
if p.Required {
usgInfo = append(usgInfo, `<important>req<reset>`)
} else {
short = fmt.Sprintf("[%s]", short)
}
if p.Env != "" {
usgInfo = append(usgInfo, fmt.Sprintf(`env: <debug>%s<reset>`, p.Env))
}
if p.Default != "" {
usgInfo = append(usgInfo, fmt.Sprintf(`default: <debug>"%s"<reset>`, p.Default))
}
if l := len(p.Name); l > argMax {
argMax = l
}
usage = append(usage, short)
if len(usgInfo) > 0 {
usg += " ("+ strings.Join(usgInfo, ", ")+ ")"
}
args = append(args, []string{p.Name, usg})
}
for _, p := range c.Options {
short := fmt.Sprintf("--%s", p.Name)
if p.Alias != "" {
short += "|-" + p.Alias
}
if !p.Flag {
short += " val"
}
long := short
usg := p.Usage
usgInfo := []string{}
if p.Multiple {
short = short + " ..."
usgInfo = append(usgInfo, `<debug>mult<reset>`)
}
if !p.Required {
short = "[" + short + "]"
} else {
usgInfo = append(usgInfo, `<important>req<reset>`)
}
if p.Env != "" {
usgInfo = append(usgInfo, fmt.Sprintf(`env: <debug>%s<reset>`, p.Env))
}
if p.Default != "" {
usgInfo = append(usgInfo, fmt.Sprintf(`default: <debug>"%s"<reset>`, p.Default))
}
if l := len(long); l > optMax {
optMax = l
}
usage = append(usage, short)
if len(usgInfo) > 0 {
usg += " ("+ strings.Join(usgInfo, ", ")+ ")"
}
opts = append(opts, []string{long, usg})
}
lines = append(lines, " "+strings.Join(usage, " "))
lines = append(lines, "")
if len(args) > 0 {
lines = append(lines, "<subline>Arguments:<reset>")
for _, l := range args {
lines = append(lines, fmt.Sprintf(" <info>%-"+fmt.Sprintf("%d", argMax)+"s<reset> %s", l[0], l[1]))
}
lines = append(lines, "")
}
if len(opts) > 0 {
lines = append(lines, "<subline>Options:<reset>")
for _, l := range opts {
lines = append(lines, fmt.Sprintf(" <info>%-"+fmt.Sprintf("%d", optMax)+"s<reset> %s", l[0], l[1]))
}
lines = append(lines, "")
}
return strings.Join(lines, "\n") + "\n"
}