mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 18:15:28 +03:00
Update documentation and code snippets
This commit is contained in:
@@ -187,7 +187,7 @@ class BaseApp:
|
||||
)
|
||||
)
|
||||
|
||||
elif isinstance(self._dividing_line, StaticDividingLine): # pyright: ignore[reportUnnecessaryIsInstance]
|
||||
elif isinstance(self._dividing_line, StaticDividingLine):
|
||||
self._print_func(
|
||||
self._dividing_line.get_full_static_line(
|
||||
is_override=self._override_system_messages
|
||||
@@ -374,18 +374,7 @@ class BaseApp:
|
||||
raise RuntimeError(f"Router for '{input_command.trigger}' not found. Panic!")
|
||||
|
||||
if processing_router.disable_redirect_stdout:
|
||||
dividing_line_unit_part: str = self._dividing_line.get_unit_part()
|
||||
self._print_func(
|
||||
StaticDividingLine(dividing_line_unit_part).get_full_static_line(
|
||||
is_override=self._override_system_messages
|
||||
)
|
||||
)
|
||||
processing_router.finds_appropriate_handler(input_command)
|
||||
self._print_func(
|
||||
StaticDividingLine(dividing_line_unit_part).get_full_static_line(
|
||||
is_override=self._override_system_messages
|
||||
)
|
||||
)
|
||||
else:
|
||||
stdout_result = self._capture_stdout(
|
||||
lambda: processing_router.finds_appropriate_handler(input_command)
|
||||
@@ -394,10 +383,7 @@ class BaseApp:
|
||||
|
||||
|
||||
AVAILABLE_DIVIDING_LINES: TypeAlias = StaticDividingLine | DynamicDividingLine
|
||||
DEFAULT_DIVIDING_LINE: StaticDividingLine = StaticDividingLine()
|
||||
|
||||
DEFAULT_PRINT_FUNC: Printer = Console().print
|
||||
DEFAULT_AUTOCOMPLETER: AutoCompleter = AutoCompleter()
|
||||
DEFAULT_EXIT_COMMAND: Command = Command("q", description="Exit command")
|
||||
|
||||
|
||||
@@ -410,10 +396,10 @@ class App(BaseApp):
|
||||
farewell_message: str = "\nSee you\n",
|
||||
exit_command: Command = DEFAULT_EXIT_COMMAND,
|
||||
system_router_title: str = "System points:",
|
||||
dividing_line: AVAILABLE_DIVIDING_LINES | None = DEFAULT_DIVIDING_LINE,
|
||||
dividing_line: AVAILABLE_DIVIDING_LINES | None = None,
|
||||
repeat_command_groups_printing: bool = False,
|
||||
override_system_messages: bool = False,
|
||||
autocompleter: AutoCompleter = DEFAULT_AUTOCOMPLETER,
|
||||
autocompleter: AutoCompleter | None = None,
|
||||
print_func: Printer = DEFAULT_PRINT_FUNC,
|
||||
) -> None:
|
||||
"""
|
||||
@@ -440,7 +426,7 @@ class App(BaseApp):
|
||||
dividing_line=dividing_line,
|
||||
repeat_command_groups_printing=repeat_command_groups_printing,
|
||||
override_system_messages=override_system_messages,
|
||||
autocompleter=autocompleter,
|
||||
autocompleter=autocompleter or AutoCompleter(),
|
||||
print_func=print_func,
|
||||
)
|
||||
if not self._override_system_messages:
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
from argenta.command.flag import Flag as Flag
|
||||
from argenta.command.flag import Flags as Flags
|
||||
from argenta.command.flag.models import Flags as Flags, InputFlags as InputFlags
|
||||
from argenta.command.flag import InputFlag as InputFlag
|
||||
from argenta.command.flag import InputFlags as InputFlags
|
||||
from argenta.command.flag import PossibleValues as PossibleValues
|
||||
from argenta.command.flag.defaults import PredefinedFlags as PredefinedFlags
|
||||
from argenta.command.models import Command as Command
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
from argenta.command.flag.flags.models import Flags as Flags
|
||||
from argenta.command.flag.flags.models import InputFlags as InputFlags
|
||||
from argenta.command.flag.models import Flags as Flags, InputFlags as InputFlags
|
||||
from argenta.command.flag.models import Flag as Flag
|
||||
from argenta.command.flag.models import InputFlag as InputFlag
|
||||
from argenta.command.flag.models import PossibleValues as PossibleValues
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
from argenta.command.flag.flags.models import Flags as Flags
|
||||
from argenta.command.flag.flags.models import InputFlags as InputFlags
|
||||
@@ -1,107 +0,0 @@
|
||||
__all__ = ["Flags", "InputFlags"]
|
||||
|
||||
from collections.abc import Iterator
|
||||
from typing import Generic, TypeVar, override
|
||||
|
||||
from argenta.command.flag.models import Flag, InputFlag
|
||||
|
||||
FlagType = TypeVar("FlagType")
|
||||
|
||||
|
||||
class BaseFlags(Generic[FlagType]):
|
||||
def __init__(self, flags: list[FlagType] | None = None) -> None:
|
||||
"""
|
||||
Public. A model that combines the registered flags
|
||||
:param flags: the flags that will be registered
|
||||
:return: None
|
||||
"""
|
||||
self.flags: list[FlagType] = flags if flags else []
|
||||
|
||||
def add_flag(self, flag: FlagType) -> None:
|
||||
"""
|
||||
Public. Adds a flag to the list of flags
|
||||
:param flag: flag to add
|
||||
:return: None
|
||||
"""
|
||||
self.flags.append(flag)
|
||||
|
||||
def add_flags(self, flags: list[FlagType]) -> None:
|
||||
"""
|
||||
Public. Adds a list of flags to the list of flags
|
||||
:param flags: list of flags to add
|
||||
:return: None
|
||||
"""
|
||||
self.flags.extend(flags)
|
||||
|
||||
def __len__(self) -> int:
|
||||
return len(self.flags)
|
||||
|
||||
def __iter__(self) -> Iterator[FlagType]:
|
||||
return iter(self.flags)
|
||||
|
||||
def __getitem__(self, flag_index: int) -> FlagType:
|
||||
return self.flags[flag_index]
|
||||
|
||||
def __bool__(self) -> bool:
|
||||
return bool(self.flags)
|
||||
|
||||
|
||||
class Flags(BaseFlags[Flag]):
|
||||
def get_flag_by_name(self, name: str) -> Flag | None:
|
||||
"""
|
||||
Public. Returns the flag entity by its name or None if not found
|
||||
:param name: the name of the flag to get
|
||||
:return: entity of the flag or None
|
||||
"""
|
||||
return next((flag for flag in self.flags if flag.name == name), None)
|
||||
|
||||
@override
|
||||
def __eq__(self, other: object) -> bool:
|
||||
if not isinstance(other, Flags):
|
||||
return False
|
||||
|
||||
if len(self.flags) != len(other.flags):
|
||||
return False
|
||||
|
||||
flag_pairs: zip[tuple[Flag, Flag]] = zip(self.flags, other.flags)
|
||||
return all(s_flag == o_flag for s_flag, o_flag in flag_pairs)
|
||||
|
||||
def __contains__(self, flag_to_check: object) -> bool:
|
||||
if isinstance(flag_to_check, Flag):
|
||||
for flag in self.flags:
|
||||
if flag == flag_to_check:
|
||||
return True
|
||||
return False
|
||||
else:
|
||||
raise TypeError
|
||||
|
||||
|
||||
class InputFlags(BaseFlags[InputFlag]):
|
||||
def get_flag_by_name(self, name: str) -> InputFlag | None:
|
||||
"""
|
||||
Public. Returns the flag entity by its name or None if not found
|
||||
:param name: the name of the flag to get
|
||||
:return: entity of the flag or None
|
||||
"""
|
||||
return next((flag for flag in self.flags if flag.name == name), None)
|
||||
|
||||
@override
|
||||
def __eq__(self, other: object) -> bool:
|
||||
if not isinstance(other, InputFlags):
|
||||
return False
|
||||
|
||||
if len(self.flags) != len(other.flags):
|
||||
return False
|
||||
|
||||
paired_flags: zip[tuple[InputFlag, InputFlag]] = zip(self.flags, other.flags)
|
||||
|
||||
return all(my_flag == other_flag for my_flag, other_flag in paired_flags)
|
||||
|
||||
def __contains__(self, ingressable_item: object) -> bool:
|
||||
if isinstance(ingressable_item, InputFlag):
|
||||
for flag in self.flags:
|
||||
if flag == ingressable_item:
|
||||
return True
|
||||
return False
|
||||
else:
|
||||
raise TypeError
|
||||
@@ -1,8 +1,8 @@
|
||||
__all__ = ["PossibleValues", "ValidationStatus", "Flag", "InputFlag"]
|
||||
__all__ = ["PossibleValues", "ValidationStatus", "Flag", "InputFlag", "InputFlags", "Flags"]
|
||||
|
||||
from enum import Enum
|
||||
from re import Pattern
|
||||
from typing import Literal, override
|
||||
from typing import Literal, override, TypeVar, Generic, Iterator, Any
|
||||
|
||||
PREFIX_TYPE = Literal["-", "--", "---"]
|
||||
|
||||
@@ -91,7 +91,7 @@ class InputFlag:
|
||||
Public. The entity of the flag of the entered command
|
||||
:param name: the name of the input flag
|
||||
:param prefix: the prefix of the input flag
|
||||
:param value: the value of the input flag
|
||||
:param input_value: the value of the input flag
|
||||
:return: None
|
||||
"""
|
||||
self.name: str = name
|
||||
@@ -122,3 +122,115 @@ class InputFlag:
|
||||
return self.name == other.name
|
||||
else:
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
FlagType = TypeVar("FlagType")
|
||||
|
||||
|
||||
class BaseFlags(Generic[FlagType]):
|
||||
def __init__(self, flags: list[FlagType] | None = None) -> None:
|
||||
"""
|
||||
Public. A model that combines the registered flags
|
||||
:param flags: the flags that will be registered
|
||||
:return: None
|
||||
"""
|
||||
self.flags: list[FlagType] = flags if flags else []
|
||||
|
||||
def add_flag(self, flag: FlagType) -> None:
|
||||
"""
|
||||
Public. Adds a flag to the list of flags
|
||||
:param flag: flag to add
|
||||
:return: None
|
||||
"""
|
||||
self.flags.append(flag)
|
||||
|
||||
def add_flags(self, flags: list[FlagType]) -> None:
|
||||
"""
|
||||
Public. Adds a list of flags to the list of flags
|
||||
:param flags: list of flags to add
|
||||
:return: None
|
||||
"""
|
||||
self.flags.extend(flags)
|
||||
|
||||
def __len__(self) -> int:
|
||||
return len(self.flags)
|
||||
|
||||
def __iter__(self) -> Iterator[FlagType]:
|
||||
return iter(self.flags)
|
||||
|
||||
def __getitem__(self, flag_index: int) -> FlagType:
|
||||
return self.flags[flag_index]
|
||||
|
||||
def __bool__(self) -> bool:
|
||||
return bool(self.flags)
|
||||
|
||||
|
||||
class Flags(BaseFlags[Flag]):
|
||||
def get_flag_by_name(self, name: str) -> Flag | None:
|
||||
"""
|
||||
Public. Returns the flag entity by its name or None if not found
|
||||
:param name: the name of the flag to get
|
||||
:return: entity of the flag or None
|
||||
"""
|
||||
return next((flag for flag in self.flags if flag.name == name), None)
|
||||
|
||||
@override
|
||||
def __eq__(self, other: object) -> bool:
|
||||
if not isinstance(other, Flags):
|
||||
return False
|
||||
|
||||
if len(self.flags) != len(other.flags):
|
||||
return False
|
||||
|
||||
flag_pairs: Iterator[tuple[Flag, Flag]] = zip(self.flags, other.flags)
|
||||
return all(s_flag == o_flag for s_flag, o_flag in flag_pairs)
|
||||
|
||||
def __contains__(self, flag_to_check: object) -> bool:
|
||||
if isinstance(flag_to_check, Flag):
|
||||
for flag in self.flags:
|
||||
if flag == flag_to_check:
|
||||
return True
|
||||
return False
|
||||
else:
|
||||
raise TypeError
|
||||
|
||||
|
||||
class InputFlags(BaseFlags[InputFlag]):
|
||||
def get_flag_by_name(
|
||||
self,
|
||||
name: str,
|
||||
with_status: ValidationStatus | None = None,
|
||||
default: Any = None
|
||||
) -> InputFlag | None:
|
||||
"""
|
||||
Public. Returns the flag entity by its name or None if not found
|
||||
:param default:
|
||||
:param with_status:
|
||||
:param name: the name of the flag to get
|
||||
:return: entity of the flag or None
|
||||
"""
|
||||
if with_status is None:
|
||||
return next((flag for flag in self.flags if flag.name == name), default)
|
||||
else:
|
||||
return next((flag for flag in self.flags if flag.name == name and flag.status == with_status), default)
|
||||
|
||||
@override
|
||||
def __eq__(self, other: object) -> bool:
|
||||
if not isinstance(other, InputFlags):
|
||||
return False
|
||||
|
||||
if len(self.flags) != len(other.flags):
|
||||
return False
|
||||
|
||||
paired_flags: Iterator[tuple[InputFlag, InputFlag]] = zip(self.flags, other.flags)
|
||||
|
||||
return all(my_flag == other_flag for my_flag, other_flag in paired_flags)
|
||||
|
||||
def __contains__(self, ingressable_item: object) -> bool:
|
||||
if isinstance(ingressable_item, InputFlag):
|
||||
for flag in self.flags:
|
||||
if flag == ingressable_item:
|
||||
return True
|
||||
return False
|
||||
else:
|
||||
raise TypeError
|
||||
|
||||
@@ -8,7 +8,7 @@ from argenta.command.exceptions import (
|
||||
RepeatedInputFlagsException,
|
||||
UnprocessedInputFlagException,
|
||||
)
|
||||
from argenta.command.flag.flags.models import Flags, InputFlags
|
||||
from argenta.command import Flags, InputFlags
|
||||
from argenta.command.flag.models import Flag, InputFlag, ValidationStatus
|
||||
|
||||
ParseFlagsResult = tuple[InputFlags, str | None, str | None]
|
||||
|
||||
@@ -2,7 +2,7 @@ __all__ = ["Response"]
|
||||
|
||||
from dishka import Container
|
||||
|
||||
from argenta.command.flag.flags.models import InputFlags
|
||||
from argenta.command import InputFlags
|
||||
from argenta.response.status import ResponseStatus
|
||||
|
||||
EMPTY_INPUT_FLAGS: InputFlags = InputFlags()
|
||||
|
||||
@@ -6,9 +6,8 @@ from typing import Callable
|
||||
from rich.console import Console
|
||||
|
||||
from argenta.app.protocols import HandlerFunc
|
||||
from argenta.command import Command, InputCommand
|
||||
from argenta.command import Command, InputCommand, InputFlags
|
||||
from argenta.command.flag import ValidationStatus
|
||||
from argenta.command.flag.flags import InputFlags
|
||||
from argenta.response import Response, ResponseStatus
|
||||
from argenta.router.command_handler.entity import CommandHandler, CommandHandlers
|
||||
from argenta.router.exceptions import (RepeatedAliasNameException,
|
||||
|
||||
Reference in New Issue
Block a user