docopt 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/docopt.gemspec +5 -3
- data/lib/docopt.rb +129 -122
- data/test/language_agnostic_tester.py +48 -0
- data/test/test_docopt.rb +7 -44
- data/test/testcases.docopt +909 -0
- data/test/testee.rb +0 -0
- metadata +25 -18
@@ -0,0 +1,48 @@
|
|
1
|
+
#! /usr/bin/env python
|
2
|
+
import sys, json, re, os
|
3
|
+
from subprocess import Popen, PIPE, STDOUT
|
4
|
+
|
5
|
+
fixtures = open(os.path.join(os.path.dirname(__file__), 'testcases.docopt'), 'r').read()
|
6
|
+
|
7
|
+
# remove comments
|
8
|
+
fixtures = re.sub('#.*$', '', fixtures, flags=re.M)
|
9
|
+
|
10
|
+
testee = (sys.argv[1] if len(sys.argv) >= 2 else
|
11
|
+
exit('Usage: language_agnostic_tester.py ./path/to/executable/testee [ID ...]'))
|
12
|
+
ids = [int(x) for x in sys.argv[2:]] if len(sys.argv) > 2 else None
|
13
|
+
summary = ''
|
14
|
+
|
15
|
+
index = 0
|
16
|
+
for fixture in fixtures.split('r"""'):
|
17
|
+
doc, _, body = fixture.partition('"""')
|
18
|
+
for case in body.split('$')[1:]:
|
19
|
+
index += 1
|
20
|
+
if ids is not None and index not in ids:
|
21
|
+
continue
|
22
|
+
argv, _, expect = case.strip().partition('\n')
|
23
|
+
prog, _, argv = argv.strip().partition(' ')
|
24
|
+
assert prog == 'prog', repr(prog)
|
25
|
+
p = Popen(testee + ' ' + argv,
|
26
|
+
stdout=PIPE, stdin=PIPE, stderr=STDOUT, shell=True)
|
27
|
+
result = p.communicate(input=doc)[0]
|
28
|
+
try:
|
29
|
+
py_result = json.loads(result)
|
30
|
+
py_expect = json.loads(expect)
|
31
|
+
except:
|
32
|
+
summary += 'J'
|
33
|
+
print (' %d: BAD JSON ' % index).center(79, '=')
|
34
|
+
print 'result>', result
|
35
|
+
print 'expect>', expect
|
36
|
+
continue
|
37
|
+
if py_result == py_expect:
|
38
|
+
summary += '.'
|
39
|
+
else:
|
40
|
+
print (' %d: FAILED ' % index).center(79, '=')
|
41
|
+
print 'r"""%s"""' % doc
|
42
|
+
print '$ prog %s\n' % argv
|
43
|
+
print 'result>', result
|
44
|
+
print 'expect>', expect
|
45
|
+
summary += 'F'
|
46
|
+
|
47
|
+
print (' %d / %d ' % (summary.count('.'), len(summary))).center(79, '=')
|
48
|
+
print summary
|
data/test/test_docopt.rb
CHANGED
@@ -1,49 +1,12 @@
|
|
1
1
|
require 'test/unit'
|
2
|
+
require 'pathname'
|
2
3
|
|
3
4
|
class DocoptTest < Test::Unit::TestCase
|
4
|
-
def setup
|
5
|
-
$LOAD_PATH << File.dirname(__FILE__)
|
6
|
-
load 'example.rb'
|
7
|
-
end
|
8
|
-
|
9
|
-
def get_options(argv=[])
|
10
|
-
begin
|
11
|
-
Docopt($DOC, { :argv => argv })
|
12
|
-
rescue SystemExit => ex
|
13
|
-
nil
|
14
|
-
end
|
15
|
-
end
|
16
5
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
def test_option
|
23
|
-
options = get_options(['arg'])
|
24
|
-
assert_equal ".svn,CVS,.bzr,.hg,.git", options['--exclude']
|
25
|
-
end
|
26
|
-
|
27
|
-
def test_values
|
28
|
-
options = get_options(['arg'])
|
29
|
-
assert !options['--help']
|
30
|
-
assert !options['-h']
|
31
|
-
assert !options['--version']
|
32
|
-
assert !options['-v']
|
33
|
-
assert !options['--verbose']
|
34
|
-
assert !options['--quiet']
|
35
|
-
assert !options['-q']
|
36
|
-
assert !options['--repeat']
|
37
|
-
assert !options['-r']
|
38
|
-
assert_equal ".svn,CVS,.bzr,.hg,.git", options['--exclude']
|
39
|
-
assert_equal "*.rb", options['--filename']
|
40
|
-
assert !options['--select']
|
41
|
-
assert !options['--ignore']
|
42
|
-
assert !options['--show-source']
|
43
|
-
assert !options['--statistics']
|
44
|
-
assert !options['--count']
|
45
|
-
assert !options['--benchmark']
|
46
|
-
assert !options['--testsuite']
|
47
|
-
assert !options['--doctest']
|
6
|
+
TOPDIR = Pathname.new(__FILE__).dirname.dirname
|
7
|
+
|
8
|
+
def test_docopt_reference_testcases
|
9
|
+
puts
|
10
|
+
assert system('python', "test/language_agnostic_tester.py", "test/testee.rb", chdir: TOPDIR)
|
48
11
|
end
|
49
|
-
end
|
12
|
+
end
|
@@ -0,0 +1,909 @@
|
|
1
|
+
r"""Usage: prog
|
2
|
+
|
3
|
+
"""
|
4
|
+
$ prog
|
5
|
+
{}
|
6
|
+
|
7
|
+
$ prog --xxx
|
8
|
+
"user-error"
|
9
|
+
|
10
|
+
|
11
|
+
r"""Usage: prog [options]
|
12
|
+
|
13
|
+
-a All.
|
14
|
+
|
15
|
+
"""
|
16
|
+
$ prog
|
17
|
+
{"-a": false}
|
18
|
+
|
19
|
+
$ prog -a
|
20
|
+
{"-a": true}
|
21
|
+
|
22
|
+
$ prog -x
|
23
|
+
"user-error"
|
24
|
+
|
25
|
+
|
26
|
+
r"""Usage: prog [options]
|
27
|
+
|
28
|
+
--all All.
|
29
|
+
|
30
|
+
"""
|
31
|
+
$ prog
|
32
|
+
{"--all": false}
|
33
|
+
|
34
|
+
$ prog --all
|
35
|
+
{"--all": true}
|
36
|
+
|
37
|
+
$ prog --xxx
|
38
|
+
"user-error"
|
39
|
+
|
40
|
+
|
41
|
+
r"""Usage: prog [options]
|
42
|
+
|
43
|
+
-v, --verbose Verbose.
|
44
|
+
|
45
|
+
"""
|
46
|
+
$ prog --verbose
|
47
|
+
{"--verbose": true}
|
48
|
+
|
49
|
+
$ prog --ver
|
50
|
+
{"--verbose": true}
|
51
|
+
|
52
|
+
$ prog -v
|
53
|
+
{"--verbose": true}
|
54
|
+
|
55
|
+
|
56
|
+
r"""Usage: prog [options]
|
57
|
+
|
58
|
+
-p PATH
|
59
|
+
|
60
|
+
"""
|
61
|
+
$ prog -p home/
|
62
|
+
{"-p": "home/"}
|
63
|
+
|
64
|
+
$ prog -phome/
|
65
|
+
{"-p": "home/"}
|
66
|
+
|
67
|
+
$ prog -p
|
68
|
+
"user-error"
|
69
|
+
|
70
|
+
|
71
|
+
r"""Usage: prog [options]
|
72
|
+
|
73
|
+
--path <path>
|
74
|
+
|
75
|
+
"""
|
76
|
+
$ prog --path home/
|
77
|
+
{"--path": "home/"}
|
78
|
+
|
79
|
+
$ prog --path=home/
|
80
|
+
{"--path": "home/"}
|
81
|
+
|
82
|
+
$ prog --pa home/
|
83
|
+
{"--path": "home/"}
|
84
|
+
|
85
|
+
$ prog --pa=home/
|
86
|
+
{"--path": "home/"}
|
87
|
+
|
88
|
+
$ prog --path
|
89
|
+
"user-error"
|
90
|
+
|
91
|
+
|
92
|
+
r"""Usage: prog [options]
|
93
|
+
|
94
|
+
-p PATH, --path=<path> Path to files.
|
95
|
+
|
96
|
+
"""
|
97
|
+
$ prog -proot
|
98
|
+
{"--path": "root"}
|
99
|
+
|
100
|
+
|
101
|
+
r"""Usage: prog [options]
|
102
|
+
|
103
|
+
-p --path PATH Path to files.
|
104
|
+
|
105
|
+
"""
|
106
|
+
$ prog -p root
|
107
|
+
{"--path": "root"}
|
108
|
+
|
109
|
+
$ prog --path root
|
110
|
+
{"--path": "root"}
|
111
|
+
|
112
|
+
|
113
|
+
r"""Usage: prog [options]
|
114
|
+
|
115
|
+
-p PATH Path to files [default: ./]
|
116
|
+
|
117
|
+
"""
|
118
|
+
$ prog
|
119
|
+
{"-p": "./"}
|
120
|
+
|
121
|
+
$ prog -phome
|
122
|
+
{"-p": "home"}
|
123
|
+
|
124
|
+
|
125
|
+
r"""UsAgE: prog [options]
|
126
|
+
|
127
|
+
--path=<files> Path to files
|
128
|
+
[dEfAuLt: /root]
|
129
|
+
|
130
|
+
"""
|
131
|
+
$ prog
|
132
|
+
{"--path": "/root"}
|
133
|
+
|
134
|
+
$ prog --path=home
|
135
|
+
{"--path": "home"}
|
136
|
+
|
137
|
+
|
138
|
+
r"""usage: prog [options]
|
139
|
+
|
140
|
+
-a Add
|
141
|
+
-r Remote
|
142
|
+
-m <msg> Message
|
143
|
+
|
144
|
+
"""
|
145
|
+
$ prog -a -r -m Hello
|
146
|
+
{"-a": true,
|
147
|
+
"-r": true,
|
148
|
+
"-m": "Hello"}
|
149
|
+
|
150
|
+
$ prog -armyourass
|
151
|
+
{"-a": true,
|
152
|
+
"-r": true,
|
153
|
+
"-m": "yourass"}
|
154
|
+
|
155
|
+
$ prog -a -r
|
156
|
+
{"-a": true,
|
157
|
+
"-r": true,
|
158
|
+
"-m": null}
|
159
|
+
|
160
|
+
|
161
|
+
r"""Usage: prog [options]
|
162
|
+
|
163
|
+
--version
|
164
|
+
--verbose
|
165
|
+
|
166
|
+
"""
|
167
|
+
$ prog --version
|
168
|
+
{"--version": true,
|
169
|
+
"--verbose": false}
|
170
|
+
|
171
|
+
$ prog --verbose
|
172
|
+
{"--version": false,
|
173
|
+
"--verbose": true}
|
174
|
+
|
175
|
+
$ prog --ver
|
176
|
+
"user-error"
|
177
|
+
|
178
|
+
$ prog --verb
|
179
|
+
{"--version": false,
|
180
|
+
"--verbose": true}
|
181
|
+
|
182
|
+
|
183
|
+
r"""usage: prog [-a -r -m <msg>]
|
184
|
+
|
185
|
+
-a Add
|
186
|
+
-r Remote
|
187
|
+
-m <msg> Message
|
188
|
+
|
189
|
+
"""
|
190
|
+
$ prog -armyourass
|
191
|
+
{"-a": true,
|
192
|
+
"-r": true,
|
193
|
+
"-m": "yourass"}
|
194
|
+
|
195
|
+
|
196
|
+
r"""usage: prog [-armmsg]
|
197
|
+
|
198
|
+
-a Add
|
199
|
+
-r Remote
|
200
|
+
-m <msg> Message
|
201
|
+
|
202
|
+
"""
|
203
|
+
$ prog -a -r -m Hello
|
204
|
+
{"-a": true,
|
205
|
+
"-r": true,
|
206
|
+
"-m": "Hello"}
|
207
|
+
|
208
|
+
|
209
|
+
r"""usage: prog -a -b
|
210
|
+
|
211
|
+
-a
|
212
|
+
-b
|
213
|
+
|
214
|
+
"""
|
215
|
+
$ prog -a -b
|
216
|
+
{"-a": true, "-b": true}
|
217
|
+
|
218
|
+
$ prog -b -a
|
219
|
+
{"-a": true, "-b": true}
|
220
|
+
|
221
|
+
$ prog -a
|
222
|
+
"user-error"
|
223
|
+
|
224
|
+
$ prog
|
225
|
+
"user-error"
|
226
|
+
|
227
|
+
|
228
|
+
r"""usage: prog (-a -b)
|
229
|
+
|
230
|
+
-a
|
231
|
+
-b
|
232
|
+
|
233
|
+
"""
|
234
|
+
$ prog -a -b
|
235
|
+
{"-a": true, "-b": true}
|
236
|
+
|
237
|
+
$ prog -b -a
|
238
|
+
{"-a": true, "-b": true}
|
239
|
+
|
240
|
+
$ prog -a
|
241
|
+
"user-error"
|
242
|
+
|
243
|
+
$ prog
|
244
|
+
"user-error"
|
245
|
+
|
246
|
+
|
247
|
+
r"""usage: prog [-a] -b
|
248
|
+
|
249
|
+
-a
|
250
|
+
-b
|
251
|
+
|
252
|
+
"""
|
253
|
+
$ prog -a -b
|
254
|
+
{"-a": true, "-b": true}
|
255
|
+
|
256
|
+
$ prog -b -a
|
257
|
+
{"-a": true, "-b": true}
|
258
|
+
|
259
|
+
$ prog -a
|
260
|
+
"user-error"
|
261
|
+
|
262
|
+
$ prog -b
|
263
|
+
{"-a": false, "-b": true}
|
264
|
+
|
265
|
+
$ prog
|
266
|
+
"user-error"
|
267
|
+
|
268
|
+
|
269
|
+
r"""usage: prog [(-a -b)]
|
270
|
+
|
271
|
+
-a
|
272
|
+
-b
|
273
|
+
|
274
|
+
"""
|
275
|
+
$ prog -a -b
|
276
|
+
{"-a": true, "-b": true}
|
277
|
+
|
278
|
+
$ prog -b -a
|
279
|
+
{"-a": true, "-b": true}
|
280
|
+
|
281
|
+
$ prog -a
|
282
|
+
"user-error"
|
283
|
+
|
284
|
+
$ prog -b
|
285
|
+
"user-error"
|
286
|
+
|
287
|
+
$ prog
|
288
|
+
{"-a": false, "-b": false}
|
289
|
+
|
290
|
+
|
291
|
+
r"""usage: prog (-a|-b)
|
292
|
+
|
293
|
+
-a
|
294
|
+
-b
|
295
|
+
|
296
|
+
"""
|
297
|
+
$ prog -a -b
|
298
|
+
"user-error"
|
299
|
+
|
300
|
+
$ prog
|
301
|
+
"user-error"
|
302
|
+
|
303
|
+
$ prog -a
|
304
|
+
{"-a": true, "-b": false}
|
305
|
+
|
306
|
+
$ prog -b
|
307
|
+
{"-a": false, "-b": true}
|
308
|
+
|
309
|
+
|
310
|
+
r"""usage: prog [ -a | -b ]
|
311
|
+
|
312
|
+
-a
|
313
|
+
-b
|
314
|
+
|
315
|
+
"""
|
316
|
+
$ prog -a -b
|
317
|
+
"user-error"
|
318
|
+
|
319
|
+
$ prog
|
320
|
+
{"-a": false, "-b": false}
|
321
|
+
|
322
|
+
$ prog -a
|
323
|
+
{"-a": true, "-b": false}
|
324
|
+
|
325
|
+
$ prog -b
|
326
|
+
{"-a": false, "-b": true}
|
327
|
+
|
328
|
+
|
329
|
+
r"""usage: prog <arg>
|
330
|
+
|
331
|
+
"""
|
332
|
+
$ prog 10
|
333
|
+
{"<arg>": "10"}
|
334
|
+
|
335
|
+
$ prog 10 20
|
336
|
+
"user-error"
|
337
|
+
|
338
|
+
$ prog
|
339
|
+
"user-error"
|
340
|
+
|
341
|
+
|
342
|
+
r"""usage: prog [<arg>]
|
343
|
+
|
344
|
+
"""
|
345
|
+
$ prog 10
|
346
|
+
{"<arg>": "10"}
|
347
|
+
|
348
|
+
$ prog 10 20
|
349
|
+
"user-error"
|
350
|
+
|
351
|
+
$ prog
|
352
|
+
{"<arg>": null}
|
353
|
+
|
354
|
+
|
355
|
+
r"""usage: prog <kind> <name> <type>
|
356
|
+
|
357
|
+
"""
|
358
|
+
$ prog 10 20 40
|
359
|
+
{"<kind>": "10", "<name>": "20", "<type>": "40"}
|
360
|
+
|
361
|
+
$ prog 10 20
|
362
|
+
"user-error"
|
363
|
+
|
364
|
+
$ prog
|
365
|
+
"user-error"
|
366
|
+
|
367
|
+
|
368
|
+
r"""usage: prog <kind> [<name> <type>]
|
369
|
+
|
370
|
+
"""
|
371
|
+
$ prog 10 20 40
|
372
|
+
{"<kind>": "10", "<name>": "20", "<type>": "40"}
|
373
|
+
|
374
|
+
$ prog 10 20
|
375
|
+
{"<kind>": "10", "<name>": "20", "<type>": null}
|
376
|
+
|
377
|
+
$ prog
|
378
|
+
"user-error"
|
379
|
+
|
380
|
+
|
381
|
+
r"""usage: prog [<kind> | <name> <type>]
|
382
|
+
|
383
|
+
"""
|
384
|
+
$ prog 10 20 40
|
385
|
+
"user-error"
|
386
|
+
|
387
|
+
$ prog 20 40
|
388
|
+
{"<kind>": null, "<name>": "20", "<type>": "40"}
|
389
|
+
|
390
|
+
$ prog
|
391
|
+
{"<kind>": null, "<name>": null, "<type>": null}
|
392
|
+
|
393
|
+
|
394
|
+
r"""usage: prog (<kind> --all | <name>)
|
395
|
+
|
396
|
+
--all
|
397
|
+
|
398
|
+
"""
|
399
|
+
$ prog 10 --all
|
400
|
+
{"<kind>": "10", "--all": true, "<name>": null}
|
401
|
+
|
402
|
+
$ prog 10
|
403
|
+
{"<kind>": null, "--all": false, "<name>": "10"}
|
404
|
+
|
405
|
+
$ prog
|
406
|
+
"user-error"
|
407
|
+
|
408
|
+
|
409
|
+
r"""usage: prog [<name> <name>]
|
410
|
+
|
411
|
+
"""
|
412
|
+
$ prog 10 20
|
413
|
+
{"<name>": ["10", "20"]}
|
414
|
+
|
415
|
+
$ prog 10
|
416
|
+
{"<name>": ["10"]}
|
417
|
+
|
418
|
+
$ prog
|
419
|
+
{"<name>": []}
|
420
|
+
|
421
|
+
|
422
|
+
r"""usage: prog [(<name> <name>)]
|
423
|
+
|
424
|
+
"""
|
425
|
+
$ prog 10 20
|
426
|
+
{"<name>": ["10", "20"]}
|
427
|
+
|
428
|
+
$ prog 10
|
429
|
+
"user-error"
|
430
|
+
|
431
|
+
$ prog
|
432
|
+
{"<name>": []}
|
433
|
+
|
434
|
+
|
435
|
+
r"""usage: prog NAME...
|
436
|
+
|
437
|
+
"""
|
438
|
+
$ prog 10 20
|
439
|
+
{"NAME": ["10", "20"]}
|
440
|
+
|
441
|
+
$ prog 10
|
442
|
+
{"NAME": ["10"]}
|
443
|
+
|
444
|
+
$ prog
|
445
|
+
"user-error"
|
446
|
+
|
447
|
+
|
448
|
+
r"""usage: prog [NAME]...
|
449
|
+
|
450
|
+
"""
|
451
|
+
$ prog 10 20
|
452
|
+
{"NAME": ["10", "20"]}
|
453
|
+
|
454
|
+
$ prog 10
|
455
|
+
{"NAME": ["10"]}
|
456
|
+
|
457
|
+
$ prog
|
458
|
+
{"NAME": []}
|
459
|
+
|
460
|
+
|
461
|
+
r"""usage: prog [NAME...]
|
462
|
+
|
463
|
+
"""
|
464
|
+
$ prog 10 20
|
465
|
+
{"NAME": ["10", "20"]}
|
466
|
+
|
467
|
+
$ prog 10
|
468
|
+
{"NAME": ["10"]}
|
469
|
+
|
470
|
+
$ prog
|
471
|
+
{"NAME": []}
|
472
|
+
|
473
|
+
|
474
|
+
r"""usage: prog [NAME [NAME ...]]
|
475
|
+
|
476
|
+
"""
|
477
|
+
$ prog 10 20
|
478
|
+
{"NAME": ["10", "20"]}
|
479
|
+
|
480
|
+
$ prog 10
|
481
|
+
{"NAME": ["10"]}
|
482
|
+
|
483
|
+
$ prog
|
484
|
+
{"NAME": []}
|
485
|
+
|
486
|
+
|
487
|
+
r"""usage: prog (NAME | --foo NAME)
|
488
|
+
|
489
|
+
--foo
|
490
|
+
|
491
|
+
"""
|
492
|
+
$ prog 10
|
493
|
+
{"NAME": "10", "--foo": false}
|
494
|
+
|
495
|
+
$ prog --foo 10
|
496
|
+
{"NAME": "10", "--foo": true}
|
497
|
+
|
498
|
+
$ prog --foo=10
|
499
|
+
"user-error"
|
500
|
+
|
501
|
+
|
502
|
+
r"""usage: prog (NAME | --foo) [--bar | NAME]
|
503
|
+
|
504
|
+
--foo
|
505
|
+
--bar
|
506
|
+
|
507
|
+
"""
|
508
|
+
$ prog 10
|
509
|
+
{"NAME": ["10"], "--foo": false, "--bar": false}
|
510
|
+
|
511
|
+
$ prog 10 20
|
512
|
+
{"NAME": ["10", "20"], "--foo": false, "--bar": false}
|
513
|
+
|
514
|
+
$ prog --foo --bar
|
515
|
+
{"NAME": [], "--foo": true, "--bar": true}
|
516
|
+
|
517
|
+
|
518
|
+
r"""Naval Fate.
|
519
|
+
|
520
|
+
Usage:
|
521
|
+
prog ship new <name>...
|
522
|
+
prog ship [<name>] move <x> <y> [--speed=<kn>]
|
523
|
+
prog ship shoot <x> <y>
|
524
|
+
prog mine (set|remove) <x> <y> [--moored|--drifting]
|
525
|
+
prog -h | --help
|
526
|
+
prog --version
|
527
|
+
|
528
|
+
Options:
|
529
|
+
-h --help Show this screen.
|
530
|
+
--version Show version.
|
531
|
+
--speed=<kn> Speed in knots [default: 10].
|
532
|
+
--moored Mored (anchored) mine.
|
533
|
+
--drifting Drifting mine.
|
534
|
+
|
535
|
+
"""
|
536
|
+
$ prog ship Guardian move 150 300 --speed=20
|
537
|
+
{"--drifting": false,
|
538
|
+
"--help": false,
|
539
|
+
"--moored": false,
|
540
|
+
"--speed": "20",
|
541
|
+
"--version": false,
|
542
|
+
"<name>": ["Guardian"],
|
543
|
+
"<x>": "150",
|
544
|
+
"<y>": "300",
|
545
|
+
"mine": false,
|
546
|
+
"move": true,
|
547
|
+
"new": false,
|
548
|
+
"remove": false,
|
549
|
+
"set": false,
|
550
|
+
"ship": true,
|
551
|
+
"shoot": false}
|
552
|
+
|
553
|
+
|
554
|
+
r"""usage: prog --hello
|
555
|
+
|
556
|
+
"""
|
557
|
+
$ prog --hello
|
558
|
+
{"--hello": true}
|
559
|
+
|
560
|
+
|
561
|
+
r"""usage: prog [--hello=<world>]
|
562
|
+
|
563
|
+
"""
|
564
|
+
$ prog
|
565
|
+
{"--hello": null}
|
566
|
+
|
567
|
+
$ prog --hello wrld
|
568
|
+
{"--hello": "wrld"}
|
569
|
+
|
570
|
+
|
571
|
+
r"""usage: prog [-o]
|
572
|
+
|
573
|
+
"""
|
574
|
+
$ prog
|
575
|
+
{"-o": false}
|
576
|
+
|
577
|
+
$ prog -o
|
578
|
+
{"-o": true}
|
579
|
+
|
580
|
+
|
581
|
+
r"""usage: prog [-opr]
|
582
|
+
|
583
|
+
"""
|
584
|
+
$ prog -op
|
585
|
+
{"-o": true, "-p": true, "-r": false}
|
586
|
+
|
587
|
+
|
588
|
+
r"""usage: prog --aabb | --aa
|
589
|
+
|
590
|
+
"""
|
591
|
+
$ prog --aa
|
592
|
+
{"--aabb": false, "--aa": true}
|
593
|
+
|
594
|
+
$ prog --a
|
595
|
+
"user-error" # not a unique prefix
|
596
|
+
|
597
|
+
#
|
598
|
+
# Counting number of flags
|
599
|
+
#
|
600
|
+
|
601
|
+
r"""Usage: prog -v
|
602
|
+
|
603
|
+
"""
|
604
|
+
$ prog -v
|
605
|
+
{"-v": true}
|
606
|
+
|
607
|
+
|
608
|
+
r"""Usage: prog [-v -v]
|
609
|
+
|
610
|
+
"""
|
611
|
+
$ prog
|
612
|
+
{"-v": 0}
|
613
|
+
|
614
|
+
$ prog -v
|
615
|
+
{"-v": 1}
|
616
|
+
|
617
|
+
$ prog -vv
|
618
|
+
{"-v": 2}
|
619
|
+
|
620
|
+
|
621
|
+
r"""Usage: prog -v ...
|
622
|
+
|
623
|
+
"""
|
624
|
+
$ prog
|
625
|
+
"user-error"
|
626
|
+
|
627
|
+
$ prog -v
|
628
|
+
{"-v": 1}
|
629
|
+
|
630
|
+
$ prog -vv
|
631
|
+
{"-v": 2}
|
632
|
+
|
633
|
+
$ prog -vvvvvv
|
634
|
+
{"-v": 6}
|
635
|
+
|
636
|
+
|
637
|
+
r"""Usage: prog [-v | -vv | -vvv]
|
638
|
+
|
639
|
+
This one is probably most readable user-friednly variant.
|
640
|
+
|
641
|
+
"""
|
642
|
+
$ prog
|
643
|
+
{"-v": 0}
|
644
|
+
|
645
|
+
$ prog -v
|
646
|
+
{"-v": 1}
|
647
|
+
|
648
|
+
$ prog -vv
|
649
|
+
{"-v": 2}
|
650
|
+
|
651
|
+
$ prog -vvvv
|
652
|
+
"user-error"
|
653
|
+
|
654
|
+
|
655
|
+
r"""usage: prog [--ver --ver]
|
656
|
+
|
657
|
+
"""
|
658
|
+
$ prog --ver --ver
|
659
|
+
{"--ver": 2}
|
660
|
+
|
661
|
+
|
662
|
+
#
|
663
|
+
# Counting commands
|
664
|
+
#
|
665
|
+
|
666
|
+
r"""usage: prog [go]
|
667
|
+
|
668
|
+
"""
|
669
|
+
$ prog go
|
670
|
+
{"go": true}
|
671
|
+
|
672
|
+
|
673
|
+
r"""usage: prog [go go]
|
674
|
+
|
675
|
+
"""
|
676
|
+
$ prog
|
677
|
+
{"go": 0}
|
678
|
+
|
679
|
+
$ prog go
|
680
|
+
{"go": 1}
|
681
|
+
|
682
|
+
$ prog go go
|
683
|
+
{"go": 2}
|
684
|
+
|
685
|
+
$ prog go go go
|
686
|
+
"user-error"
|
687
|
+
|
688
|
+
r"""usage: prog go...
|
689
|
+
|
690
|
+
"""
|
691
|
+
$ prog go go go go go
|
692
|
+
{"go": 5}
|
693
|
+
|
694
|
+
#
|
695
|
+
# Test [options] shourtcut
|
696
|
+
#
|
697
|
+
|
698
|
+
r"""Usage: prog [options] A
|
699
|
+
|
700
|
+
-q Be quiet
|
701
|
+
-v Be verbose.
|
702
|
+
|
703
|
+
"""
|
704
|
+
$ prog arg
|
705
|
+
{"A": "arg", "-v": false, "-q": false}
|
706
|
+
|
707
|
+
$ prog -v arg
|
708
|
+
{"A": "arg", "-v": true, "-q": false}
|
709
|
+
|
710
|
+
$ prog -q arg
|
711
|
+
{"A": "arg", "-v": false, "-q": true}
|
712
|
+
|
713
|
+
#
|
714
|
+
# Test single dash
|
715
|
+
#
|
716
|
+
|
717
|
+
r"""usage: prog [-]"""
|
718
|
+
|
719
|
+
$ prog -
|
720
|
+
{"-": true}
|
721
|
+
|
722
|
+
$ prog
|
723
|
+
{"-": false}
|
724
|
+
|
725
|
+
#
|
726
|
+
# If argument is repeated, its value should always be a list
|
727
|
+
#
|
728
|
+
|
729
|
+
r"""usage: prog [NAME [NAME ...]]"""
|
730
|
+
|
731
|
+
$ prog a b
|
732
|
+
{"NAME": ["a", "b"]}
|
733
|
+
|
734
|
+
$ prog
|
735
|
+
{"NAME": []}
|
736
|
+
|
737
|
+
#
|
738
|
+
# Option's argument defaults to null/None
|
739
|
+
#
|
740
|
+
|
741
|
+
r"""usage: prog [options]
|
742
|
+
|
743
|
+
-a Add
|
744
|
+
-m <msg> Message
|
745
|
+
|
746
|
+
"""
|
747
|
+
$ prog -a
|
748
|
+
{"-m": null, "-a": true}
|
749
|
+
|
750
|
+
#
|
751
|
+
# Test options without description
|
752
|
+
#
|
753
|
+
|
754
|
+
r"""usage: prog --hello"""
|
755
|
+
$ prog --hello
|
756
|
+
{"--hello": true}
|
757
|
+
|
758
|
+
r"""usage: prog [--hello=<world>]"""
|
759
|
+
$ prog
|
760
|
+
{"--hello": null}
|
761
|
+
|
762
|
+
$ prog --hello wrld
|
763
|
+
{"--hello": "wrld"}
|
764
|
+
|
765
|
+
r"""usage: prog [-o]"""
|
766
|
+
$ prog
|
767
|
+
{"-o": false}
|
768
|
+
|
769
|
+
$ prog -o
|
770
|
+
{"-o": true}
|
771
|
+
|
772
|
+
r"""usage: prog [-opr]"""
|
773
|
+
$ prog -op
|
774
|
+
{"-o": true, "-p": true, "-r": false}
|
775
|
+
|
776
|
+
r"""usage: git [-v | --verbose]"""
|
777
|
+
$ prog -v
|
778
|
+
{"-v": true, "--verbose": false}
|
779
|
+
|
780
|
+
r"""usage: git remote [-v | --verbose]"""
|
781
|
+
$ prog remote -v
|
782
|
+
{"remote": true, "-v": true, "--verbose": false}
|
783
|
+
|
784
|
+
#
|
785
|
+
# Test empty usage pattern
|
786
|
+
#
|
787
|
+
|
788
|
+
r"""usage: prog"""
|
789
|
+
$ prog
|
790
|
+
{}
|
791
|
+
|
792
|
+
r"""usage: prog
|
793
|
+
prog <a> <b>
|
794
|
+
"""
|
795
|
+
$ prog 1 2
|
796
|
+
{"<a>": "1", "<b>": "2"}
|
797
|
+
|
798
|
+
$ prog
|
799
|
+
{"<a>": null, "<b>": null}
|
800
|
+
|
801
|
+
r"""usage: prog <a> <b>
|
802
|
+
prog
|
803
|
+
"""
|
804
|
+
$ prog
|
805
|
+
{"<a>": null, "<b>": null}
|
806
|
+
|
807
|
+
#
|
808
|
+
# Option's argument should not capture default value from usage pattern
|
809
|
+
#
|
810
|
+
|
811
|
+
r"""usage: prog [--file=<f>]"""
|
812
|
+
|
813
|
+
$ prog
|
814
|
+
{"--file": null}
|
815
|
+
|
816
|
+
r"""usage: prog [--file=<f>]
|
817
|
+
|
818
|
+
--file <a>
|
819
|
+
|
820
|
+
"""
|
821
|
+
$ prog
|
822
|
+
{"--file": null}
|
823
|
+
|
824
|
+
r"""Usage: prog [-a <host:port>]
|
825
|
+
|
826
|
+
-a, --address <host:port> TCP address [default: localhost:6283].
|
827
|
+
|
828
|
+
"""
|
829
|
+
$ prog
|
830
|
+
{"--address": "localhost:6283"}
|
831
|
+
|
832
|
+
#
|
833
|
+
# If option with argument could be repeated,
|
834
|
+
# its arguments should be accumulated into a list
|
835
|
+
#
|
836
|
+
|
837
|
+
r"""usage: prog --long=<arg> ..."""
|
838
|
+
|
839
|
+
$ prog --long one
|
840
|
+
{"--long": ["one"]}
|
841
|
+
|
842
|
+
$ prog --long one --long two
|
843
|
+
{"--long": ["one", "two"]}
|
844
|
+
|
845
|
+
#
|
846
|
+
# Test multiple elements repeated at once
|
847
|
+
#
|
848
|
+
|
849
|
+
r"""usage: prog (go <direction> --speed=<km/h>)..."""
|
850
|
+
$ prog go left --speed=5 go right --speed=9
|
851
|
+
{"go": 2, "<direction>": ["left", "right"], "--speed": ["5", "9"]}
|
852
|
+
|
853
|
+
#
|
854
|
+
# Required options should work with option shortcut
|
855
|
+
#
|
856
|
+
|
857
|
+
r"""usage: prog [options] -a
|
858
|
+
|
859
|
+
-a
|
860
|
+
|
861
|
+
"""
|
862
|
+
$ prog -a
|
863
|
+
{"-a": true}
|
864
|
+
|
865
|
+
#
|
866
|
+
# If option could be repeated its defaults should be split into a list
|
867
|
+
#
|
868
|
+
|
869
|
+
r"""usage: prog [-o <o>]...
|
870
|
+
|
871
|
+
-o <o> [default: x]
|
872
|
+
|
873
|
+
"""
|
874
|
+
$ prog -o this -o that
|
875
|
+
{"-o": ["this", "that"]}
|
876
|
+
|
877
|
+
$ prog
|
878
|
+
{"-o": ["x"]}
|
879
|
+
|
880
|
+
r"""usage: prog [-o <o>]...
|
881
|
+
|
882
|
+
-o <o> [default: x y]
|
883
|
+
|
884
|
+
"""
|
885
|
+
$ prog -o this
|
886
|
+
{"-o": ["this"]}
|
887
|
+
|
888
|
+
$ prog
|
889
|
+
{"-o": ["x", "y"]}
|
890
|
+
|
891
|
+
#
|
892
|
+
# Test stacked option's argument
|
893
|
+
#
|
894
|
+
|
895
|
+
r"""usage: prog -pPATH
|
896
|
+
|
897
|
+
-p PATH
|
898
|
+
|
899
|
+
"""
|
900
|
+
$ prog -pHOME
|
901
|
+
{"-p": "HOME"}
|
902
|
+
|
903
|
+
#
|
904
|
+
# Issue 56: Repeated mutually exclusive args give nested lists sometimes
|
905
|
+
#
|
906
|
+
|
907
|
+
r"""Usage: foo (--xx=x|--yy=y)..."""
|
908
|
+
$ prog --xx=1 --yy=2
|
909
|
+
{"--xx": ["1"], "--yy": ["2"]}
|