HelpFormatter 在点击

HelpFormatter in Click

我在本地模块中使用 click,我想调整帮助的显示方式:

当前输出为--help:

Usage: __main__.py [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  foo     Foo is a program very nice and pretty...

默认情况下,prog 名称为 __main__.py,文本被裁剪为 78 个字符。

我发现可以使用 HelpFormatter class 进行调整。但我不知道如何在这种情况下使用它。

当前代码:

import click

@click.group()
def main(ctx):
   pass

@main.command()
def foo():
   pass

click.CommandCollection(sources=[main])()

预期输出:

Usage: my_module_name [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  foo     Foo is a program very nice and pretty and this sentence is very long.

如果您试图避免截断帮助字符串,可以通过 short_help 参数来实现。 short_help 通常源自 help 但被截断了。如果显式传递,将显示整个字符串。

显示字符串my_module_name,可以在参数prog_name

下传递

测试代码:

import click

@click.group()
def main(ctx):
   pass

@main.command(short_help='Foo is a program very nice and pretty and '
                         'this sentence is very long.')
def foo():
   pass

main(['--help'], prog_name='my_module_name')

short_help的结果:

Usage: my_module_name [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  foo  Foo is a program very nice and pretty and this sentence is very long.