cloup.constraints._conditional

This modules contains classes to create conditional constraints.

Classes

If(condition, then[, else_])

Checks one constraint or another depending on the truth value of the condition.

Functions

as_predicate(arg)

Contents

cloup.constraints._conditional.as_predicate(arg)[source]
Parameters

arg (Union[str, Sequence[str], Predicate]) –

Return type

Predicate

class cloup.constraints._conditional.If(condition, then, else_=None)[source]

Bases: cloup.constraints._core.Constraint

A constraint that can be checked against an arbitrary collection of CLI parameters with respect to a specific click.Context (which contains the values assigned to the parameters in ctx.params).

Changed in version 0.9.0: calling a constraint, previously equivalent to check(), is now equivalent to calling cloup.constrained_params() with this constraint as first argument.

Checks one constraint or another depending on the truth value of the condition.

New in version 0.8.0: you can now pass a sequence of parameter names as condition, which corresponds to the predicate AllSet(*param_names).

Parameters
  • condition (Union[str, Sequence[str], Predicate]) – can be either an instance of Predicate or (more often) the name of a parameter or a list/tuple of parameters that must be all set for the condition to be true.

  • then (Constraint) – a constraint checked if the condition is true.

  • else – an (optional) constraint checked if the condition is false.

  • else_ (Optional[Constraint]) –

help(ctx)[source]

A description of the constraint.

Parameters

ctx (click.Context) –

Return type

str

check_consistency(params)[source]

Performs some sanity checks that detect inconsistencies between these constraints and the properties of the input parameters (e.g. required).

For example, a constraint that requires the parameters to be mutually exclusive is not consistent with a group of parameters with multiple required options.

These sanity checks are meant to catch developer’s mistakes and don’t depend on the values assigned to the parameters; therefore:

  • they can be performed before any parameter parsing

  • they can be disabled in production (setting check_constraints_consistency=False in context_settings)

Parameters

params (Sequence[click.Parameter]) – list of click.Parameter instances

Raises

UnsatisfiableConstraint if the constraint cannot be satisfied independently from the values provided by the user

Return type

None

check_values(params, ctx)[source]

Checks that the constraint is satisfied by the input parameters in the given context, which (among other things) contains the values assigned to the parameters in ctx.params.

You probably don’t want to call this method directly. Use check() instead.

Parameters
  • params (Sequence[click.Parameter]) – list of click.Parameter instances

  • ctx (click.Context) – click.Context

Raises

ConstraintViolated

Return type

None

__repr__()[source]

Return repr(self).

Return type

str