为什么此代码会因以下异常而失败?
"""my_program - for doing awesome stuff
Usage: my_program [--foo]
Options:
--foo - this will do foo
"""
import docopt
args = docopt.docopt(doc=__doc__)
例外:
Traceback (most recent call last):
File "/tmp/post.py", line 10, in <module>
args = docopt.docopt(doc=__doc__)
File "/Users/rbednark/.virtualenvs/docopt-python2/lib/python3.5/site-packages/docopt.py", line 560, in docopt
pattern = parse_pattern(formal_usage(DocoptExit.usage), options)
File "/Users/rbednark/.virtualenvs/docopt-python2/lib/python3.5/site-packages/docopt.py", line 373, in parse_pattern
result = parse_expr(tokens, options)
File "/Users/rbednark/.virtualenvs/docopt-python2/lib/python3.5/site-packages/docopt.py", line 381, in parse_expr
seq = parse_seq(tokens, options)
File "/Users/rbednark/.virtualenvs/docopt-python2/lib/python3.5/site-packages/docopt.py", line 396, in parse_seq
atom = parse_atom(tokens, options)
File "/Users/rbednark/.virtualenvs/docopt-python2/lib/python3.5/site-packages/docopt.py", line 413, in parse_atom
result = pattern(*parse_expr(tokens, options))
File "/Users/rbednark/.virtualenvs/docopt-python2/lib/python3.5/site-packages/docopt.py", line 381, in parse_expr
seq = parse_seq(tokens, options)
File "/Users/rbednark/.virtualenvs/docopt-python2/lib/python3.5/site-packages/docopt.py", line 396, in parse_seq
atom = parse_atom(tokens, options)
File "/Users/rbednark/.virtualenvs/docopt-python2/lib/python3.5/site-packages/docopt.py", line 415, in parse_atom
raise tokens.error("unmatched '%s'" % token)
docopt.DocoptLanguageError: unmatched '['
docopt版本:0.6.2
python版本:2.7.10
,3.5.1
答案 0 :(得分:6)
由于此行--foo
之后只有一个空格,因此失败了:
--foo - this will do foo
通过在--foo
之后添加另一个空格来修复它:
--foo - this will do foo
使用两个空格将选项与其非正式描述分开:
--verbose More text. # BAD, will be treated as if verbose option had
# an argument "More", so use 2 spaces instead
-q Quit. # GOOD
-o FILE Output file. # GOOD
--stdout Use stdout. # GOOD, 2 spaces
其他参考:docopt source code