import click


def cb_bbox(ctx, param, value):

    """
    Click callback to handle ``--bbox`` syntax and validation.

    Parameters
    ----------
    ctx : click.Context
        Ignored.
    param : click.Parameter
        Ignored.
    value : tuple
        x_min, y_min, x_max, y_max

    Raises
    ------
    click.BadParameter

    Returns
    -------
    tuple
        (x_min, y_min, x_max, y_max)
    """

    if not value:
        return None

    bbox = value
    x_min, y_min, x_max, y_max = bbox

    if (x_max < x_min) or (y_max < y_min):
        raise click.BadParameter('min exceeds max for one or more dimensions: {0}'.format(' '.join(bbox)))

    return bbox