Cloup

3.0.9 · active · verified Sun Apr 12

Cloup — originally from 'Click + option groups' — enriches the popular Click library with several features that make command-line interfaces more expressive and configurable. These include option groups, constraints (e.g., mutually exclusive parameters), subcommand aliases, subcommand sections, and a themeable help formatter. The library, currently at version 3.0.9, is under active development, statically type-checked with MyPy, and extensively tested.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a command with custom help formatting, option groups, and constraints. It uses `option_group` to logically group related options and applies `mutually_exclusive` and `RequireAtLeast` constraints.

import cloup
from cloup import command, option, option_group, HelpFormatter, HelpTheme, Style
from cloup.constraints import RequireAtLeast, mutually_exclusive

# Configure a custom help theme for better readability
formatter_settings = HelpFormatter.settings(
    theme=HelpTheme(
        invoked_command=Style(fg='bright_yellow'),
        heading=Style(fg='bright_white', bold=True),
        constraint=Style(fg='magenta'),
        col1=Style(fg='bright_yellow'),
    )
)

@command(formatter_settings=formatter_settings)
@option_group(
    "Cool options",
    option('--foo', help='This text describes the option --foo.'),
    option('--bar', help='This text describes the option --bar.'),
    constraint=mutually_exclusive,
)
@option_group(
    "Other cool options",
    "This is an optional description for this option group.",
    option('--pippo', help='This text describes the option --pippo.'),
    option('--pluto', help='This text describes the option --pluto.'),
    constraint=RequireAtLeast(1),
)
@option('--verbose', '-v', is_flag=True, help='Enable verbose output.')
def cli(foo, bar, pippo, pluto, verbose):
    """A simple CLI demonstrating Cloup's features."""
    if verbose:
        cloup.echo('Verbose mode enabled.')
    cloup.echo(f'Foo: {foo}, Bar: {bar}, Pippo: {pippo}, Pluto: {pluto}')

if __name__ == '__main__':
    cli(prog_name='my-app')

view raw JSON →