Python 单击组:如何为所有命令设置 -h/--help
Python click group: How to have -h/--help for all commands
上下文:
我有几个带有大量子命令的脚本,我想将它们转换为使用 click
目前所有这些命令都接受 -h
和 --help
以显示帮助选项。我想保持这种行为。
问题:
点击接受默认--help
显示帮助文本,但不-h
对于单击命令,可以通过添加轻松更改。
@click.group()
@click.help_option("--help", "-h")
def cli():
""" the doc string """
enter code here
@cli.command()
@click.help_option("--help", "-h")
def mycommand()
pass
@cli.command()
@click.help_option("--help", "-h")
def mycommand1()
pass
...
但是,如果我有数十个命令,我必须重新应用装饰器行
@click.help_option("--help", "-h")
每个子命令。
有什么技巧可以避免到处都写这一行吗?
您需要定义一个 CONTEXT_SETTINGS 并像这样使用它:
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
@click.command(context_settings=CONTEXT_SETTINGS)
def cli():
pass
来自点击documentation:
Help Parameter Customization Changelog The help parameter is
implemented in Click in a very special manner. Unlike regular
parameters it’s automatically added by Click for any command and it
performs automatic conflict resolution. By default it’s called --help,
but this can be changed. If a command itself implements a parameter
with the same name, the default help parameter stops accepting it.
There is a context setting that can be used to override the names of
the help parameters called help_option_names.
This example changes the default parameters to -h and --help instead
of just --help:
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
@click.command(context_settings=CONTEXT_SETTINGS) def cli():
pass And what it looks like:
$ cli -h Usage: cli [OPTIONS]
Options: -h, --help Show this message and exit.
上下文:
我有几个带有大量子命令的脚本,我想将它们转换为使用 click
目前所有这些命令都接受 -h
和 --help
以显示帮助选项。我想保持这种行为。
问题:
点击接受默认--help
显示帮助文本,但不-h
对于单击命令,可以通过添加轻松更改。
@click.group()
@click.help_option("--help", "-h")
def cli():
""" the doc string """
enter code here
@cli.command()
@click.help_option("--help", "-h")
def mycommand()
pass
@cli.command()
@click.help_option("--help", "-h")
def mycommand1()
pass
...
但是,如果我有数十个命令,我必须重新应用装饰器行
@click.help_option("--help", "-h")
每个子命令。
有什么技巧可以避免到处都写这一行吗?
您需要定义一个 CONTEXT_SETTINGS 并像这样使用它:
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
@click.command(context_settings=CONTEXT_SETTINGS)
def cli():
pass
来自点击documentation:
Help Parameter Customization Changelog The help parameter is implemented in Click in a very special manner. Unlike regular parameters it’s automatically added by Click for any command and it performs automatic conflict resolution. By default it’s called --help, but this can be changed. If a command itself implements a parameter with the same name, the default help parameter stops accepting it. There is a context setting that can be used to override the names of the help parameters called help_option_names.
This example changes the default parameters to -h and --help instead of just --help:
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
@click.command(context_settings=CONTEXT_SETTINGS) def cli(): pass And what it looks like:
$ cli -h Usage: cli [OPTIONS]
Options: -h, --help Show this message and exit.