From 3347bf94b255bbfe566952c509b7ff9963013078 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Gon=C3=A7alves?= Date: Wed, 5 Oct 2022 15:37:02 +0200 Subject: wip: fix required key for option arguments --- pydanclick/core.py | 14 ++++++++------ pydanclick/schemas.py | 1 + 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/pydanclick/core.py b/pydanclick/core.py index 1da29a1..cfb5512 100644 --- a/pydanclick/core.py +++ b/pydanclick/core.py @@ -87,6 +87,7 @@ def get_processable_arguments( def get_option_arguments( schema: CliSchema, parameter: CliSchema.PropertySchema, + key: str, *args, **kwargs ) -> OptionArgumentsSchema: @@ -95,6 +96,7 @@ def get_option_arguments( """ arguments = OptionArgumentsSchema(help=parameter.description) + arguments.required = key in schema.required option_min = parameter.minLength or parameter.exclusiveMinimum option_max = parameter.maxLength or parameter.exlusiveMaximum @@ -117,7 +119,7 @@ def get_option_arguments( if parameter.ref: definition = schema.get_definition_from_ref(parameter.ref) arguments = get_option_arguments( - schema, definition, *args, **kwargs + schema, definition, key, *args, **kwargs ) return arguments @@ -140,19 +142,19 @@ def get_parameters_from_arguments( def generate_cli_option( schema: CliSchema, - title: str, parameter: CliSchema.PropertySchema, + key: str, *, key_prefix: None | str = "" ) -> Callable: """ Generate the option object of a click.Command. """ - option_title = title.lower().replace("_", "-") + option_title = key.lower().replace("_", "-") return click.option( f"--{key_prefix}{option_title}", - **dict(get_option_arguments(schema, parameter)) + **dict(get_option_arguments(schema, parameter, key)) ) @@ -167,9 +169,9 @@ def generate_cli_options(*, key_prefix: str = "", **kwargs) -> Callable: schema = CliSchema(**function_argument[1].annotation.schema()) properties = schema.properties.items() - for title, parameter in properties: + for key, parameter in properties: function = generate_cli_option( - schema, title, parameter, key_prefix=key_prefix + schema, parameter, key, key_prefix=key_prefix )(function) return function diff --git a/pydanclick/schemas.py b/pydanclick/schemas.py index 7fb79ce..f0308e8 100644 --- a/pydanclick/schemas.py +++ b/pydanclick/schemas.py @@ -9,6 +9,7 @@ class CliSchema(BaseModel): title: None | str type: None | str description: None | str + required: None | bool exclusiveMinimum: None | int exlusiveMaximum: None | int minLength: None | int -- cgit v1.2.3