| 1 |
from typing import TYPE_CHECKING |
| 2 |
|
| 3 |
if TYPE_CHECKING: |
| 4 |
from cyclopts.argument import ArgumentCollection |
| 5 |
|
| 6 |
|
| 7 |
class LimitedChoice: |
| 8 |
def __init__( |
| 9 |
self, |
| 10 |
min: int = 0, |
| 11 |
max: int | None = None, |
| 12 |
allow_none: bool = False, |
| 13 |
): |
| 14 |
"""Group validator that limits the number of selections per group. |
| 15 |
|
| 16 |
Commonly used for enforcing mutually-exclusive parameters (default behavior). |
| 17 |
|
| 18 |
Parameters |
| 19 |
---------- |
| 20 |
min: int |
| 21 |
The minimum (inclusive) number of CLI parameters allowed. |
| 22 |
If negative, then **all** parameters in the group must have CLI values provided. |
| 23 |
max: int | None |
| 24 |
The maximum (inclusive) number of CLI parameters allowed. |
| 25 |
Defaults to ``1`` if ``min==0``, ``min`` otherwise. |
| 26 |
allow_none: bool |
| 27 |
If :obj:`True`, also allow 0 CLI parameters (even if ``min`` is greater than 0). |
| 28 |
Defaults to :obj:`False`. |
| 29 |
""" |
| 30 |
self.min = min |
| 31 |
self.max = (self.min or 1) if max is None else max |
| 32 |
if self.max < self.min: |
| 33 |
raise ValueError("max must be >=min.") |
| 34 |
self.allow_none = allow_none |
| 35 |
|
| 36 |
def __call__(self, argument_collection: "ArgumentCollection"): |
| 37 |
group_size = len(argument_collection) |
| 38 |
populated_argument_collection = argument_collection.filter_by(value_set=True) |
| 39 |
n_arguments = len(populated_argument_collection) |
| 40 |
|
| 41 |
if self.allow_none and n_arguments == 0: |
| 42 |
return |
| 43 |
elif self.min < 0: |
| 44 |
|
| 45 |
if group_size == n_arguments: |
| 46 |
return |
| 47 |
all_names = {a.name for a in argument_collection} |
| 48 |
supplied_names = {a.name for a in populated_argument_collection} |
| 49 |
missing_names = sorted(all_names - supplied_names) |
| 50 |
if len(missing_names) == 1: |
| 51 |
raise ValueError(f"Missing argument: {missing_names[0]}") |
| 52 |
else: |
| 53 |
raise ValueError(f"Missing arguments: {missing_names}") |
| 54 |
elif self.min <= n_arguments <= self.max: |
| 55 |
return |
| 56 |
else: |
| 57 |
offenders = ( |
| 58 |
"{" |
| 59 |
+ ", ".join( |
| 60 |
a.tokens[0].keyword if (a.tokens and a.tokens[0].keyword) else a.name |
| 61 |
for a in populated_argument_collection |
| 62 |
) |
| 63 |
+ "}" |
| 64 |
) |
| 65 |
if self.min == 0 and self.max == 1: |
| 66 |
raise ValueError(f"Mutually exclusive arguments: {offenders}") |
| 67 |
else: |
| 68 |
raise ValueError( |
| 69 |
f"Received {n_arguments} arguments: {offenders}. Only [{self.min}, {self.max}] choices may be specified." |
| 70 |
) |
| 71 |
|
| 72 |
|
| 73 |
class MutuallyExclusive(LimitedChoice): |
| 74 |
def __init__(self): |
| 75 |
"""Alias for :class:`LimitedChoice` to make intentions more obvious. |
| 76 |
|
| 77 |
Only 1 argument in the group can be supplied a value. |
| 78 |
""" |
| 79 |
super().__init__() |
| 80 |
|
| 81 |
|
| 82 |
mutually_exclusive = MutuallyExclusive() |
| 83 |
all_or_none = LimitedChoice(-1, allow_none=True) |