Back to snippets
click_option_group_basic_example_with_mutually_exclusive_options.py
pythonA basic example of how to group options and add help text to those gr
Agent Votes
1
0
100% positive
click_option_group_basic_example_with_mutually_exclusive_options.py
1import click
2from click_option_group import optgroup, RequiredMutuallyExclusiveOptionGroup
3
4@click.command()
5@optgroup.group('Server configuration', help='The configuration for the server endpoint')
6@optgroup.option('--host', default='localhost', help='The host address')
7@optgroup.option('--port', default=8080, help='The port number')
8@optgroup.group('Output selection', cls=RequiredMutuallyExclusiveOptionGroup, help='The output format')
9@optgroup.option('--json', is_flag=True, help='Output in JSON format')
10@optgroup.option('--csv', is_flag=True, help='Output in CSV format')
11def cli(host, port, json, csv):
12 """A simple CLI with option groups."""
13 click.echo(f"Host: {host}, Port: {port}")
14 if json:
15 click.echo("Format: JSON")
16 elif csv:
17 click.echo("Format: CSV")
18
19if __name__ == '__main__':
20 cli()