-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_line.go
483 lines (373 loc) · 9 KB
/
command_line.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
package program
import (
"math"
"os"
"regexp"
"strconv"
"strings"
)
var commandNameRE = regexp.MustCompile("\\s+")
type Command struct {
Name string
FullName string
Description string
Main Main
program *Program
subcommands map[string]*Command
options map[string]*Option
arguments []*Argument
}
func (p *Program) newCommandGroup(name, fullName string) *Command {
return &Command{
Name: name,
FullName: fullName,
program: p,
subcommands: make(map[string]*Command),
}
}
func (c *Command) Label() string {
if len(c.subcommands) == 0 {
return c.Name
}
return c.Name + " <subcommand>..."
}
type Option struct {
ShortName string
LongName string
ValueName string
DefaultValue string
Description string
Set bool
Value string
}
type Argument struct {
Name string
Description string
Optional bool
Trailing bool
Set bool
Value string
TrailingValues []string
}
func (p *Program) AddCommand(fullName, description string, main Main) *Command {
if p.Main != nil {
panic("cannot have a main function with commands")
}
names := splitCommandName(fullName)
if len(names) == 0 {
panic("empty command name")
}
if p.command == nil {
p.command = p.newCommandGroup("", "")
}
group := p.command
for i := range len(names) - 1 {
name := names[i]
if group.subcommands == nil {
group.subcommands = make(map[string]*Command)
}
group2 := group.subcommands[name]
if group2 == nil {
group2 = p.newCommandGroup(name, strings.Join(names[:i+1], " "))
group.subcommands[name] = group2
}
group = group2
}
name := names[len(names)-1]
cmd := Command{
Name: name,
FullName: strings.Join(names, " "),
Description: description,
Main: main,
program: p,
options: make(map[string]*Option),
}
if cmd := group.subcommands[name]; cmd != nil {
if len(cmd.subcommands) == 0 {
Panic("duplicate command %q", cmd.FullName)
} else {
Panic("command %q has subcommands", cmd.FullName)
}
}
if group.Main != nil {
Panic("command %q cannot be used as a group", group.FullName)
}
group.subcommands[name] = &cmd
return &cmd
}
func (p *Program) AddOption(shortName, longName, valueName, defaultValue, description string) {
option := &Option{
ShortName: shortName,
LongName: longName,
ValueName: valueName,
DefaultValue: defaultValue,
Description: description,
}
p.addOption(nil, option)
}
func (p *Program) AddFlag(shortName, longName, description string) {
p.AddOption(shortName, longName, "", "", description)
}
func (c *Command) AddOption(shortName, longName, valueName, defaultValue, description string) {
option := &Option{
ShortName: shortName,
LongName: longName,
ValueName: valueName,
DefaultValue: defaultValue,
Description: description,
}
c.program.addOption(c, option)
}
func (c *Command) AddFlag(shortName, longName, description string) {
c.AddOption(shortName, longName, "", "", description)
}
func (p *Program) addOption(c *Command, option *Option) {
var m map[string]*Option
if option.ShortName == "" && option.LongName == "" {
panic("option has no short or long name")
}
if c == nil {
m = p.options
} else {
m = c.options
}
if option.ShortName != "" {
if _, found := m[option.ShortName]; found {
Panic("duplicate option name %q", option.ShortName)
}
if c != nil {
if _, found := c.program.options[option.ShortName]; found {
Panic("duplicate option name %q", option.ShortName)
}
}
m[option.ShortName] = option
}
if option.LongName != "" {
if _, found := m[option.LongName]; found {
Panic("duplicate option name %q", option.LongName)
}
if c != nil {
if _, found := c.program.options[option.LongName]; found {
Panic("duplicate option name %q", option.LongName)
}
}
m[option.LongName] = option
}
}
func (p *Program) AddArgument(name, description string) {
checkForArgument(p.arguments)
arg := &Argument{
Name: name,
Description: description,
}
p.arguments = append(p.arguments, arg)
}
func (p *Program) AddOptionalArgument(name, description string) {
checkForOptionalArgument(p.arguments)
arg := &Argument{
Name: name,
Description: description,
Optional: true,
}
p.arguments = append(p.arguments, arg)
}
func (p *Program) AddTrailingArgument(name, description string) {
checkForTrailingArgument(p.arguments)
arg := &Argument{
Name: name,
Description: description,
Trailing: true,
}
p.arguments = append(p.arguments, arg)
}
func (c *Command) AddArgument(name, description string) {
checkForArgument(c.arguments)
arg := &Argument{
Name: name,
Description: description,
}
c.arguments = append(c.arguments, arg)
}
func (c *Command) AddOptionalArgument(name, description string) {
checkForOptionalArgument(c.arguments)
arg := &Argument{
Name: name,
Description: description,
Optional: true,
}
c.arguments = append(c.arguments, arg)
}
func (c *Command) AddTrailingArgument(name, description string) {
checkForTrailingArgument(c.arguments)
arg := &Argument{
Name: name,
Description: description,
Trailing: true,
}
c.arguments = append(c.arguments, arg)
}
func checkForArgument(args []*Argument) {
if len(args) == 0 {
return
}
lastArg := args[len(args)-1]
if lastArg.Optional {
panic("cannot add non-optional argument after optional argument")
}
if lastArg.Trailing {
panic("cannot add argument after trailing argument")
}
}
func checkForOptionalArgument(args []*Argument) {
if len(args) == 0 {
return
}
lastArg := args[len(args)-1]
if lastArg.Trailing {
panic("cannot add argument after trailing argument")
}
}
func checkForTrailingArgument(args []*Argument) {
if len(args) == 0 {
return
}
lastArg := args[len(args)-1]
if lastArg.Trailing {
panic("cannot add multiple trailing arguments")
}
}
func (p *Program) CommandName() string {
if p.command == nil {
Panic("no command defined")
}
return p.selectedCommand.Name
}
func (p *Program) CommandFullName() string {
if p.command == nil {
Panic("no command defined")
}
return p.selectedCommand.FullName
}
func (p *Program) IsOptionSet(name string) bool {
return p.mustOption(name).Set
}
func (p *Program) OptionValue(name string) string {
opt := p.mustOption(name)
if !opt.Set {
return opt.DefaultValue
}
return opt.Value
}
func (p *Program) mustOption(name string) *Option {
if cmd := p.selectedCommand; cmd != nil {
option, found := cmd.options[name]
if found {
return option
}
}
option, found := p.options[name]
if !found {
Panic("unknown option %q", name)
}
return option
}
func (p *Program) ArgumentValue(name string) string {
return p.mustArgument(name).Value
}
func (p *Program) OptionalArgumentValue(name string) *string {
arg := p.mustArgument(name)
if !arg.Set {
return nil
}
return &arg.Value
}
func (p *Program) TrailingArgumentValues(name string) []string {
return p.mustArgument(name).TrailingValues
}
func (p *Program) mustArgument(name string) *Argument {
var arguments []*Argument
if cmd := p.selectedCommand; cmd == nil {
arguments = p.arguments
} else {
arguments = cmd.arguments
}
for _, argument := range arguments {
if name == argument.Name {
return argument
}
}
Panic("unknown argument %q", name)
return nil // make the compiler happy
}
func (p *Program) findCommand(names []string) *Command {
cmd := p.command
for _, name := range names {
cmd = cmd.subcommands[name]
if cmd == nil {
return nil
}
}
if cmd == p.command {
return nil
}
return cmd
}
func (p *Program) ParseCommandLine() {
if p.command != nil {
p.addDefaultCommands()
}
p.parse()
if p.IsOptionSet("help") {
cmdHelp(p)
os.Exit(0)
}
p.Quiet = p.IsOptionSet("quiet")
if p.IsOptionSet("debug") {
s := p.OptionValue("debug")
i, err := strconv.ParseInt(s, 10, 64)
if err != nil || i < 0 || i > math.MaxInt32 {
p.Fatal("invalid debug level %q", s)
}
p.DebugLevel = int(i)
}
}
func (p *Program) addDefaultOptions() {
p.AddFlag("h", "help", "print help and exit")
p.AddFlag("q", "quiet", "do not print status and information messages")
p.AddOption("", "debug", "level", "0", "print debug messages")
}
func (p *Program) addDefaultCommands() {
c := p.AddCommand("help", "print help and exit", cmdHelp)
c.AddTrailingArgument("command", "the name of the command")
}
func cmdHelp(p *Program) {
cmd := p.command
if p.selectedCommand != nil {
if p.selectedCommand.FullName == "help" && !p.IsOptionSet("help") {
names := p.TrailingArgumentValues("command")
if len(names) > 0 {
cmd = p.findCommand(names)
if cmd == nil {
p.Fatal("unknown command %q", strings.Join(names, " "))
}
}
} else {
cmd = p.selectedCommand
}
}
p.PrintUsage(cmd)
}
func splitCommandName(s string) []string {
s = strings.TrimSpace(s)
if len(s) == 0 {
return []string{}
}
parts := commandNameRE.Split(s, -1)
var names []string
for _, part := range parts {
if name := strings.TrimSpace(part); name != "" {
names = append(names, name)
}
}
return names
}