-
Notifications
You must be signed in to change notification settings - Fork 9
/
usage.go
187 lines (168 loc) · 3.89 KB
/
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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (c) 2017. Oleg Sklyar & teris.io. All rights reserved.
// See the LICENSE file in the project root for licensing information.
package cli
import (
"errors"
"fmt"
"io"
"strings"
)
type usageline struct {
section string
key string
value string
}
// Usage prints out the complete usage string.
func Usage(a App, invocation []string, w io.Writer) error {
if len(invocation) < 1 {
return errors.New("invalid invocation path []")
}
descr := a.Description()
cmds := a.Commands()
args := a.Args()
opts := a.Options()
if len(invocation) > 1 {
for _, key := range invocation[1:] {
matched := false
for _, cmd := range cmds {
if cmd.Key() == key {
descr = cmd.Description()
cmds = cmd.Commands()
args = cmd.Args()
opts = append(opts, cmd.Options()...)
matched = true
break
}
}
// should never happen if invocation originates from the parser
if !matched {
// ignore errors here as no alternative writer is available
err := fmt.Errorf("fatal: invalid invocation path %v\n", invocation)
fmt.Fprint(w, err.Error())
return err
}
}
}
indent := " "
thiscmd := strings.Join(invocation, " ")
fmt.Fprintf(w, "%s%s%s\n\n", thiscmd, optstring(opts), argstring(args))
fmt.Fprintln(w, "Description:")
fmt.Fprintf(w, "%s%s\n", indent, descr)
var lines []usageline
maxkey := 0
if len(args) > 0 {
for _, arg := range args {
value := arg.Description()
if arg.Optional() {
value += ", optional"
}
line := usageline{
section: "Arguments",
key: arg.Key(),
value: value,
}
lines = append(lines, line)
if len(line.key) > maxkey {
maxkey = len(line.key)
}
}
}
if len(opts) > 0 {
for _, opt := range opts {
charstr := " "
if opt.CharKey() != rune(0) {
charstr = "-" + string(opt.CharKey()) + ", "
}
line := usageline{
section: "Options",
key: charstr + "--" + opt.Key(),
value: opt.Description(),
}
lines = append(lines, line)
if len(line.key) > maxkey {
maxkey = len(line.key)
}
}
}
if len(cmds) > 0 {
for _, cmd := range cmds {
shortstr := ""
if cmd.Shortcut() != "" {
shortstr = ", shortcut: " + cmd.Shortcut()
}
line := usageline{
section: "Sub-commands",
key: thiscmd + " " + cmd.Key(),
value: cmd.Description() + shortstr,
}
lines = append(lines, line)
if len(line.key) > maxkey {
maxkey = len(line.key)
}
}
}
lastsection := ""
for _, line := range lines {
if line.section != lastsection {
fmt.Fprintf(w, "\n%s:\n", line.section)
}
lastsection = line.section
spacer := 3 + maxkey - len(line.key)
fmt.Fprintf(w, "%s%s%s%s\n", indent, line.key, strings.Repeat(" ", spacer), line.value)
}
return nil
}
func optstring(opts []Option) string {
res := ""
for _, opt := range opts {
res += " [--" + opt.Key()
switch opt.Type() {
case TypeString:
res += "=string"
case TypeInt:
res += "=int"
case TypeNumber:
res += "=number"
}
res += "]"
}
return res
}
func argstring(args []Arg) string {
res := ""
for _, arg := range args {
if arg.Optional() {
res += " [" + arg.Key() + "]"
} else {
res += " <" + arg.Key() + ">"
}
}
return res
}
func shortUsage(a App, invocation []string) string {
if len(invocation) < 1 {
return "invalid invocation path []"
}
cmds := a.Commands()
args := a.Args()
opts := a.Options()
if len(invocation) > 1 {
for _, key := range invocation[1:] {
matched := false
for _, cmd := range cmds {
if cmd.Key() == key {
cmds = cmd.Commands()
args = cmd.Args()
opts = append(opts, cmd.Options()...)
matched = true
break
}
}
// should never happen if invocation originates from the parser
if !matched {
return fmt.Sprintf("fatal: invalid invocation path %v", invocation)
}
}
}
return fmt.Sprintf("%s%s%s", strings.Join(invocation, " "), optstring(opts), argstring(args))
}