1 回答
TA贡献1943条经验 获得超7个赞
您可以通过使用组的自定义类来更改子命令 help 给出的消息。可以通过继承click.Group和更改format_commands()方法来创建自定义类,例如:
自定义类:
class HelpAsArgs(click.Group):
# change the section head of sub commands to "Arguments"
def format_commands(self, ctx, formatter):
rows = []
for subcommand in self.list_commands(ctx):
cmd = self.get_command(ctx, subcommand)
if cmd is None:
continue
help = cmd.short_help or ''
rows.append((subcommand, help))
if rows:
with formatter.section('Arguments'):
formatter.write_dl(rows)
测试代码:
import click
@click.group()
def cli():
pass
@cli.group(cls=HelpAsArgs)
def show():
""" Define the environment of the product """
pass
@show.command()
def name():
click.echo("run show name command")
@show.command()
def height():
click.echo("run show height command")
if __name__ == "__main__":
commands = (
'show',
'show --help',
'--help',
)
import sys, time
time.sleep(1)
print('Click Version: {}'.format(click.__version__))
print('Python Version: {}'.format(sys.version))
for command in commands:
try:
time.sleep(0.1)
print('-----------')
print('> ' + command)
time.sleep(0.1)
cli(command.split())
except BaseException as exc:
if str(exc) != '0' and \
not isinstance(exc, (click.ClickException, SystemExit)):
raise
结果:
Click Version: 6.7
Python Version: 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)]
-----------
> show
Usage: test.py show [OPTIONS] COMMAND [ARGS]...
Define the environment of the product
Options:
--help Show this message and exit.
Arguments:
height
name
-----------
> show --help
Usage: test.py show [OPTIONS] COMMAND [ARGS]...
Define the environment of the product
Options:
--help Show this message and exit.
Arguments:
height
name
-----------
> --help
Usage: test.py [OPTIONS] COMMAND [ARGS]...
Options:
--help Show this message and exit.
Commands:
show Define the environment of the product
添加回答
举报