Skip to main content

Composable Click callback utilities for building flexible CLI applications.

Project description

Composable Click callback utilities for building flexible CLI applications.

Installation

$ pip install click-compose

Or with uv:

$ uv add click-compose

Quick Start

click-compose provides two main utilities for composing Click callbacks:

multi_callback

Combine multiple callbacks into a single callback that applies them in sequence:

"""Example of using multi_callback to combine validators."""

import click

from click_compose import multi_callback


def validate_positive(
    _ctx: click.Context,
    _param: click.Parameter,
    value: int,
) -> int:
    """Validate that value is positive."""
    if value <= 0:
        msg = "Must be positive"
        raise click.BadParameter(message=msg)
    return value


MAX_VALUE = 100


def validate_max_100(
    _ctx: click.Context,
    _param: click.Parameter,
    value: int,
) -> int:
    """Validate that value is at most 100."""
    if value > MAX_VALUE:
        msg = "Must be <= 100"
        raise click.BadParameter(message=msg)
    return value


@click.command()
@click.option(
    "--count",
    type=int,
    callback=multi_callback(callbacks=[validate_positive, validate_max_100]),
)
def cmd(count: int) -> None:
    """Example command with multiple validators."""
    click.echo(message=f"Count: {count}")

sequence_validator

Apply a validator to each element in a sequence (useful with multiple=True):

"""Example of using sequence_validator with multiple values."""

import click

from click_compose import sequence_validator


def validate_positive(
    _ctx: click.Context | None,
    _param: click.Parameter | None,
    value: int,
) -> int:
    """Validate that value is positive."""
    if value <= 0:
        msg = "Must be positive"
        raise click.BadParameter(message=msg)
    return value


@click.command()
@click.option(
    "--numbers",
    multiple=True,
    type=int,
    callback=sequence_validator(validator=validate_positive),
)
def cmd(numbers: tuple[int, ...]) -> None:
    """Example command with sequence validation."""
    click.echo(message=f"Sum: {sum(numbers)}")

Documentation

See the full documentation.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

click_compose-2025.10.27.3.tar.gz (18.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

click_compose-2025.10.27.3-py2.py3-none-any.whl (4.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file click_compose-2025.10.27.3.tar.gz.

File metadata

  • Download URL: click_compose-2025.10.27.3.tar.gz
  • Upload date:
  • Size: 18.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for click_compose-2025.10.27.3.tar.gz
Algorithm Hash digest
SHA256 6d3326a13b690ac7a0f0e99de785aa78ea81d130ba02d609e6367a7af23477a5
MD5 b9d0fbbb74dd8e243f939c0000390e25
BLAKE2b-256 459f7b380e5318643348e256ec31df1362b74dfa12733f76b1a97e1171ba74fe

See more details on using hashes here.

Provenance

The following attestation bundles were made for click_compose-2025.10.27.3.tar.gz:

Publisher: release.yml on adamtheturtle/click-compose

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file click_compose-2025.10.27.3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for click_compose-2025.10.27.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6821fb769067e76d2b2e9c5d4d5e8d974002137322ab71cde65b10bf9c025834
MD5 5a67dfcf2aba58b16d1804500a18c388
BLAKE2b-256 453a411c2ad30f87b2e874a4a4d1578dc9fe11a6ea8f139b4e1f7ff291a934ca

See more details on using hashes here.

Provenance

The following attestation bundles were made for click_compose-2025.10.27.3-py2.py3-none-any.whl:

Publisher: release.yml on adamtheturtle/click-compose

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page