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
|
from copy import copy
import sys
import re
class DocoptLanguageError(Exception):
"""Error in construction of usage-message by developer."""
class DocoptExit(SystemExit):
"""Exit in case user invoked program with incorrect arguments."""
usage = ""
def __init__(self, message=""):
SystemExit.__init__(self, (message + "\n" + self.usage).strip())
class Pattern(object):
def __init__(self, *children):
self.children = list(children)
def __eq__(self, other):
return repr(self) == repr(other)
def __hash__(self):
return hash(repr(self))
def __repr__(self):
return "%s(%s)" % (
self.__class__.__name__,
", ".join(repr(a) for a in self.children),
)
@property
def flat(self):
if not hasattr(self, "children"):
return [self]
return sum([c.flat for c in self.children], [])
def fix(self):
self.fix_identities()
self.fix_list_arguments()
return self
def fix_identities(self, uniq=None):
"""Make pattern-tree tips point to same object if they are equal."""
if not hasattr(self, "children"):
return self
uniq = list(set(self.flat)) if uniq is None else uniq
for i, c in enumerate(self.children):
if not hasattr(c, "children"):
assert c in uniq
self.children[i] = uniq[uniq.index(c)]
else:
c.fix_identities(uniq)
def fix_list_arguments(self):
"""Find arguments that should accumulate values and fix them."""
either = [list(c.children) for c in self.either.children]
for case in either:
case = [c for c in case if case.count(c) > 1]
for a in [e for e in case if type(e) == Argument]:
a.value = []
return self
@property
def either(self):
"""Transform pattern into an equivalent, with only top-level Either."""
# Currently the pattern will not be equivalent, but more "narrow",
# although good enough to reason about list arguments.
if not hasattr(self, "children"):
return Either(Required(self))
else:
ret = []
groups = [[self]]
while groups:
children = groups.pop(0)
types = [type(c) for c in children]
if Either in types:
either = [c for c in children if type(c) is Either][0]
children.pop(children.index(either))
for c in either.children:
groups.append([c] + children)
elif Required in types:
required = [c for c in children if type(c) is Required][0]
children.pop(children.index(required))
groups.append(list(required.children) + children)
elif Optional in types:
optional = [c for c in children if type(c) is Optional][0]
children.pop(children.index(optional))
groups.append(list(optional.children) + children)
elif OneOrMore in types:
oneormore = [c for c in children if type(c) is OneOrMore][0]
children.pop(children.index(oneormore))
groups.append(list(oneormore.children) * 2 + children)
else:
ret.append(children)
return Either(*[Required(*e) for e in ret])
class Argument(Pattern):
def __init__(self, name, value=None):
self.name = name
self.value = value
def match(self, left, collected=None):
collected = [] if collected is None else collected
args = [arg_left for arg_left in left if type(arg_left) is Argument]
if not len(args):
return False, left, collected
left.remove(args[0])
if type(self.value) is not list:
return True, left, collected + [Argument(self.name, args[0].value)]
same_name = [
a for a in collected if type(a) is Argument and a.name == self.name
]
if len(same_name):
same_name[0].value += [args[0].value]
return True, left, collected
else:
return (
True,
left,
collected + [Argument(self.name, [args[0].value])],
)
def __repr__(self):
return "Argument(%r, %r)" % (self.name, self.value)
class Command(Pattern):
def __init__(self, name, value=False):
self.name = name
self.value = value
def match(self, left, collected=None):
collected = [] if collected is None else collected
args = [arg_left for arg_left in left if type(arg_left) is Argument]
if not len(args) or args[0].value != self.name:
return False, left, collected
left.remove(args[0])
return True, left, collected + [Command(self.name, True)]
def __repr__(self):
return "Command(%r, %r)" % (self.name, self.value)
class Option(Pattern):
def __init__(self, short=None, long=None, argcount=0, value=False):
assert argcount in (0, 1)
self.short, self.long = short, long
self.argcount, self.value = argcount, value
self.value = None if not value and argcount else value # HACK
@classmethod
def parse(class_, option_description):
short, long, argcount, value = None, None, 0, False
options, _, description = option_description.strip().partition(" ")
options = options.replace(",", " ").replace("=", " ")
for s in options.split():
if s.startswith("--"):
long = s
elif s.startswith("-"):
short = s
else:
argcount = 1
if argcount:
matched = re.findall(r"\[default: (.*)\]", description, flags=re.I)
value = matched[0] if matched else None
return class_(short, long, argcount, value)
def match(self, left, collected=None):
collected = [] if collected is None else collected
left_ = []
for arg_left in left:
# if this is so greedy, how to handle OneOrMore then?
if not (
type(arg_left) is Option
and (self.short, self.long) == (arg_left.short, arg_left.long)
):
left_.append(arg_left)
return (left != left_), left_, collected
@property
def name(self):
return self.long or self.short
def __repr__(self):
return "Option(%r, %r, %r, %r)" % (
self.short,
self.long,
self.argcount,
self.value,
)
class AnyOptions(Pattern):
def match(self, left, collected=None):
collected = [] if collected is None else collected
left_ = [opt_left for opt_left in left if not type(opt_left) == Option]
return (left != left_), left_, collected
class Required(Pattern):
def match(self, left, collected=None):
collected = [] if collected is None else collected
copied_left = copy(left)
c = copy(collected)
for p in self.children:
matched, copied_left, c = p.match(copied_left, c)
if not matched:
return False, left, collected
return True, copied_left, c
class Optional(Pattern):
def match(self, left, collected=None):
collected = [] if collected is None else collected
left = copy(left)
for p in self.children:
m, left, collected = p.match(left, collected)
return True, left, collected
class OneOrMore(Pattern):
def match(self, left, collected=None):
assert len(self.children) == 1
collected = [] if collected is None else collected
pattern_left = copy(left)
c = copy(collected)
l_ = None
matched = True
times = 0
while matched:
# could it be that something didn't match but
# changed pattern_left or c?
matched, pattern_left, c = self.children[0].match(pattern_left, c)
times += 1 if matched else 0
if l_ == pattern_left:
break
l_ = copy(pattern_left)
if times >= 1:
return True, pattern_left, c
return False, left, collected
class Either(Pattern):
def match(self, left, collected=None):
collected = [] if collected is None else collected
outcomes = []
for p in self.children:
matched, _, _ = outcome = p.match(copy(left), copy(collected))
if matched:
outcomes.append(outcome)
if outcomes:
return min(outcomes, key=lambda outcome: len(outcome[1]))
return False, left, collected
class TokenStream(list):
def __init__(self, source, error):
self += source.split() if type(source) is str else source
self.error = error
def move(self):
return self.pop(0) if len(self) else None
def current(self):
return self[0] if len(self) else None
def parse_long(tokens, options):
raw, eq, value = tokens.move().partition("=")
value = None if eq == value == "" else value
opt = [o for o in options if o.long and o.long.startswith(raw)]
if len(opt) < 1:
if tokens.error is DocoptExit:
raise tokens.error("%s is not recognized" % raw)
else:
o = Option(None, raw, (1 if eq == "=" else 0))
options.append(o)
return [o]
if len(opt) > 1:
raise tokens.error(
"%s is not a unique prefix: %s?"
% (raw, ", ".join("%s" % o.long for o in opt))
)
opt = copy(opt[0])
if opt.argcount == 1:
if value is None:
if tokens.current() is None:
raise tokens.error("%s requires argument" % opt.name)
value = tokens.move()
elif value is not None:
raise tokens.error("%s must not have an argument" % opt.name)
opt.value = value or True
return [opt]
def parse_shorts(tokens, options):
raw = tokens.move()[1:]
parsed = []
while raw != "":
opt = [
o
for o in options
if o.short and o.short.lstrip("-").startswith(raw[0])
]
if len(opt) > 1:
raise tokens.error(
"-%s is specified ambiguously %d times" % (raw[0], len(opt))
)
if len(opt) < 1:
if tokens.error is DocoptExit:
raise tokens.error("-%s is not recognized" % raw[0])
else:
o = Option("-" + raw[0], None)
options.append(o)
parsed.append(o)
raw = raw[1:]
continue
opt = copy(opt[0])
raw = raw[1:]
if opt.argcount == 0:
value = True
else:
if raw == "":
if tokens.current() is None:
raise tokens.error("-%s requires argument" % opt.short[0])
raw = tokens.move()
value, raw = raw, ""
opt.value = value
parsed.append(opt)
return parsed
def parse_pattern(source, options):
tokens = TokenStream(
re.sub(r"([\[\]\(\)\|]|\.\.\.)", r" \1 ", source), DocoptLanguageError
)
result = parse_expr(tokens, options)
if tokens.current() is not None:
raise tokens.error("unexpected ending: %r" % " ".join(tokens))
return Required(*result)
def parse_expr(tokens, options):
"""expr ::= seq ( '|' seq )* ;"""
seq = parse_seq(tokens, options)
if tokens.current() != "|":
return seq
result = [Required(*seq)] if len(seq) > 1 else seq
while tokens.current() == "|":
tokens.move()
seq = parse_seq(tokens, options)
result += [Required(*seq)] if len(seq) > 1 else seq
return [Either(*result)] if len(result) > 1 else result
def parse_seq(tokens, options):
"""seq ::= ( atom [ '...' ] )* ;"""
result = []
while tokens.current() not in [None, "]", ")", "|"]:
atom = parse_atom(tokens, options)
if tokens.current() == "...":
atom = [OneOrMore(*atom)]
tokens.move()
result += atom
return result
def parse_atom(tokens, options):
"""atom ::= '(' expr ')' | '[' expr ']' | 'options'
| long | shorts | argument | command ;
"""
token = tokens.current()
result = []
if token == "(":
tokens.move()
result = [Required(*parse_expr(tokens, options))]
if tokens.move() != ")":
raise tokens.error("Unmatched '('")
return result
elif token == "[":
tokens.move()
result = [Optional(*parse_expr(tokens, options))]
if tokens.move() != "]":
raise tokens.error("Unmatched '['")
return result
elif token == "options":
tokens.move()
return [AnyOptions()]
elif token.startswith("--") and token != "--":
return parse_long(tokens, options)
elif token.startswith("-") and token not in ("-", "--"):
return parse_shorts(tokens, options)
elif token.startswith("<") and token.endswith(">") or token.isupper():
return [Argument(tokens.move())]
else:
return [Command(tokens.move())]
def parse_args(source, options):
tokens = TokenStream(source, DocoptExit)
options = copy(options)
parsed = []
while tokens.current() is not None:
if tokens.current() == "--":
return parsed + [Argument(None, v) for v in tokens]
elif tokens.current().startswith("--"):
parsed += parse_long(tokens, options)
elif tokens.current().startswith("-") and tokens.current() != "-":
parsed += parse_shorts(tokens, options)
else:
parsed.append(Argument(None, tokens.move()))
return parsed
def parse_doc_options(doc):
return [Option.parse("-" + s) for s in re.split("^ *-|\n *-", doc)[1:]]
def printable_usage(doc):
usage_split = re.split(r"([Uu][Ss][Aa][Gg][Ee]:)", doc)
if len(usage_split) < 3:
raise DocoptLanguageError('"usage:" (case-insensitive) not found.')
if len(usage_split) > 3:
raise DocoptLanguageError('More than one "usage:" (case-insensitive).')
return re.split(r"\n\s*\n", "".join(usage_split[1:]))[0].strip()
def formal_usage(printable_usage):
pu = printable_usage.split()[1:] # split and drop "usage:"
return " ".join("|" if s == pu[0] else s for s in pu[1:])
def extras(help, version, options, doc):
if help and any((o.name in ("-h", "--help")) and o.value for o in options):
print(doc.strip())
exit()
if version and any(o.name == "--version" and o.value for o in options):
print(version)
exit()
class Dict(dict):
"""Dictionary with custom repr bbehaviour."""
def __repr__(self):
"""Dictionary representation for docopt."""
return "{%s}" % ",\n ".join("%r: %r" % i for i in sorted(self.items()))
def docopt(doc, argv=sys.argv[1:], help=True, version=None):
DocoptExit.usage = docopt.usage = usage = printable_usage(doc)
pot_options = parse_doc_options(doc)
formal_pattern = parse_pattern(formal_usage(usage), options=pot_options)
argv = parse_args(argv, options=pot_options)
extras(help, version, argv, doc)
matched, left, arguments = formal_pattern.fix().match(argv)
if matched and left == []: # better message if left?
options = [o for o in argv if type(o) is Option]
pot_arguments = [
a for a in formal_pattern.flat if type(a) in [Argument, Command]
]
return Dict(
(a.name, a.value)
for a in (pot_options + options + pot_arguments + arguments)
)
raise DocoptExit()
|