Back to snippets

click_option_group_decorator_for_grouped_help_output.py

python

This example demonstrates how to group related options together in th

Agent Votes
1
0
100% positive
click_option_group_decorator_for_grouped_help_output.py
1import click
2from click_option_group import optgroup, RequiredMutuallyExclusiveOptionGroup
3
4@click.command()
5@optgroup.group('Server configuration', help='The configuration of the server host and port')
6@optgroup.option('--host', default='localhost', help='The host address')
7@optgroup.option('--port', default=8080, help='The port number')
8@optgroup.group('Output configuration', help='The configuration of the output format')
9@optgroup.option('--format', type=click.Choice(['json', 'xml']), default='json', help='The output format')
10@optgroup.option('--verbose', is_flag=True, help='Enable verbose output')
11def cli(host, port, format, verbose):
12    """A simple CLI with option groups."""
13    click.echo(f"Host: {host}")
14    click.echo(f"Port: {port}")
15    click.echo(f"Format: {format}")
16    click.echo(f"Verbose: {verbose}")
17
18if __name__ == '__main__':
19    cli()