问题 运行 单击命令组

Problems running click command group

我在使用命令组时遇到问题。我一直在关注 this guide.

#!/usr/bin/env python

import click


@click.group()
@click.option("--template-id", prompt="Template ID", help="The template to use.")
@click.option("--lang", prompt="Langcode", help="The language to use.")
def cli(template_id, lang):
    pass


@cli.command()
@click.argument('template-id')
@click.argument('lang')
def upload_translations(template_id, lang):
    pass


if __name__ == "__main__":
    cli()

运行 这会导致问题:

» ~/cli.py upload_translations --template-id=xxxxx --lang=ja 
Template ID: sdf
Langcode: asdf
Error: no such option: --template-id
  1. 为什么点击请求选项?我已经在命令行上传递了它!
  2. 为什么会出现错误:no such option: --template-id

--template-id 选项不是 upload_translations 命令的选项;它是基础 cli 的一个选项。所以你会这样称呼它:

./cli.py --template-id=xxxxxx --lang=ja upload_translations ...

此外,您在 cliupload_translations 上都有一个 --lang 选项。这意味着这也是有效的:

./cli.py --template-id=xxxxxx upload_translations --lang=ja ...

这有点令人困惑;您可能想要从一个或另一个中删除 --lang 选项,或者如果它实际上不是同一件事,则在这两个命令之一中给它一个不同的名称。