cloup.constraints._core

Classes summary

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).

cloup.constraints._core.Operator(*constraints)

Base class for all n-ary operators defined on constraints.

cloup.constraints._core.And(*constraints)

It’s satisfied if all operands are satisfied.

cloup.constraints._core.Or(*constraints)

It’s satisfied if at least one of the operands is satisfied.

cloup.constraints._core.Rephraser(constraint)

A Constraint decorator that can override the help and/or the error message of the wrapped constraint.

cloup.constraints._core.WrapperConstraint(…)

Abstract class that wraps another constraint and delegates all methods to it.

cloup.constraints._core.RequireAtLeast(n)

Satisfied if the number of set parameters is >= n.

cloup.constraints._core.AcceptAtMost(n)

Satisfied if the number of set parameters is <= n.

cloup.constraints._core.RequireExactly(n)

Requires an exact number of parameters to be set.

cloup.constraints._core.AcceptBetween(min, max)

Satisfied if the number of set parameters is between min and max (included).

Contents

cloup.constraints._core.Op
cloup.constraints._core.HelpRephraser
cloup.constraints._core.ErrorRephraser
class cloup.constraints._core.Constraint[source]

Bases: abc.ABC

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).

classmethod must_check_consistency(cls)bool[source]

Returns True if consistency checks are enabled.

classmethod toggle_consistency_checks(cls, value: bool)[source]

Enables/disables consistency checks. Enabling means that:

classmethod consistency_checks_toggled(cls, value: bool)[source]
abstract help(self, ctx: click.Context)str[source]

A description of the constraint.

check_consistency(self, params: Sequence[Parameter])None[source]

Performs some sanity checks that detect inconsistencies between this 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:

Parameters

params – list of click.Parameter instances

Raises

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

