我正在尝试学习如何使用Python-click。我无法使用其中一个选项的帮助参数,所以我最终放弃并更改了代码以不包含该选项的帮助。但是,尽管关闭并重新启动Python并且现在重新启动计算机,仍然会出现与尝试使用help参数相关的错误消息。
import click
def something():
pass
@click.command()
@click.argument('dest_dir',type=click.Path(exists=True, readable=True,
resolve_path=True, dir_okay=True),
help='Location of directory where results will be saved')
@click.option('--use_terms', is_flag=True,
help='Process strings based on terms or phrases')
@click.option('--use_query', is_flag=True, help='Process string based on
search query')
@click.option('--search_phrase', '-s', multiple=True)
def do_process(dest_dir,use_terms,use_query,*search_phrase):
""" testing setting parameters for snip tables"""
outref = open('e:\\myTemp\\testq.txt')
ms = dest_dir + '\n'
if use_terms:
ms += use_term + '\n'
else:
ms += use_query + '\n'
for each in search_phrase:
x = something()
ms += each + '\n'
outref.writelines(ms)
outref.close()
if __name__ == "__main__":
do_process()
最初是@ click.option我有
@click.option('--search_phrase', '-s', multiple=True, help='The search phrase to use')
我一直收到一条错误消息,我无法解决有关未知参数帮助的问题。我抛弃它,改为上面的内容,现在我得到了类似的错误,
然后我关闭了Python,关闭了我的模块,然后重新启动Python打开并再次运行我的代码并仍然收到此错误消息
Traceback (most recent call last):
File "C:\Program Files\PYTHON\snipTables\test_snip_click.py", line 14, in <module>
@click.option('--search_phrase', '-s', multiple=True)
File "C:\Program Files\PYTHON\lib\site-packages\click\decorators.py", line 148, in decorator
_param_memo(f, ArgumentClass(param_decls, **attrs))
File "C:\Program Files\PYTHON\lib\site-packages\click\core.py", line 1618, in __init__
Parameter.__init__(self, param_decls, required=required, **attrs)
TypeError: __init__() got an unexpected keyword argument 'help'
然后我关闭了Python Idle,我保存并关闭了我的代码,然后重新启动了Python,重新打开了我的代码,但是我仍然得到了相同的回溯,除了注意回溯有我在击败我之后切换到的代码行严厉对抗显示器并放弃
我正准备重新启动,但我对原因非常好奇。
我重新启动,但仍然遇到同样的错误
重命名文件并再次运行并未改变结果 - 相同的追溯
答案 0 :(得分:4)
问题是click不接受带参数参数的帮助字符串。这是有趣的行为。与参数关联的帮助字符串将是处理参数和选项的函数中的字符串。
错误消息将始终显示与最后一个选项相关联。所以这个例子的正确代码是
import click
def something():
pass
@click.command()
@click.argument('dest_dir',type=click.Path(exists=True, readable=True,
resolve_path=True, dir_okay=True)
##Notice no help string here
@click.option('--use_terms', is_flag=True,
help='Process strings based on terms or phrases')
@click.option('--use_query', is_flag=True, help='Process string based on
search query')
@click.option('--search_phrase', '-s', multiple=True)
def do_process(dest_dir,use_terms,use_query,*search_phrase):
""" testing setting parameters for snip tables"""
outref = open('e:\\myTemp\\testq.txt')
ms = dest_dir + '\n'
if use_terms:
ms += use_term + '\n'
else:
ms += use_query + '\n'
for each in search_phrase:
x = something()
ms += each + '\n'
outref.writelines(ms)
outref.close()
if __name__ == "__main__":
do_process()
这很好地解决了我最初遇到的问题是点击没有很好地解释错误的来源。上面,即使我删除了选项中的帮助字符串,点击解析器也会将参数中的帮助字符串与它解析的最后一个选项相关联。
答案 1 :(得分:0)
也许您重命名了源文件,并且您正在运行使用以前名称编译的旧版本?
尝试删除* .pyc文件