Python 单击与选项关联的帮助字符串中的错误

Python Click Error from help string associated with an option

我正在努力学习如何使用 Python-click。我无法将帮助参数与我的一个选项一起使用,所以我最终放弃并更改了代码以不包含该选项的帮助。然而,尽管关闭并重新启动 Python 并且现在重新启动我的计算机,与尝试使用帮助参数相关的错误消息仍然出现。

代码:

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 空闲,我保存并关闭了我的代码然后重新启动 Python,重新打开了我的代码,但是我仍然得到相同的回溯,只是注意到回溯有在显示器上用力敲打并放弃后我切换到的代码行

我正准备重启,但我真的很好奇原因。

我重新启动了,但仍然出现同样的错误

重命名文件并再次 运行 没有改变结果 - 相同的追溯

也许您重命名了源文件并且您是运行使用以前的名称编译的旧版本?

尝试删除 *.pyc 文件

问题是 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()

这运行良好,我最初遇到的问题是 click 没有很好地解释错误的来源。在上面,即使我去掉了选项中的帮助字符串,点击解析器也会将参数中的帮助字符串与它解析的最后一个选项相关联。