为什么我使用 click.argument 会产生“得到一个意外的关键字参数 'help'?

Why does my use of click.argument produce "got an unexpected keyword argument 'help'?

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

TypeError: init() got an unexpected keyword argument 'help'

代码:

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'

为什么会出现这个错误?

我运行一次又一次地进入这个,因为跟踪输出与导致错误的参数不对应。请注意跟踪中的 ArgumentClass,这是您的提示。

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

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

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.

您正在将命令定义为参数。请注意,click 有比您在这里尝试的更好的方法来定义命令。

@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 参数。单击文档确实指出您应该记录您自己的论点。但是,我不知道该怎么做。有什么提示吗?

EDIT

As mentioned in the comments: this is to align with the Unix standard to document arguments in the main help text.

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

click library不允许click.arguments里面有-help参数(包括当前版本6.7,当这个评论写完了)。 但是,您可以在 click.options.
中使用 -help 参数 您可以在 http://click.pocoo.org/6/documentation/ or previous at http://click.pocoo.org/5/documentation/ 查看当前的点击文档,此行为最近没有改变。 然后,它是一个 WAD。不是BUG

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