File: group_validators.rst

package info (click to toggle)
python-cyclopts 3.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,288 kB
  • sloc: python: 11,445; makefile: 24
file content (73 lines) | stat: -rw-r--r-- 3,307 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
.. _Group Validators:

================
Group Validators
================
Group validators operate on a set of parameters, :ref:`ensuring that their values are mutually compatible <Parameter Groups>`.
Cyclopts has some builtin common group validators in the :ref:`cyclopts.validators <API Validators>` module.

.. _Group Validators - LimitedChoice:

-------------
LimitedChoice
-------------
Limits the number of specified arguments within the group.
Most commonly used for mutually-exclusive arguments (default behavior).


.. code-block:: python

   from cyclopts import App, Group, Parameter, validators
   from typing import Annotated

   app = App()

   vehicle = Group(
       "Vehicle (choose one)",
       default_parameter=Parameter(negative=""),  # Disable "--no-" flags
       validator=validators.LimitedChoice(),  # Mutually Exclusive Options
   )

   @app.default
   def main(
       *,
       car: Annotated[bool, Parameter(group=vehicle)] = False,
       truck: Annotated[bool, Parameter(group=vehicle)] = False,
   ):
       if car:
           print("I'm driving a car.")
       if truck:
           print("I'm driving a truck.")

   app()

.. code-block:: console

   $ python drive.py --help
   Usage: main COMMAND [OPTIONS]

   ╭─ Commands ─────────────────────────────────────────────────────────╮
   │ --help -h  Display this message and exit.                          │
   │ --version  Display application version.                            │
   ╰────────────────────────────────────────────────────────────────────╯
   ╭─ Vehicle (choose one) ─────────────────────────────────────────────╮
   │ --car    [default: False]                                          │
   │ --truck  [default: False]                                          │
   ╰────────────────────────────────────────────────────────────────────╯

   $ python drive.py --car
   I'm driving a car.

   $ python drive.py --car --truck
   ╭─ Error ────────────────────────────────────────────────────────────╮
   │ Invalid values for group "Vehicle (choose one)". Mutually          │
   │ exclusive arguments: {--car, --truck}                              │
   ╰────────────────────────────────────────────────────────────────────╯

See the :class:`.LimitedChoice` docs for more info.

-----------------
MutuallyExclusive
-----------------
Alias for :class:`.LimitedChoice` with default arguments.
Exists primarily because the usage/implication will be more directly obvious and searchable to developers than :class:`.LimitedChoice`.