docopt fails with docopt.DocoptLanguageError: unmatched '['

docopt fails with docopt.DocoptLanguageError: unmatched '['

为什么此代码会失败并出现以下异常?

"""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.103.5.1

它失败了,因为在这一行的 --foo 之后只有一个 space:

    --foo - this will do foo

通过在 --foo 之后添加另一个 space 来修复它:

    --foo  - this will do foo



the documentation:

Use two spaces to separate options with their informal description:

--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