【发布时间】:2019-04-23 20:12:06
【问题描述】:
我正在使用Python 3.6 构建一个click 7.x 应用程序,并且在获取有关子命令的帮助时遇到了一些问题。我有一个必需的全局选项,当我对任何子命令运行帮助时,该选项被报告为缺失。
例如,给定以下虚拟脚本cli.py:
import click
@click.group()
@click.option('--directory', required=True)
def cli(directory):
"""
this is a tool that has an add and remove command
"""
click.echo(directory)
@cli.command()
@click.overwrite('--overwrite', is_flag=True)
def add(overwrite):
"""
this is the add command
"""
click.echo("add overwrite={}".format(overwrite))
@cli.command()
def remove():
"""
this is the remove command
"""
click.echo('remove')
if __name__ == '__main__':
cli()
当我运行以下命令时:
python cli.py --help
我得到了所需的输出:
Usage cli.py [OPTIONS] COMMAND [ARGS]...
this is a tool that has an add and remove command
Options:
--directory TEXT [required]
--help Show this message and exit.
Commands:
add this is the add command
remove this is the remove command
但是如果我运行这个:
python cli.py add --help
我收到以下错误:
Usage cli.py [OPTIONS] COMMAND [ARGS]...
Try "cli.py --help" for help.
Error: Missing option "--directory"
如何在不提供--directory 选项的情况下获得显示添加命令的帮助?
【问题讨论】:
标签: python command-line-interface python-click