abstract check_values(self, params: Sequence[Parameter], ctx: click.Context)[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
Raises

ConstraintViolated

check(self, params: Sequence[Parameter], ctx: Optional[Context] = None)None[source]
check(self, params: Iterable[str], ctx: Optional[Context] = None)None

Raises an exception if the constraint is not satisfied by the input parameters in the given (or current) context.

This method calls both check_consistency() (if enabled) and check_values().

Tip

By default check_consistency() is called since it shouldn’t have any performance impact. Nonetheless, you can disable it in production passing False to toggle_consistency_checks().

Parameters
Raises

ConstraintViolated UnsatisfiableConstraint

rephrased(self, help: Union[None, str, HelpRephraser] = None, error: Union[None, str, ErrorRephraser] = None)cloup.constraints._core.Rephraser[source]
hidden(self)cloup.constraints._core.Rephraser[source]

Hides this constraint from the command help.

__call__(self, param_names: Iterable[str], ctx: Optional[Context] = None)None[source]
__or__(self, other: cloup.constraints._core.Constraint)cloup.constraints._core.Or[source]
__and__(self, other: cloup.constraints._core.Constraint)cloup.constraints._core.And[source]
__repr__(self)[source]

Return repr(self).

class cloup.constraints._core.Operator(*constraints: cloup.constraints._core.Constraint)[source]

Bases: cloup.constraints._core.Constraint, abc.ABC

Base class for all n-ary operators defined on constraints.

Parameters

constraints (cloup.constraints._core.Constraint) –

HELP_SEP :str
help(self, ctx: click.Context)str[source]

A description of the constraint.

check_consistency(self, params: Sequence[Parameter])None[source]

Performs some sanity checks that detect inconsistencies between this 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 (see toggle_consistency_checks())

Parameters

params – list of click.Parameter instances

Raises

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

__repr__(self)[source]

Return repr(self).

class cloup.constraints._core.And(*constraints: cloup.constraints._core.Constraint)[source]

Bases: cloup.constraints._core.Operator

It’s satisfied if all operands are satisfied.

Parameters

constraints (cloup.constraints._core.Constraint) –

HELP_SEP = and
check_values(self, params: Sequence[Parameter], ctx: click.Context)[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
Raises

ConstraintViolated

__and__(self, other)cloup.constraints._core.And[source]
class cloup.constraints._core.Or(*constraints: cloup.constraints._core.Constraint)[source]

Bases: cloup.constraints._core.Operator

It’s satisfied if at least one of the operands is satisfied.

Parameters

constraints (cloup.constraints._core.Constraint) –

HELP_SEP = or
check_values(self, params: Sequence[Parameter], ctx: click.Context)[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
Raises

ConstraintViolated

__or__(self, other)cloup.constraints._core.Or[source]
class cloup.constraints._core.Rephraser(constraint: cloup.constraints._core.Constraint, help: Union[None, str, HelpRephraser] = None, error: Union[None, str, ErrorRephraser] = None)[source]

Bases: cloup.constraints._core.Constraint

A Constraint decorator that can override the help and/or the error message of the wrapped constraint.

This is useful also for defining new constraints. See also WrapperConstraint.

Parameters
help(self, ctx: click.Context)str[source]

A description of the constraint.

check_consistency(self, params: Sequence[Parameter])None[source]

Performs some sanity checks that detect inconsistencies between this 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 (see toggle_consistency_checks())

Parameters

params – list of click.Parameter instances

Raises

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

check_values(self, params: Sequence[Parameter], ctx: click.Context)[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
Raises

ConstraintViolated

__repr__(self)[source]

Return repr(self).

class cloup.constraints._core.WrapperConstraint(constraint: cloup.constraints._core.Constraint, **attrs)[source]

Bases: cloup.constraints._core.Constraint

Abstract class that wraps another constraint and delegates all methods to it. Useful when you want to define a parametric constraint combining other existing constraints minimizing the boilerplate.

This is an alternative to defining a function and using Rephraser. Feel free to do that in your code, but cloup will stick to the convention that parametric constraints are defined as classes and written in camel-case.

Parameters

constraint (cloup.constraints._core.Constraint) –

help(self, ctx: click.Context)str[source]

A description of the constraint.

check_consistency(self, params: Sequence[Parameter])None[source]

Performs some sanity checks that detect inconsistencies between this 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 (see toggle_consistency_checks())

Parameters

params – list of click.Parameter instances

Raises

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

check_values(self, params: Sequence[Parameter], ctx: click.Context)[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
Raises

ConstraintViolated

__repr__(self)[source]

Return repr(self).

class cloup.constraints._core.RequireAtLeast(n: int)[source]

Bases: cloup.constraints._core.Constraint

Satisfied if the number of set parameters is >= n.

Parameters

n (int) –

help(self, ctx: click.Context)str[source]

A description of the constraint.

check_consistency(self, params: Sequence[Parameter])None[source]

Performs some sanity checks that detect inconsistencies between this 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 (see toggle_consistency_checks())

Parameters

params – list of click.Parameter instances

Raises

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

check_values(self, params: Sequence[Parameter], ctx: click.Context)[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
Raises

ConstraintViolated

__repr__(self)[source]

Return repr(self).

class cloup.constraints._core.AcceptAtMost(n: int)[source]

Bases: cloup.constraints._core.Constraint

Satisfied if the number of set parameters is <= n.

Parameters

n (int) –

help(self, ctx: click.Context)str[source]

A description of the constraint.

check_consistency(self, params: Sequence[Parameter])None[source]

Performs some sanity checks that detect inconsistencies between this 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 (see toggle_consistency_checks())

Parameters

params – list of click.Parameter instances

Raises

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

check_values(self, params: Sequence[Parameter], ctx: click.Context)[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
Raises

ConstraintViolated

__repr__(self)[source]

Return repr(self).

class cloup.constraints._core.RequireExactly(n: int)[source]

Bases: cloup.constraints._core.WrapperConstraint

Requires an exact number of parameters to be set.

Parameters

n (int) –

help(self, ctx: click.Context)str[source]

A description of the constraint.

check_values(self, params: Sequence[Parameter], ctx: click.Context)[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
Raises

ConstraintViolated

class cloup.constraints._core.AcceptBetween(min: int, max: int)[source]

Bases: cloup.constraints._core.WrapperConstraint

Abstract class that wraps another constraint and delegates all methods to it. Useful when you want to define a parametric constraint combining other existing constraints minimizing the boilerplate.

This is an alternative to defining a function and using Rephraser. Feel free to do that in your code, but cloup will stick to the convention that parametric constraints are defined as classes and written in camel-case.

Parameters
help(self, ctx: click.Context)str[source]

A description of the constraint.

cloup.constraints._core.require_all
cloup.constraints._core.accept_none
cloup.constraints._core.all_or_none
cloup.constraints._core.mutually_exclusive