-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
541 lines (452 loc) · 12.5 KB
/
options.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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
// options.go - Self documenting CLI options parser for Go
//
// Copyright (c) 2012 Simon Menke <simon.menke@gmail.com>
// Changes/Enhancements Copyright (c) 2015-2016 Sudhi Herle <sudhi@herle.net>
//
// This software does not come with any express or implied
// warranty; it is provided "as is". No claim is made to its
// suitability for any purpose.
// Package options implements a self-documenting command line
// options parsing framework.
//
// The list of options a program wishes to use is specified as a
// lengthy, multi-line string. This string also serves as the
// help-string for the options.
//
// Here is an example option specification:
//
// usage: example-tool
// A short description of the command
// --
// flag --flag,-f,FLAG A description for this flag
// option= --option=,-o=,OPTION= A description for this option
// the description continues here
// !required= --required,-r=,REQUIRED= A required option
// --
// env_var= ENV_VAR= An environment variable
// --
// help help,h Show this help message
// run run Run some function
// --
// Additional help for options or defaults etc. go here.
package options
import (
"fmt"
"os"
"strconv"
"strings"
)
// Representation of a parsed option specification.
type Spec struct {
usage string
allow_unknown_args bool
options map[string]string
defaults map[string]string
flags map[string]bool
required map[string]bool
environment map[string]string
commands map[string]string
}
// Representation of parsed command line arguments according to a
// given option specification
type Options struct {
options map[string]string
// If the options are repeated on the command line, second and subsequent
// values are in optionv
optionv map[string][]string
defaults map[string]string
Command string
Args []string
}
// Parse a spec string and return a Spec object
func Parse(desc string) (spec *Spec, err error) {
spec = new(Spec)
spec.options = make(map[string]string, 0)
spec.defaults = make(map[string]string, 0)
spec.flags = make(map[string]bool, 0)
spec.required = make(map[string]bool, 0)
spec.commands = make(map[string]string, 0)
spec.environment = make(map[string]string, 0)
spec.allow_unknown_args = false
g_indent := -1
indent := -1
section := 0
lines := []string{}
for _, line := range strings.Split(desc, "\n") {
if g_indent == -1 {
clean_line := strings.TrimLeft(line, " \t")
if clean_line != "" {
g_indent = len(line) - len(clean_line)
}
} else {
line = line[g_indent:]
}
line := strings.TrimRight(line, " \t")
if line == "" {
if section != 1 && section != 2 && section != 3 {
lines = append(lines, line)
}
continue
}
if section == 1 || section == 2 || section == 3 {
if strings.HasPrefix(line, "#") {
if indent == -1 {
indent = len(line) - len(strings.TrimLeft(line[1:], " \t"))
}
if line == "#" {
lines = append(lines, "")
} else {
line = line[indent:]
lines = append(lines, line)
}
continue
}
}
switch section {
case 0: // usage
if line == "--" {
if len(lines) > 0 && lines[len(lines)-1] != "" {
lines = append(lines, "")
}
section += 1
continue
}
lines = append(lines, line)
case 1: // options
if line == "--" {
if len(lines) > 0 && lines[len(lines)-1] != "" {
lines = append(lines, "")
}
section += 1
continue
}
parts := strings.SplitN(line, " ", 2)
if len(parts) == 1 {
err = fmt.Errorf("Invalid option spec: %s", line)
return
}
if indent == -1 {
indent = len(line) - len(strings.TrimLeft(parts[1], " \t"))
}
option := parts[0]
line = strings.Trim(parts[1], " \t")
required := false
flag := true
if strings.HasPrefix(option, "!") {
option = option[1:]
required = true
}
if strings.Contains(option, "=") {
ks := strings.Split(option, "=")
option = ks[0]
if defval := ks[1]; len(defval) > 0 {
spec.defaults[option] = defval
}
flag = false
}
spec.flags[option] = flag
spec.required[option] = required
parts = strings.SplitN(line, " ", 2)
if len(parts) == 1 {
parts = append(parts, "-")
}
parts[1] = strings.Trim(parts[1], " \t")
if parts[1] != "-" {
lines = append(lines, " "+line)
}
parts = strings.Split(parts[0], ",")
for _, part := range parts {
pieces := strings.SplitN(part, "=", 2)
part = pieces[0]
if strings.HasPrefix(part, "--") || strings.HasPrefix(part, "-") {
spec.options[part] = option
continue
}
spec.environment[part] = option
}
case 2: // environment variables
if line == "--" {
if len(lines) > 0 && lines[len(lines)-1] != "" {
lines = append(lines, "")
}
section += 1
continue
}
parts := strings.SplitN(line, " ", 2)
if len(parts) == 1 {
err = fmt.Errorf("Invalid env spec: %s", line)
return
}
if indent == -1 {
indent = len(line) - len(strings.TrimLeft(parts[1], " \t"))
}
env := parts[0]
line = strings.Trim(parts[1], " \t")
required := false
flag := true
if strings.HasPrefix(env, "!") {
env = env[1:]
required = true
}
if strings.HasSuffix(env, "=") {
env = env[0 : len(env)-1]
flag = false
}
spec.flags[env] = flag
spec.required[env] = required
parts = strings.SplitN(line, " ", 2)
if len(parts) == 1 {
parts = append(parts, "-")
}
parts[1] = strings.Trim(parts[1], " \t")
if parts[1] != "-" {
lines = append(lines, " "+line)
}
parts = strings.Split(parts[0], ",")
for _, part := range parts {
part = strings.SplitN(part, "=", 2)[0]
spec.environment[part] = env
}
case 3: // commands
if line == "--" {
if len(lines) > 0 && lines[len(lines)-1] != "" {
lines = append(lines, "")
}
section += 1
continue
}
if line == "*" {
spec.allow_unknown_args = true
continue
}
parts := strings.SplitN(line, " ", 2)
if len(parts) == 1 {
err = fmt.Errorf("Invalid command spec: %s", line)
return
}
if indent == -1 {
indent = len(line) - len(strings.TrimLeft(parts[1], " \t"))
}
command := parts[0]
line = strings.Trim(parts[1], " \t")
parts = strings.SplitN(line, " ", 2)
if len(parts) == 1 {
parts = append(parts, "-")
}
parts[1] = strings.Trim(parts[1], " \t")
if parts[1] != "-" {
lines = append(lines, " "+line)
}
parts = strings.Split(parts[0], ",")
for _, part := range parts {
spec.commands[part] = command
}
case 4: // appendix
if line == "--" {
if len(lines) > 0 && lines[len(lines)-1] != "" {
lines = append(lines, "")
}
section += 1
continue
}
lines = append(lines, line)
}
}
spec.usage = strings.Join(lines, "\n") + "\n"
spec.usage = strings.Trim(spec.usage, " \t\n")
//fmt.Printf("Parsed data:\n%+v\n", spec)
return
}
// Parse a spec string and die if it fails
func MustParse(desc string) *Spec {
var p *Spec
var err error
if p, err = Parse(desc); err != nil {
fmt.Fprintf(os.Stderr, "Spec parse error for\n'%.80s' ..\n%s\n", desc, err)
os.Exit(1)
}
return p
}
// Parse the command line arguments in 'args' and the environment
// variables in 'environ'. This expects the parsing to succeed and
// exits with usage string and error if the parsing fails.
func (this *Spec) MustInterpret(args []string, environ []string) *Options {
opts, err := this.Interpret(args, environ)
if err != nil {
this.PrintUsageWithError(err)
}
return opts
}
// Parse the command line arguments in 'args' and the environment
// variables in 'environ'. Return the resulting, parsed options in
// 'o' and any error in 'err'.
func (spec *Spec) Interpret(args []string, environ []string) (o *Options, err error) {
opts := new(Options)
opts.options = make(map[string]string, 0)
opts.optionv = make(map[string][]string, 0)
opts.defaults = spec.defaults
opts.Args = []string{}
for _, env := range environ {
parts := strings.SplitN(env, "=", 2)
if option, present := spec.environment[parts[0]]; present {
opts.options[option] = parts[1]
}
}
//fmt.Printf("Options: %+v\n", spec.options)
for i := 1; i < len(args); i++ {
arg := args[i]
// A lone "--" terminates option parsing
if arg == "--" {
if i+1 < len(args) {
opts.Args = append(opts.Args, args[i+1:]...)
}
break
}
if strings.HasPrefix(arg, "--") || strings.HasPrefix(arg, "-") {
option := "-"
value := "true"
parts := strings.SplitN(arg, "=", 2)
//fmt.Printf("<< arg %d: %s >>: parts = %v\n", i, arg, parts)
if len(parts) == 2 {
option = parts[0]
} else {
option = arg
}
if opt, present := spec.options[option]; present {
option = opt
} else {
err = fmt.Errorf("Invalid option: %s was not recognized", arg)
return
}
if spec.flags[option] {
if len(parts) == 2 {
err = fmt.Errorf("Invalid option: %s was not recognized (doesn't take a value)", arg)
return
}
} else {
if len(parts) == 2 {
value = parts[1]
} else if len(args) > i+1 {
value = args[i+1]
i++
} else {
err = fmt.Errorf("Invalid option: %s was not recognized (requires a value)", arg)
return
}
}
// second and subsequent options go in optionv
if _, ok := opts.options[option]; ok {
opts.optionv[option] = append(opts.optionv[option], value)
} else {
opts.options[option] = value
}
continue
}
if command, present := spec.commands[arg]; present {
opts.Command = command
opts.Args = args[i:]
opts.Args[0] = opts.Command
break
}
if spec.allow_unknown_args {
opts.Args = append(opts.Args, arg)
continue
}
err = fmt.Errorf("Invalid argument: %s was not recognized", arg)
return
}
for option, required := range spec.required {
if _, present := opts.options[option]; required && !present {
err = fmt.Errorf("Missing option: %s", option)
return
}
}
for env, option := range spec.environment {
if value, present := opts.options[option]; present {
os.Setenv(env, value)
}
}
o = opts
return
}
// Print the usage string to STDOUT
func (spec *Spec) PrintUsage() {
fmt.Fprintf(os.Stdout, "%s\n", spec.usage)
}
// Print the usage string to STDOUT and exit with a non-zero code.
func (spec *Spec) PrintUsageAndExit() {
spec.PrintUsage()
os.Exit(1)
}
// Print the error string corresponding to 'err' and then show the
// usage string. Both are sent to STDERR. Exit with a non-zero code.
func (spec *Spec) PrintUsageWithError(err error) {
fmt.Fprintf(os.Stderr, "error: %s\n%s\n", err, spec.usage)
os.Exit(1)
}
// Return the option corresponding to 'nm'. If the option is not set
// (provided on the command line), the bool retval will be False.
func (opts *Options) Get(nm string) (string, bool) {
if v, ok := opts.options[nm]; ok {
return v, true
}
if v, ok := opts.defaults[nm]; ok {
return v, true
}
return "", false
}
// For options that are providd multiple times, return all of them in a
// slice. A nil slice implies the option was not set on the command line.
func (opts *Options) GetMulti(nm string) []string {
var rv []string
var v string
var ok bool
if v, ok = opts.options[nm]; !ok {
return nil
}
rv = append(rv, v)
rv = append(rv, opts.optionv[nm]...)
return rv
}
// Interpret the option corresponding to the key 'nm' as
// a Bool and parse it. A failed parse defaults to False.
func (opts *Options) GetBool(nm string) bool {
if v, ok := opts.Get(nm); ok {
switch strings.ToLower(v) {
case "true", "ok", "1", "yes", "on":
return true
default:
return false
}
}
return false
}
// Interpret the option corresponding to the key 'nm' as a signed
// integer (auto-detected base). The second retval will be false if
// the parse fails or the key is not found.
func (opts *Options) GetInt(nm string) (int64, bool) {
if v, ok := opts.Get(nm); ok {
if i, err := strconv.ParseInt(v, 0, 64); err == nil {
return i, true
}
}
return 0, false
}
// Interpret the option corresponding to the key 'nm' as an unsigned
// integer (auto-detected base). The second retval will be false if
// the parse fails or the key is not found.
func (opts *Options) GetUint(nm string) (uint64, bool) {
if v, ok := opts.Get(nm); ok {
if i, err := strconv.ParseUint(v, 0, 64); err == nil {
return i, true
}
}
return 0, false
}
// Return true if the option with the key 'nm' is set (i.e., provided
// on the command line).
func (opts *Options) IsSet(nm string) bool {
_, ok := opts.options[nm]
return ok
}
// vim: ft=go:sw=4:ts=4:tw=78:expandtab: