pythondocopt

Why does docopt fail with docopt.DocoptLanguageError: unmatched '['?


Why does this code fail with the following exception?

"""my_program - for doing awesome stuff

Usage: my_program [--foo]

Options:
    --foo - this will do foo
"""

import docopt
args = docopt.docopt(doc=__doc__)

Exception:

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 version: 0.6.2
python versions: 2.7.10, 3.5.1


Solution

  • It fails because of only a single space after --foo on this line:

        --foo - this will do foo
    

    Fix it by adding another space after --foo:

        --foo  - this will do foo
    



    Per 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
    



    Additional reference: docopt source code