| 1 |
.. _Group Validators: |
| 2 |
|
| 3 |
================ |
| 4 |
Group Validators |
| 5 |
================ |
| 6 |
Group validators operate on a set of parameters, :ref:`ensuring that their values are mutually compatible <Parameter Groups>`. |
| 7 |
Validator(s) for a group can be set via the :attr:`.Group.validator` attribute. An individual validator is a callable object/function with signature: |
| 8 |
|
| 9 |
.. code-block:: python |
| 10 |
|
| 11 |
def validator(argument_collection: ArgumentCollection): |
| 12 |
"Raise an exception if something is invalid." |
| 13 |
|
| 14 |
|
| 15 |
Cyclopts has some builtin common group validators in the :ref:`cyclopts.validators <API Validators>` module. |
| 16 |
|
| 17 |
.. _Group Validators - LimitedChoice: |
| 18 |
|
| 19 |
------------- |
| 20 |
LimitedChoice |
| 21 |
------------- |
| 22 |
Limits the number of specified arguments within the group. |
| 23 |
Most commonly used for mutually-exclusive arguments (default behavior). |
| 24 |
|
| 25 |
|
| 26 |
.. code-block:: python |
| 27 |
|
| 28 |
from cyclopts import App, Group, Parameter, validators |
| 29 |
from typing import Annotated |
| 30 |
|
| 31 |
app = App() |
| 32 |
|
| 33 |
vehicle = Group( |
| 34 |
"Vehicle (choose one)", |
| 35 |
default_parameter=Parameter(negative=""), # Disable "--no-" flags |
| 36 |
validator=validators.LimitedChoice(), # Mutually Exclusive Options |
| 37 |
) |
| 38 |
|
| 39 |
@app.default |
| 40 |
def main( |
| 41 |
*, |
| 42 |
car: Annotated[bool, Parameter(group=vehicle)] = False, |
| 43 |
truck: Annotated[bool, Parameter(group=vehicle)] = False, |
| 44 |
): |
| 45 |
if car: |
| 46 |
print("I'm driving a car.") |
| 47 |
if truck: |
| 48 |
print("I'm driving a truck.") |
| 49 |
|
| 50 |
app() |
| 51 |
|
| 52 |
.. code-block:: console |
| 53 |
|
| 54 |
$ python drive.py --help |
| 55 |
Usage: main COMMAND [OPTIONS] |
| 56 |
|
| 57 |
╭─ Commands ─────────────────────────────────────────────────────────╮ |
| 58 |
│ --help -h Display this message and exit. │ |
| 59 |
│ --version Display application version. │ |
| 60 |
╰────────────────────────────────────────────────────────────────────╯ |
| 61 |
╭─ Vehicle (choose one) ─────────────────────────────────────────────╮ |
| 62 |
│ --car [default: False] │ |
| 63 |
│ --truck [default: False] │ |
| 64 |
╰────────────────────────────────────────────────────────────────────╯ |
| 65 |
|
| 66 |
$ python drive.py --car |
| 67 |
I'm driving a car. |
| 68 |
|
| 69 |
$ python drive.py --car --truck |
| 70 |
╭─ Error ────────────────────────────────────────────────────────────╮ |
| 71 |
│ Invalid values for group "Vehicle (choose one)". Mutually │ |
| 72 |
│ exclusive arguments: {--car, --truck} │ |
| 73 |
╰────────────────────────────────────────────────────────────────────╯ |
| 74 |
|
| 75 |
See the :class:`.LimitedChoice` docs for more info. |
| 76 |
|
| 77 |
----------------- |
| 78 |
MutuallyExclusive |
| 79 |
----------------- |
| 80 |
Alias for :class:`.LimitedChoice` with default arguments. |
| 81 |
Exists primarily because the usage/implication will be more directly obvious and searchable to developers than :class:`.LimitedChoice`. |
| 82 |
Since this class takes no arguments, an already instantiated version :obj:`.mutually_exclusive` is also provided for convenience. |
| 83 |
|
| 84 |
----------- |
| 85 |
all_or_none |
| 86 |
----------- |
| 87 |
Group validator that enforces that either **all** parameters in the group must be supplied an argument, or **none** of them. |
| 88 |
|
| 89 |
.. code-block:: python |
| 90 |
|
| 91 |
from typing import Annotated |
| 92 |
|
| 93 |
from cyclopts import App, Group, Parameter |
| 94 |
from cyclopts.validators import all_or_none |
| 95 |
|
| 96 |
app = App() |
| 97 |
|
| 98 |
group_1 = Group(validator=all_or_none) |
| 99 |
group_2 = Group(validator=all_or_none) |
| 100 |
|
| 101 |
|
| 102 |
@app.default |
| 103 |
def default( |
| 104 |
foo: Annotated[bool, Parameter(group=group_1)] = False, |
| 105 |
bar: Annotated[bool, Parameter(group=group_1)] = False, |
| 106 |
fizz: Annotated[bool, Parameter(group=group_2)] = False, |
| 107 |
buzz: Annotated[bool, Parameter(group=group_2)] = False, |
| 108 |
): |
| 109 |
print(f"{foo=} {bar=}") |
| 110 |
print(f"{fizz=} {buzz=}") |
| 111 |
|
| 112 |
|
| 113 |
if __name__ == "__main__": |
| 114 |
app() |
| 115 |
|
| 116 |
.. code-block:: console |
| 117 |
|
| 118 |
$ python all_or_none.py |
| 119 |
foo=False bar=False |
| 120 |
fizz=False buzz=False |
| 121 |
|
| 122 |
$ python all_or_none.py --foo |
| 123 |
╭─ Error ──────────────────────────────────────────────────────────╮ |
| 124 |
│ Missing argument: --bar │ |
| 125 |
╰──────────────────────────────────────────────────────────────────╯ |
| 126 |
|
| 127 |
$ python all_or_none.py --foo --bar |
| 128 |
foo=True bar=True |
| 129 |
fizz=False buzz=False |
| 130 |
|
| 131 |
$ python all_or_none.py --foo --bar --fizz |
| 132 |
╭─ Error ────────────────────────────────────────────────────────────╮ |
| 133 |
│ Missing argument: --buzz │ |
| 134 |
╰────────────────────────────────────────────────────────────────────╯ |
| 135 |
|
| 136 |
$ python all_or_none.py --foo --bar --fizz --buzz |
| 137 |
foo=True bar=True |
| 138 |
fizz=True buzz=True |
| 139 |
|
| 140 |
|
| 141 |
See the :obj:`.all_or_none` docs for more info. |