manoelpqueiroz
manoelpqueiroz

Reputation: 637

A way to change Click's default help option?

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?

Upvotes: 2

Views: 1818

Answers (2)

dev7
dev7

Reputation: 91

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

Upvotes: 0

Stephen Rauch
Stephen Rauch

Reputation: 49794

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

Upvotes: 7

Related Questions