-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusage.go
248 lines (194 loc) · 4.68 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package program
import (
"bytes"
"fmt"
"io"
"os"
"slices"
"sort"
"golang.org/x/exp/maps"
)
func (p *Program) PrintUsage(cmd *Command) {
// The logic here is convoluted. The "cmd" parameter is nil if the program
// has no commands, but is the top-level empty command (p.command) if the
// program has commands but usage is about the program and not a command.
// Ugly.
var buf bytes.Buffer
var hasCommands bool
var commands map[string]*Command
if cmd != nil {
hasCommands = len(cmd.subcommands) > 0
commands = cmd.subcommands
}
var arguments []*Argument
var description string
if cmd == nil {
arguments = p.arguments
description = p.Description
} else {
arguments = cmd.arguments
description = cmd.Description
}
hasArguments := len(arguments) > 0
maxWidth := p.computeMaxWidth(cmd)
partialCommand := cmd != nil && cmd.FullName != ""
fmt.Fprintf(&buf, "Usage: %s", os.Args[0])
if cmd == nil {
fmt.Fprintf(&buf, " [OPTIONS]")
} else {
fmt.Fprintf(&buf, " [GLOBAL OPTIONS]")
}
if partialCommand {
fmt.Fprintf(&buf, " %s", cmd.FullName)
}
if hasCommands && !hasArguments {
if partialCommand {
fmt.Fprintf(&buf, " SUBCOMMAND...")
} else {
fmt.Fprintf(&buf, " COMMAND...")
}
}
if cmd != nil && cmd.Name != "" && hasArguments && len(cmd.options) > 0 {
fmt.Fprintf(&buf, " [COMMAND OPTIONS]")
}
if hasArguments {
for _, arg := range arguments {
if arg.Trailing {
fmt.Fprintf(&buf, " [<%s>...]", arg.Name)
} else if arg.Optional {
fmt.Fprintf(&buf, " [<%s>]", arg.Name)
} else {
fmt.Fprintf(&buf, " <%s>", arg.Name)
}
}
}
fmt.Fprintf(&buf, "\n")
if description != "" {
fmt.Fprintf(&buf, "\n%s\n", sentence(description))
}
if hasCommands {
label := "COMMANDS"
if partialCommand {
label = "SUBCOMMANDS"
}
p.usageCommands(&buf, label, commands, maxWidth)
} else if hasArguments {
p.usageArguments(&buf, arguments, maxWidth)
}
if len(p.options) > 0 {
if cmd == nil {
p.usageOptions(&buf, "OPTIONS", p.options, maxWidth)
} else {
p.usageOptions(&buf, "GLOBAL OPTIONS", p.options, maxWidth)
}
}
if cmd != nil && len(cmd.options) > 0 {
p.usageOptions(&buf, "COMMAND OPTIONS", cmd.options, maxWidth)
}
io.Copy(os.Stderr, &buf)
}
func (p *Program) computeMaxWidth(cmd *Command) int {
max := 0
if cmd != nil {
for _, subcmd := range cmd.subcommands {
if label := subcmd.Label(); len(label) > max {
max = len(label)
}
}
}
var args []*Argument
if cmd == nil {
args = p.arguments
} else {
args = cmd.arguments
}
for _, arg := range args {
if len(arg.Name) > max {
max = len(arg.Name)
}
}
f := func(opt *Option) {
length := 2 + 2 + 2 + len(opt.LongName)
if opt.ValueName != "" {
length += 2 + len(opt.ValueName) + 1
}
if length > max {
max = length
}
}
for _, opt := range p.options {
f(opt)
}
if cmd != nil {
for _, opt := range cmd.options {
f(opt)
}
}
return max
}
func (p *Program) usageCommands(buf *bytes.Buffer, label string, commands map[string]*Command, maxWidth int) {
fmt.Fprintf(buf, "\n%s\n\n", label)
names := maps.Keys(commands)
slices.Sort(names)
for _, name := range names {
cmd := commands[name]
fmt.Fprintf(buf, "%-*s %s\n", maxWidth, cmd.Label(), cmd.Description)
}
}
func (p *Program) usageArguments(buf *bytes.Buffer, args []*Argument, maxWidth int) {
fmt.Fprintf(buf, "\nARGUMENTS\n\n")
for _, arg := range args {
fmt.Fprintf(buf, "%-*s %s\n", maxWidth, arg.Name, arg.Description)
}
}
func (p *Program) usageOptions(buf *bytes.Buffer, label string, options map[string]*Option, maxWidth int) {
fmt.Fprintf(buf, "\n%s\n\n", label)
strs := make(map[*Option]string)
for _, opt := range options {
if _, found := strs[opt]; found {
continue
}
buf := bytes.NewBuffer([]byte{})
if opt.ShortName == "" {
fmt.Fprintf(buf, " ")
} else {
fmt.Fprintf(buf, "-%s", opt.ShortName)
}
if opt.LongName != "" {
if opt.ShortName == "" {
buf.WriteString(" ")
} else {
buf.WriteString(", ")
}
fmt.Fprintf(buf, "--%s", opt.LongName)
}
if opt.ValueName != "" {
fmt.Fprintf(buf, " <%s>", opt.ValueName)
}
str := buf.String()
strs[opt] = str
}
var opts []*Option
for opt, _ := range strs {
opts = append(opts, opt)
}
sort.Slice(opts, func(i, j int) bool {
return opts[i].sortKey() < opts[j].sortKey()
})
for _, opt := range opts {
fmt.Fprintf(buf, "%-*s %s", maxWidth, strs[opt], opt.Description)
if opt.DefaultValue != "" {
fmt.Fprintf(buf, " (default: %q)", opt.DefaultValue)
}
fmt.Fprintf(buf, "\n")
}
}
func (opt *Option) sortKey() string {
if opt.ShortName != "" {
return opt.ShortName
}
if opt.LongName != "" {
return opt.LongName
}
return ""
}