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



我正在学习如何使用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。选项I had

@click.option('--search_phrase', '-s', multiple=True, help='The search phrase to use')

我一直得到一个错误消息,我无法解决有关有一个未知参数帮助。我放弃了它,改变了上面的内容,现在我得到了一个类似的错误,

然后我关闭了Python,我关闭了我的模块,然后重新启动Python打开并再次运行我的代码,仍然得到这个错误信息

回溯:

Traceback (most recent call last):
 File "C:Program FilesPYTHONsnipTablestest_snip_click.py", line 14, in <module>
@click.option('--search_phrase', '-s', multiple=True)
File "C:Program FilesPYTHONlibsite-packagesclickdecorators.py", line 148, in decorator
_param_memo(f, ArgumentClass(param_decls, **attrs))
 File "C:Program FilesPYTHONlibsite-packagesclickcore.py", line 1618, in __init__
Parameter.__init__(self, param_decls, required=required, **attrs)
TypeError: __init__() got an unexpected keyword argument 'help'

所以然后我关闭了Python Idle,我保存并关闭了我的代码,然后重新启动Python,重新打开我的代码,但我仍然得到相同的回溯,除了注意到回溯有我切换到的代码行在我的头重重地撞在监视器上并放弃

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

我重新启动,仍然得到相同的错误

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

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

这运行良好的问题,我原来是点击没有做一个很好的工作来解释错误的来源。上面,即使我去掉了选项中的帮助字符串,点击解析器还是会将参数中的帮助字符串与它解析的最后一个选项关联起来。

也许您重命名了源文件,并且您正在运行使用前一个名称编译的旧版本?

尝试删除*。佩克文件

最新更新