This commit is contained in:
2025-02-27 01:16:39 +03:00
parent 158e5eb75a
commit b72fcc6a11
5 changed files with 49 additions and 16 deletions
+26 -6
View File
@@ -1,8 +1,12 @@
from typing import Callable
from inspect import getfullargspec
from ..command.entity import Command
from argenta.command.input_comand.entity import InputCommand
from argenta.command.input_comand.exceptions import InvalidInputFlagException
from ..router.entity import Router
from ..command.input_comand.entity import InputCommand
from ..command.input_comand.exceptions import (IncorrectInputFlagException,
InvalidInputFlagsHandlerHasBeenAlreadyCreatedException,
IncorrectNumberArgsInvalidInputFlagsHandlerException)
from .exceptions import (InvalidRouterInstanceException,
InvalidDescriptionMessagePatternException,
OnlyOneMainRouterIsAllowedException,
@@ -44,6 +48,7 @@ class App:
self.repeat_command_groups = repeat_command_groups
self._routers: list[Router] = []
self._invalid_input_flags_handler: Callable[[str], None] | None = None
self._registered_router_entities: list[dict[str, str | list[dict[str, Callable[[], None] | Command]] | Router]] = []
self._app_main_router: Router | None = None
self._description_message_pattern: str = '[{command}] *=*=* {description}'
@@ -67,11 +72,16 @@ class App:
self.print_func(self.prompt)
raw_command: str = input()
try:
input_command: InputCommand = InputCommand.parse(raw_command=raw_command)
except InvalidInputFlagException:
except IncorrectInputFlagException:
self.print_func(self.line_separate)
if self._invalid_input_flags_handler:
self._invalid_input_flags_handler(raw_command)
else:
self.print_func(f'Incorrect flag syntax: "{raw_command}"')
self.print_func(self.line_separate)
self.print_func(self.command_group_description_separate)
if not self.repeat_command_groups:
self.print_func(self.prompt)
continue
@@ -86,7 +96,6 @@ class App:
continue
for router in self._routers:
router.input_command_handler(input_command)
self.print_func(self.line_separate)
@@ -112,6 +121,17 @@ class App:
self._description_message_pattern: str = pattern
def set_invalid_input_flags_handler(self, handler: Callable[[str], None]) -> None:
if self._invalid_input_flags_handler:
raise InvalidInputFlagsHandlerHasBeenAlreadyCreatedException()
else:
args = getfullargspec(handler).args
if len(args) != 1:
raise IncorrectNumberArgsInvalidInputFlagsHandlerException()
else:
self._invalid_input_flags_handler = handler
def get_main_router(self) -> Router:
return self._app_main_router
@@ -169,7 +189,7 @@ class App:
raise MissingHandlerForUnknownCommandsException()
for router in self._routers:
if router.get_unknown_command_func() and self._app_main_router is not router:
if router.get_unknown_command_func() and not (self._app_main_router is router):
raise HandlerForUnknownCommandsOnNonMainRouterException()