diff options
author | Romain Gonçalves <me@rgoncalves.se> | 2022-10-03 23:57:35 +0200 |
---|---|---|
committer | Romain Gonçalves <me@rgoncalves.se> | 2022-10-03 23:57:35 +0200 |
commit | 185bf5c7aebd1cc0ccaa8427b38141be71f9b8eb (patch) | |
tree | 0aaa90cc0b679dfb156cc3131fc92c0e9e4bfca1 | |
parent | 8ad3000474ea48589601f5bb1a05b7b608eb5ef4 (diff) | |
download | pydanclick-185bf5c7aebd1cc0ccaa8427b38141be71f9b8eb.tar.gz |
wip: add enum support
-rw-r--r-- | pydanclick/core.py | 7 | ||||
-rw-r--r-- | pydanclick/examples/__main__.py | 4 | ||||
-rw-r--r-- | pydanclick/schemas.py | 10 |
3 files changed, 16 insertions, 5 deletions
diff --git a/pydanclick/core.py b/pydanclick/core.py index 0c0518b..d88816f 100644 --- a/pydanclick/core.py +++ b/pydanclick/core.py @@ -15,7 +15,7 @@ class Command(click.Command): Override of click.Command, specifically for pydantic integration. """ - def invoke(self, ctx: click.Context) -> Any: + def invoke(self, ctx: click.Context) -> Any: # pragma: no cover """ Callback with serialized click options to pydantic object(s). """ @@ -115,6 +115,11 @@ def generate_cli_option( case "boolean": option_type = bool is_flag = True + case None: + if parameter.ref: + typ = schema.get_definition_from_ref(parameter.ref) + print(typ) + option_type = click.Choice(typ.enum) return click.option( f"--{key_prefix}{option_title}", diff --git a/pydanclick/examples/__main__.py b/pydanclick/examples/__main__.py index cea5821..0367e17 100644 --- a/pydanclick/examples/__main__.py +++ b/pydanclick/examples/__main__.py @@ -7,7 +7,7 @@ from pydanclick.core import Command, generate_cli_options class MainArguments(BaseModel): - class NetworkEnum(Enum): + class NetworkEnum(str, Enum): dhcp = "dhcp" static = "static" disconnected = "disconnected" @@ -15,7 +15,7 @@ class MainArguments(BaseModel): filename: str = Field(min_length=10) minimum_version: int = Field(gt=0) force_download: bool = Field(default=False) - network_type = NetworkEnum + network_type: NetworkEnum @command(cls=Command) diff --git a/pydanclick/schemas.py b/pydanclick/schemas.py index 3b2ffe4..5601b2b 100644 --- a/pydanclick/schemas.py +++ b/pydanclick/schemas.py @@ -10,14 +10,14 @@ class CliSchema(BaseModel): enum: list[str] class PropertySchema(BaseModel): - type: str title: None | str + type: None | str description: None | str exclusiveMinimum: None | int exlusiveMaximum: None | int minLength: None | int maxLength: None | int - _ref: CliSchema.DefinitionSchema | None = Field(alias="$ref") + ref: None | str = Field(alias="$ref") class Config: extra = "allow" @@ -27,3 +27,9 @@ class CliSchema(BaseModel): required: list[str] description: None | str definitions: None | dict[str, CliSchema.DefinitionSchema] + + def get_definition_from_ref(self, ref: str) -> CliSchema.DefinitionSchema: + if not self.definitions: + raise RuntimeError + + return self.definitions[ref.split("/")[-1]] |