AJN
AJN

Reputation: 1206

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?

Upvotes: 7

Views: 2633

Answers (2)

Pani
Pani

Reputation: 1377

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

Upvotes: 1

EarthDragon
EarthDragon

Reputation: 755

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

Upvotes: 16

Related Questions