所以我认为使用 python click 库来构建一个简单的命令行工具会非常简单。似乎我被困在了一些不适合我的东西上。我有以下代码:
import click
import json
import os.path
import sys
@click.command()
@click.option('--count', default=1, help='Number of greetings.')
@click.option('--name', prompt='Your name',
help='The person to greet.')
@click.argument('title')
@click.argument('category')
@click.argument('tech')
@click.argument('problemNotes')
@click.argument('solutionNotes')
def hello(count, name, title, category, tech, problemNotes, solutionNotes):
print(tech + problemNotes + solutionNotes)
if __name__ == '__main__':
hello()
这似乎很简单。CL 工具应该接受一些选项和参数,然后打印出来。然后我在终端输入以下内容来调用该工具:
python bugbook.py --name="steve" "Annoying stack bug" "compile bug" "xcode" "annoying" "get meowed"
但我收到以下错误:
Traceback (most recent call last):
File "bugbook.py", line 22, in <module>
hello()
File "/Library/Python/2.7/site-packages/click/core.py", line 722, in __call__
return self.main(*args, **kwargs)
File "/Library/Python/2.7/site-packages/click/core.py", line 697, in main
rv = self.invoke(ctx)
File "/Library/Python/2.7/site-packages/click/core.py", line 895, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/Library/Python/2.7/site-packages/click/core.py", line 535, in invoke
return callback(*args, **kwargs)
TypeError: hello() got an unexpected keyword argument 'solutionnotes'
奇怪,我想。似乎此错误是由以下原因引起的:
if __name__ == '__main__':
hello()
实际上调用主函数的函数无法传入正确的参数或其他东西,但我尝试过修补,但无法使其正常工作。我错过了什么明显的东西?