Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python docopt: "expected string or buffer"

I am using docopt module to handle python script options :

from docopt import docopt
"""Usage:
  ./convert [ -h | -i | -t | -c ]

Options:
  -h          Show this help
  -i          Convert image to vertical scroll box
  -t          Convert text to vertical scroll box
  -c          Convert command list to html
"""

def main(docopt_args):
...
if __name__ == '__main__':
    args = docopt(__doc__, version='v0.1')
    main(args)

Traceback (most recent call last): File
"/home/ajn/Converter-yaml-to-html-blocks/convert.py", line 66, in
args = docopt(doc, version='v0.1') File "/usr/local/lib/python3.4/dist-packages/docopt.py", line 558, in docopt
DocoptExit.usage = printable_usage(doc) File "/usr/local/lib/python3.4/dist-packages/docopt.py", line 466, in printable_usage
usage_split = re.split(r'([Uu][Ss][Aa][Gg][Ee]:)', doc) File "/usr/lib/python3.4/re.py", line 196, in split
return _compile(pattern, flags).split(string, maxsplit) TypeError: expected string or buffer

Any hint?

like image 617
AJN Avatar asked Jul 02 '17 04:07

AJN


2 Answers

Move the doc string to the start of the file (before the import line)

like image 88
EarthDragon Avatar answered Sep 23 '22 21:09

EarthDragon


To anyone confused, when using docopt you should write a docstring before the from docopt import docopt. The options for the arguments are generated by parsing that string automatically. In this question the docstring is:

"""Usage:
  ./convert [ -h | -i | -t | -c ]

Options:
  -h          Show this help
  -i          Convert image to vertical scroll box
  -t          Convert text to vertical scroll box
  -c          Convert command list to html
"""

and should have been placed before the import.

See more examples at the README of the project here

like image 22
Pani Avatar answered Sep 21 '22 21:09

Pani