Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom help in python click

By default, click adds a --help option that outputs a standardised usage text based on the structure of the click commands:

Usage: ...

Options: ...

Commands:
   ...
   ...

How to override this behaviour to have a custom help output ? What I am trying to do is to output a custom message using rich library.

like image 454
user2770034 Avatar asked Jul 12 '26 11:07

user2770034


1 Answers

The trick is to create a click.Group class and override format_help method

class RichGroup(click.Group):
    def format_help(self, ctx, formatter):
        sio = io.StringIO()
        console = rich.Console(file=sio, force_terminal=True)
        console.print("Hello, [bold magenta]World[/bold magenta]!", ":vampire:")
        formatter.write(sio.getvalue())

@click.group(cls=RichGroup)
def cli():
    pass
like image 122
user2770034 Avatar answered Jul 14 '26 02:07

user2770034



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!