python decopt:“期望的字符串或缓冲区”

时间:2017-07-02 04:15:07

标签: python python-3.x

我使用decopt模块来处理python脚本选项:

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 <module>  
    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

任何提示?

2 个答案:

答案 0 :(得分:4)

将doc字符串移动到文件的开头(导入行之前)

答案 1 :(得分:1)

对于任何困惑的人,在使用docopt时,您应该在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
"""

,并且应该放置在导入之前。

在项目here的自述文件中查看更多示例