Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if len(usage_sections) == 0:
raise DocoptLanguageError('"usage:" (case-insensitive) not found.')
if len(usage_sections) > 1:
raise DocoptLanguageError('More than one "usage:" (case-insensitive).')
DocoptExit.usage = usage_sections[0]
options = parse_defaults(doc)
pattern = parse_pattern(formal_usage(DocoptExit.usage), options)
# [default] syntax for argument is disabled
#for a in pattern.flat(Argument):
# same_name = [d for d in arguments if d.name == a.name]
# if same_name:
# a.value = same_name[0].value
argv = parse_argv(Tokens(argv), list(options), options_first)
pattern_options = set(pattern.flat(Option))
for options_shortcut in pattern.flat(OptionsShortcut):
doc_options = parse_defaults(doc)
options_shortcut.children = list(set(doc_options) - pattern_options)
#if any_options:
# options_shortcut.children += [Option(o.short, o.long, o.argcount)
# for o in argv if type(o) is Option]
extras(help, version, argv, doc)
matched, left, collected = pattern.fix().match(argv)
if matched and left == []: # better error message if left?
return Dict((a.name, a.value) for a in (pattern.flat() + collected))
raise DocoptExit()
def transform(pattern):
"""Expand pattern into an (almost) equivalent one, but with single Either.
Example: ((-a | -b) (-c | -d)) => (-a -c | -a -d | -b -c | -b -d)
Quirks: [-a] => (-a), (-a...) => (-a -a)
"""
result = []
groups = [[pattern]]
while groups:
children = groups.pop(0)
parents = [Required, Optional, OptionsShortcut, Either, OneOrMore]
if any(t in map(type, children) for t in parents):
child = [c for c in children if type(c) in parents][0]
children.remove(child)
if type(child) is Either:
for c in child.children:
groups.append([c] + children)
elif type(child) is OneOrMore:
groups.append(child.children * 2 + children)
else:
groups.append(child.children + children)
else:
result.append(children)
return Either(*[Required(*e) for e in result])
def parse_atom(tokens, options):
"""atom ::= '(' expr ')' | '[' expr ']' | 'options'
| long | shorts | argument | command ;
"""
token = tokens.current()
result = []
if token in '([':
tokens.move()
matching, pattern = {'(': [')', Required], '[': [']', Optional]}[token]
result = pattern(*parse_expr(tokens, options))
if tokens.move() != matching:
raise tokens.error("unmatched '%s'" % token)
return [result]
elif token == 'options':
tokens.move()
return [OptionsShortcut()]
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 docoptToArgumentParser(self, docopt_str):
# initial doc validation
extc_dict = dcpt.docopt(docopt_str)
usage_sections = dcpt.parse_section('usage:', docopt_str)
options = dcpt.parse_defaults(docopt_str)
pattern = dcpt.parse_pattern(
dcpt.formal_usage(usage_sections[0]), options)
argv = dcpt.parse_argv(dcpt.Tokens(sys.argv[1:]), list(options), False)
pattern_options = set(pattern.flat(dcpt.Option))
for options_shortcut in pattern.flat(dcpt.OptionsShortcut):
doc_options = dcpt.parse_defaults(docopt_str)
options_shortcut.children = list(set(doc_options) - pattern_options)
matched, left, collected = pattern.fix().match(argv)
# can loop through to compare extracted params with extc_dict
# and add param to argparser for each prm
for prm in pattern.flat() + collected:
print(prm)
}
self.docopt_str = docopt_str
self.dependencies = {}
self.all_desc_and_type = {}
self.unique_ids = {}
options = dcpt.parse_defaults(docopt_str)
self.pattern = dcpt.parse_pattern(
dcpt.formal_usage(dcpt.parse_section('usage:', docopt_str)[0]),
options)
argv = dcpt.parse_argv(dcpt.Tokens(sys.argv[1:]), list(options), False)
pattern_options = set(self.pattern.flat(dcpt.Option))
for options_shortcut in self.pattern.flat(dcpt.OptionsShortcut):
doc_options = dcpt.parse_defaults(docopt_str)
options_shortcut.children = list(set(doc_options) - pattern_options)
matched, left, collected = self.pattern.fix().match(argv)
def parse_atom(tokens, options):
"""atom ::= '(' expr ')' | '[' expr ']' | 'options'
| long | shorts | argument | command ;
"""
token = tokens.current()
result = []
if token in '([':
tokens.move()
matching, pattern = {'(': [')', Required], '[': [']', Optional]}[token]
result = pattern(*parse_expr(tokens, options))
if tokens.move() != matching:
raise tokens.error("unmatched '%s'" % token)
return [result]
elif token == 'options':
tokens.move()
return [OptionsShortcut()]
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())]