Skip to content

Commit

Permalink
feat(parameters): restrict values to specific list of choices
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-martian committed Oct 11, 2024
1 parent 123e343 commit f22decc
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion rebabel_format/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

from rebabel_format.config import get_single_param, parse_mappings
from dataclasses import dataclass
from typing import Any
from typing import Any, Optional, Sequence

@dataclass
class Parameter:
required: bool = True
default: Any = None
type: type = None
help: str = 'a parameter'
choices: Optional[Sequence[Any]] = None

name = None

Expand Down Expand Up @@ -39,6 +40,8 @@ def process(self, name, value):
else:
if self.type and not isinstance(value, self.type):
raise ValueError(f"Parameter '{name}' should be {self.type} but it is {type(value)}.")
if self.choices and value not in self.choices:
raise ValueError(f"Parameter '{name}' should be one of {', '.join(map(str, self.choices))}.")
return value

def extract(self, conf, action, attribute):
Expand Down

0 comments on commit f22decc

Please sign in to comment.