Search code examples
pycharmpython-3.7docopt

python3 docopt throws usage error while using pyCharm


I need some basic help on docopt with python3.7 .I am using in pyCharm. I tried to let run the example code which is shown on the docopt.org website. But the system is throwing usage errors. I installed the doctop through pip install docopt an have version 0.6.2 . I found a thread with the same error messages, but the user didn't have a docstring in it, so that is why it didn't work for him.

Here is the code:

#!/usr/bin/python
# -*- coding: utf-8 -*-

"""Naval fate.

Usage:
  naval_fate.py  ship new <name>...
  naval_fate.py  ship <name> move <x> <y> [ --speed=<kn>]
  naval_fate.py  ship shoot <x> <y>
  naval_fate.py  mine (set|remove) <x> <y> [--moored|--drifting}
  naval_fate.py  -h| --help
  naval_fate.py  --version

Options:
  -h --help     Show this screen.
  --version     Show version.  
  --speed=<kn>  Speed in knots [default: 10].
  -- moored     Moored (anchored) mine.
  -- drifting   Drifting mine.

"""
from docopt import docopt

if __name__ == '__main__':
    arguments = docopt(__doc__, version='1')
    print(arguments)

Here is ther ERROR:

...\Scripts\python.exe 
.../naval_fate.py
Traceback (most recent call last):
  File 
".../naval_fate.py", line 27, in <module>
    arguments = docopt(__doc__, version='1')
  File "...\lib\site-packages\docopt.py", line 558, in docopt
    DocoptExit.usage = printable_usage(doc)
  File "...\lib\site-packages\docopt.py", line 466, in printable_usage
    usage_split = re.split(r'([Uu][Ss][Aa][Gg][Ee]:)', doc)
  File "C:\Python\Python37\Lib\re.py", line 213, in split
    return _compile(pattern, flags).split(string, maxsplit)
TypeError: expected string or bytes-like object

Process finished with exit code 1

What I tried:

  • deleted the file extensions of naval_fate.py.
  • uninstalled and reinstalled docopt.
  • changed the positions of the words in and after the usage and options sections.
  • used two spaces between the options and the text (as found in another thread)
  • tried to format outside the docstring in diffrent ways.

e.g

from docopt
...
arguments = docopt.docopt(__doc__, version='1')

instead of

from docopt import docopt
...
arguments = docopt(__doc__, version='1')

But it all didn't help. Does anybody have an idea? Thanks so far.


Solution

  • Well, I found it! It was the curly braces at the end of the line in the 'Usage' part.

    naval_fate.py  mine (set|remove) <x> <y> [--moored|--drifting}
    

    After changing it to ] everything worked well.
    Hopefully I can help someone else with this little(big) mistake.