Search code examples
pythonpython-click

Does click lib provide a way to print the builtin help message?


I am using the click lib.

In my code , sometimes I want to print help msg , But the only way I know is :

python xxx --help

But I want to print the help msg in my code using a certain function , for example:

click.print_help_msg()

Is there a function like this ?


Solution

  • You can use Command's get_help method

    import click
    
    @click.command()
    @click.option('--name', help='The person to greet.')
    def hello(name):
        """Simple program that greets NAME."""
        click.echo('Hello %s!' % name)
    
    def print_help_msg(command):
        with click.Context(command) as ctx:
            click.echo(command.get_help(ctx))
    
    >> print_help_msg(hello)