一种更改单击的方法默认帮助选项?

发布于 2025-02-06 09:31:02 字数 977 浏览 1 评论 0原文

每当您使用单击创建命令或组时,都会有一个默认- help选项以携带用法指南:

import click

@click.command()
def main():
    click.echo('Success!')

if __name__ == '__main__':
    main()

如果我使用- help help运行文件,我应该get:

$ python file.py --help
Usage: file.py [OPTIONS]

Options:
--help  Show this message and exit.

现在,单击允许您通过装饰器中的参数通过终端调用help> help选项的覆盖方式:

@click.command(
    context_settings=dict(
        help_option_names=['-f', '--foo']
    )
)
def main():
    click.echo('Success!')
$ python file.py -f
Usage: file.py [OPTIONS]

Options:
-f, --foo  Show this message and exit.

但是,通过click


是否有一个参数可以指定到click.command覆盖文本“显示此消息并退出” 在终端寻求帮助时?

Whenever you create a command or group with Click, there's a default --help option to bring the usage guide:

import click

@click.command()
def main():
    click.echo('Success!')

if __name__ == '__main__':
    main()

If I run the file with the --help, I should get:

$ python file.py --help
Usage: file.py [OPTIONS]

Options:
--help  Show this message and exit.

Now, Click allows you to override how the help option is called through the terminal via a parameter in the decorator:

@click.command(
    context_settings=dict(
        help_option_names=['-f', '--foo']
    )
)
def main():
    click.echo('Success!')
$ python file.py -f
Usage: file.py [OPTIONS]

Options:
-f, --foo  Show this message and exit.

However, rummaging through Click's documentation, I don't see a similar option to override the default help message.


Is there a parameter to specify to click.command that overrides the text "Show this message and exit" when calling for help in the terminal?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泼猴你往哪里跑 2025-02-13 09:31:05

为了更改帮助文本,仅单行 装饰器已经足够了

@click.help_option('--help', help='Show my better message and exit')

,您可以按照完整的 @stephen Rauch 的详细调整,

For changing help text only a one-line help_option decorator is sufficient

@click.help_option('--help', help='Show my better message and exit')

Alternatively you can follow the full @Stephen Rauch's answer to tweak it in details

讽刺将军 2025-02-13 09:31:04

您可以使用 help_option 装饰器

示例代码:

@click.command(add_help_option=False)
@click.help_option('--foo', '-f', help='Show my better message and exit')
def main():
    """The docstring is the Help Message"""
    click.echo('Success!')

测试代码:

if __name__ == "__main__":
    print('Click Version: {}'.format(click.__version__))
    print('Python Version: {}'.format(sys.version))
    print('-----------')
    cmd = 'main --foo'
    print('> ' + cmd)
    main(cmd.split())

测试结果:

Click Version: 8.1.3
Python Version: 3.7.8 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)]
-----------
> main --foo
Usage: test_code.py [OPTIONS]

  The docstring is the Help Message

Options:
  -f, --foo  Show my better message and exit

You can change click's default help option with the help_option decorator

Sample Code:

@click.command(add_help_option=False)
@click.help_option('--foo', '-f', help='Show my better message and exit')
def main():
    """The docstring is the Help Message"""
    click.echo('Success!')

Test Code:

if __name__ == "__main__":
    print('Click Version: {}'.format(click.__version__))
    print('Python Version: {}'.format(sys.version))
    print('-----------')
    cmd = 'main --foo'
    print('> ' + cmd)
    main(cmd.split())

Test Results:

Click Version: 8.1.3
Python Version: 3.7.8 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)]
-----------
> main --foo
Usage: test_code.py [OPTIONS]

  The docstring is the Help Message

Options:
  -f, --foo  Show my better message and exit
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文