A way to change Click's default help option?

Issue

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?

Solution

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

Answered By – Stephen Rauch

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published