31

运行以下代码会导致此错误:

TypeError:init()得到了一个意外的关键字参数“帮助”

代码:

import click

@click.command()
@click.argument('command', required=1, help="start|stop|restart")
@click.option('--debug/--no-debug', default=False, help="Run in foreground")
def main(command, debug):
    print (command)
    print (debug)

if __name__ == '__main__':
    main()

完整的错误输出:

$ python3 foo.py start
Traceback (most recent call last):
  File "foo.py", line 5, in <module>
    @click.option('--debug/--no-debug', default=False, help="Run in foreground")
  File "/home/cbetti/python/lib/python3/dist-packages/click-4.0-py3.4.egg/click/decorators.py", line 148, in decorator
    _param_memo(f, ArgumentClass(param_decls, **attrs))
  File "/home/cbetti/python/lib/python3/dist-packages/click-4.0-py3.4.egg/click/core.py", line 1618, in __init__
    Parameter.__init__(self, param_decls, required=required, **attrs)
TypeError: __init__() got an unexpected keyword argument 'help'

为什么会出现这个错误?

4

5 回答 5

43

我一次又一次地遇到这个问题,因为跟踪输出与导致错误的参数不对应。注意ArgumentClass跟踪,这是你的提示。

'help' 是一个可接受的参数@click.option。但是,单击库更喜欢您记录自己的论点。该@click.argument help参数导致此异常。

此代码有效:(注意缺少, help="start|stop|restart"in @click.argument

import click

@click.command()
@click.argument('command', required=1)
@click.option('--debug/--no-debug', default=False, help="Run in foreground")
def main(command, debug):
    """ COMMAND: start|stop|restart """
    print (command)
    print (debug)

if __name__ == '__main__':
        main()

输出:

$ python3 foo.py start
start
False

帮助输出:

Usage: test.py [OPTIONS] COMMAND

  COMMAND: start|stop|restart

Options:
  --debug / --no-debug  Run in foreground
  --help                Show this message and exit.
于 2015-07-01T23:30:49.777 回答
6

您将命令定义为参数。请注意,单击比您在此处尝试的定义命令有更好的方法。

@click.group()
def main():
    pass

@main.command()
def start():
    """documentation for the start command"""
    print("running command `start`")

@main.command()
def stop():
    """documentation for the stop command"""
    print("running command `stop`")

if __name__ == '__main__':
    main()

将产生以下默认帮助文本:

Usage: test_cli.py [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  start  documentation for the start command
  stop   documentation for the stop command

话虽如此,如果您真的需要参数,则不能使用help参数。点击文档确实声明您应该记录自己的论点。但是,我不知道该怎么做。有什么提示吗?

编辑

正如评论中提到的:这是为了与 Unix 标准保持一致,以便在主要帮助文本中记录参数。

于 2015-08-17T13:33:44.040 回答
3

click 库不允许-help内部参数click.arguments(包括当前版本6.7时已写入此注释)。但是,你可以使用-help里面的参数click.options您可以在http://click.pocoo.org/6/documentation/或以前在http://click.pocoo.org/5/documentation/
查看当前点击文档,此行为最近没有改变。然后,它是一个WAD。这不是一个BUG。

于 2018-05-28T15:39:42.287 回答
1

对我来说,这是因为我的变量看起来像DnsCryptExractDir,我也必须更改它,dnscryptexractdir因为 *args 找不到它。

于 2018-01-29T08:19:42.183 回答
1

对于同样的错误,我得到了它,因为我的参数名称是 url_Hook (camelCase)。在我将其更改为 url_hook 后,它得到了解决。

于 2018-09-18T22:19:31.220 回答