import click


def set_org_name(required):
    """Generate a callback for processing the `org_name` option or argument

    `required` is a boolean for whether org_name is required
    """
    # could be generalized to work for any mutex pair (or list) but no obvious need
    def callback(ctx, param, value):
        """Callback which enforces mutex and 'required' behaviour (if required)."""
        prev_value = ctx.params.get("org_name")
        if value and prev_value and prev_value != value:
            raise click.UsageError(
                f"Either ORGNAME or --org ORGNAME should be supplied, not both ({value}, {prev_value})"
            )
        ctx.params["org_name"] = value or prev_value
        if required and not ctx.params.get("org_name"):
            raise click.UsageError("Please specify ORGNAME or --org ORGNAME")

    return callback