diff --git a/mock/local_test.py b/mock/local_test.py index 1c03962..1408a36 100644 --- a/mock/local_test.py +++ b/mock/local_test.py @@ -14,8 +14,10 @@ from argenta.command.models import InputCommand import inspect -inv = InvalidValueInputFlags(InputFlag('test')) -inve = InvalidValueInputFlags(InputFlag('test')) +router = Router() -print(inv == inve) \ No newline at end of file +@router.command(Command('some')) +def handler(res: Response) -> Response: + pass + diff --git a/mock/mock_app/handlers/routers.py b/mock/mock_app/handlers/routers.py index 225a6b6..f9f3301 100644 --- a/mock/mock_app/handlers/routers.py +++ b/mock/mock_app/handlers/routers.py @@ -20,7 +20,7 @@ def command_help(response: Response): print(response.invalid_value_flags.get_flags()) -@work_router.command(Command('run', 'Run All')) +@work_router.command('run') def command_start_solving(response: Response): print(response.status) print(response.undefined_flags.get_flags()) diff --git a/pyproject.toml b/pyproject.toml index 5ae1ddd..1c218b1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,7 +9,7 @@ license = { text = "MIT" } dependencies = [ "rich (>=14.0.0,<15.0.0)", "art (>=6.4,<7.0)", - "pyreadline3 (>=3.5.4,<4.0.0)", + "pyreadline3>=3.5.4", ] [dependency-groups] diff --git a/src/argenta/command/models.py b/src/argenta/command/models.py index 8be7af9..4c8c252 100644 --- a/src/argenta/command/models.py +++ b/src/argenta/command/models.py @@ -39,7 +39,7 @@ class Command(BaseCommand): """ super().__init__(trigger) self._registered_flags: Flags = flags if isinstance(flags, Flags) else Flags(flags) if isinstance(flags, Flag) else Flags() - self._description = f'Description for "{self._trigger}" command' if not description else description + self._description = f'Very useful command' if not description else description self._aliases = aliases if isinstance(aliases, list) else [] def get_registered_flags(self) -> Flags: diff --git a/src/argenta/response/entity.py b/src/argenta/response/entity.py index bcea1fd..67efeda 100644 --- a/src/argenta/response/entity.py +++ b/src/argenta/response/entity.py @@ -1,5 +1,7 @@ -from argenta.command.flags.models import ValidInputFlags, UndefinedInputFlags, InvalidValueInputFlags from argenta.response.status import Status +from argenta.command.flags import (ValidInputFlags, + UndefinedInputFlags, + InvalidValueInputFlags) class Response: diff --git a/src/argenta/router/entity.py b/src/argenta/router/entity.py index 2a49ac8..87de658 100644 --- a/src/argenta/router/entity.py +++ b/src/argenta/router/entity.py @@ -6,7 +6,10 @@ from argenta.command import Command from argenta.command.models import InputCommand from argenta.response import Response, Status from argenta.router.command_handler.entity import CommandHandlers, CommandHandler -from argenta.command.flags.models import Flags, InputFlags, UndefinedInputFlags, ValidInputFlags, InvalidValueInputFlags +from argenta.command.flags.models import (Flags, InputFlags, + UndefinedInputFlags, + ValidInputFlags, + InvalidValueInputFlags) from argenta.router.exceptions import (RepeatedFlagNameException, TooManyTransferredArgsException, RequiredArgumentNotPassedException, @@ -26,13 +29,15 @@ class Router: self._ignore_command_register: bool = False - def command(self, command: Command) -> Callable: + def command(self, command: Command | str) -> Callable: """ Public. Registers handler :param command: Registered command :return: decorated handler as Callable """ self._validate_command(command) + if isinstance(command, str): + command = Command(command) def command_decorator(func): Router._validate_func_args(func) @@ -128,21 +133,25 @@ class Router: @staticmethod - def _validate_command(command: Command) -> None: + def _validate_command(command: Command | str) -> None: """ Private. Validates the command registered in handler :param command: validated command :return: None if command is valid else raise exception """ - command_name: str = command.get_trigger() - if command_name.find(' ') != -1: - raise TriggerContainSpacesException() - - flags: Flags = command.get_registered_flags() - if flags: - flags_name: list = [x.get_string_entity().lower() for x in flags] - if len(set(flags_name)) < len(flags_name): - raise RepeatedFlagNameException() + match type(command).__name__: + case 'Command': + command_name: str = command.get_trigger() + if command_name.find(' ') != -1: + raise TriggerContainSpacesException() + flags: Flags = command.get_registered_flags() + if flags: + flags_name: list = [x.get_string_entity().lower() for x in flags] + if len(set(flags_name)) < len(flags_name): + raise RepeatedFlagNameException() + case 'str': + if command.find(' ') != -1: + raise TriggerContainSpacesException() @staticmethod @@ -158,13 +167,19 @@ class Router: elif len(transferred_args) == 0: raise RequiredArgumentNotPassedException() + transferred_arg: str = transferred_args[0] func_annotations: dict[str, Type] = get_annotations(func) - if func_annotations: - arg_annotation: Type = func_annotations[transferred_args[0]] - if not arg_annotation is Response: - Console().print(f'\n\nFile "{getsourcefile(func)}", line {getsourcelines(func)[1]+1}\n' - f'[b red]WARNING:[/b red] [i]The typehint of argument([green]{transferred_args[0]}[/green]) passed to the handler is [/i][blue]{Response}[/blue],' - f' [i]but[/i] [bold blue]{arg_annotation}[/bold blue] [i]is specified[/i]', highlight=False) + + if arg_annotation := func_annotations.get(transferred_arg): + if arg_annotation is Response: + pass + else: + file_path: str = getsourcefile(func) + source_line: int = getsourcelines(func)[1]+1 + fprint = Console().print + fprint(f'\nFile "{file_path}", line {source_line}\n[b red]WARNING:[/b red] [i]The typehint ' + f'of argument([green]{transferred_arg}[/green]) passed to the handler is [/i][bold blue]{Response}[/bold blue],' + f' [i]but[/i] [bold blue]{arg_annotation}[/bold blue] [i]is specified[/i]\n', highlight=False)