7

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?

2
  • 1
    I suspect that the __doc__ you're passing is None, which is neither a string nor a buffer. (Remember: "Explicit is better than implicit".) Commented Jul 2, 2017 at 4:19
  • what was the solution for this ? Commented Mar 18, 2020 at 7:56

2 Answers 2

16

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

Sign up to request clarification or add additional context in comments.

2 Comments

I didnt understand this...can anyone reply to this ?
the docstring is the string literal after the import. I added a new answer explaining it a bit
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.