mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-17 05:03:07 +00:00
feat(python): support case-insensitive choicetype
This commit is contained in:
parent
3ee051bf69
commit
9b9ce11395
@ -40,14 +40,20 @@ if TYPE_CHECKING:
|
||||
|
||||
|
||||
class ChoiceType(click.Choice):
|
||||
def __init__(self, typemap: Dict[str, Any]) -> None:
|
||||
def __init__(self, typemap: Dict[str, Any], case_sensitive: bool = True) -> None:
|
||||
super().__init__(list(typemap.keys()))
|
||||
self.typemap = typemap
|
||||
self.case_sensitive = case_sensitive
|
||||
if case_sensitive:
|
||||
self.typemap = typemap
|
||||
else:
|
||||
self.typemap = {k.lower(): v for k, v in typemap.items()}
|
||||
|
||||
def convert(self, value: str, param: Any, ctx: click.Context) -> Any:
|
||||
def convert(self, value: Any, param: Any, ctx: click.Context) -> Any:
|
||||
if value in self.typemap.values():
|
||||
return value
|
||||
value = super().convert(value, param, ctx)
|
||||
if isinstance(value, str) and not self.case_sensitive:
|
||||
value = value.lower()
|
||||
return self.typemap[value]
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user