我是第一次在Python 3中使用docopt . 当我运行代码时,输出只显示Usage选项数据,并且不执行模块中的功能 .
这是一个例子 . 我在一个文件中有这个代码 .
"""Usage: scratch.py [-h] [--boston | --sandiego] [--prostitution |
--drugs] [--lim VALUE]
Options:
-h --help :Show help information
--boston : work on boston addresses
--sandiego : work on san diego addresses
--prostitution : prostitution incidents
--drugs : drug sale incidents
--lim : number of addresses to work with
"""
from docopt import docopt
def scratch(args):
print(args['--boston'])
print('hello')
if __name__ == '__main__':
arg = docopt(__doc__, help=False)
print(arg)
scratch(arg)
当我使用各种选项运行此文件时,我得到的只是文档字符串的副本 . 我应该看到docopt从docstring参数创建的字典的副本 . 然后我还应该看到函数调用的打印结果 . 但除了文档之外,我什么都看不到 .
所以,如果我进行如下命令行调用:
python scratch.py --boston --drugs --lim 100
返回的所有内容如下 .
Usage: scratch.py <city> [--boston | --sandiego] <crime> [--prostitution |
--drugs] --count=N
Options:
city : city to geocode
--boston : work on boston addresses
--sandiego : work on san diego addresses
crime : crime to select on
--prostitution : prostitution incidents
--drugs : drug sale incidents
-count : number of addresses to work with
我想知道什么是错的,这样我才能真正得到定义的函数来运行 .
1 回答
这个答案的功劳属于J.F. Sebastian, above;我只是想确保我结束了这个问题 .
文档字符串的
Usage:
和Options:
部分之间需要有一个空行,否则docopt
会给出结果,就像我上面的问题一样 . documentation中提到了这个要求,但可能很难捕捉到(强调我的):