summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--pydanclick/core.py40
-rw-r--r--pydanclick/examples/__main__.py6
2 files changed, 32 insertions, 14 deletions
diff --git a/pydanclick/core.py b/pydanclick/core.py
index d88816f..9ccdda9 100644
--- a/pydanclick/core.py
+++ b/pydanclick/core.py
@@ -1,6 +1,7 @@
from __future__ import annotations
import click
+import functools
import inspect
from typing import Any
@@ -34,18 +35,21 @@ class Command(click.Command):
ctx.invoke(self.callback, **arguments)
-def get_signature_arguments(
- func: Callable
-) -> list[tuple[str, inspect.Parameter]]:
+def command(name: str = None, cls: Type = Command, **attrs: Any) -> Callable:
"""
- Return the arguments of a function.
-
- >>> get_signature_arguments(get_signature_arguments)
- [('func', <Parameter "func: 'Callable'">)]
+ Pydantic arguments to click command.
"""
- return [
- parameter for parameter in inspect.signature(func).parameters.items()
- ]
+
+ def wrapper(entrypoint: Callable) -> Callable:
+
+ @functools.wraps(entrypoint)
+ def _(*args, **kwargs):
+ entrypoint(*args, **kwargs)
+
+ options = generate_cli_options(**attrs)(_)
+ return click.command(options)
+
+ return wrapper
def is_valid_schema_annotation(annotation: Type) -> bool:
@@ -68,6 +72,20 @@ def is_valid_schema_annotation(annotation: Type) -> bool:
)
+def get_signature_arguments(
+ func: Callable
+) -> list[tuple[str, inspect.Parameter]]:
+ """
+ Return the arguments of a function.
+
+ >>> get_signature_arguments(get_signature_arguments)
+ [('func', <Parameter "func: 'Callable'">)]
+ """
+ return [
+ parameter for parameter in inspect.signature(func).parameters.items()
+ ]
+
+
def get_processable_arguments(
func: Callable
) -> list[tuple[str, inspect.Parameter]]:
@@ -133,7 +151,7 @@ def generate_cli_option(
)
-def generate_cli_options(key_prefix: str = "") -> Callable:
+def generate_cli_options(*, key_prefix: str = "", **kwargs) -> Callable:
"""
Generate the option(s) of a click.Command from a pydantic schema.
"""
diff --git a/pydanclick/examples/__main__.py b/pydanclick/examples/__main__.py
index 0367e17..8a6a9cd 100644
--- a/pydanclick/examples/__main__.py
+++ b/pydanclick/examples/__main__.py
@@ -1,8 +1,9 @@
-from click import command
+# from click import command
from enum import Enum
from pydantic import BaseModel, Field
from pydanclick.core import Command, generate_cli_options
+from pydanclick.core import command
class MainArguments(BaseModel):
@@ -18,8 +19,7 @@ class MainArguments(BaseModel):
network_type: NetworkEnum
-@command(cls=Command)
-@generate_cli_options()
+@command(key_prefix="", a="c")
def main(parameters: MainArguments) -> None:
print(vars(parameters))
remember that computers suck.