mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 10:05:28 +03:00
work on v0.3.0
This commit is contained in:
@@ -12,7 +12,7 @@ class Command:
|
||||
flags: Flag | FlagsGroup | None = None):
|
||||
self._command = command
|
||||
self._description = description
|
||||
self._flags = flags
|
||||
self._flags: FlagsGroup | None = flags if isinstance(flags, FlagsGroup) else FlagsGroup([flags]) if isinstance(flags, Flag) else flags
|
||||
|
||||
self._input_flags: InputFlag | FlagsGroup | None = None
|
||||
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -1,4 +1,4 @@
|
||||
from ..input_comand.exceptions import IncorrectInputFlagException
|
||||
from ..input_comand.exceptions import IncorrectInputFlagException, RepeatedInputFlagsException
|
||||
from ..entity import Command
|
||||
from ..params.flag.flags_group.entity import FlagsGroup
|
||||
from ..params.flag.input_flag.entity import InputFlag
|
||||
@@ -46,7 +46,11 @@ class InputCommand(Command, Generic[T]):
|
||||
flag_prefix=flag_prefix)
|
||||
input_flag.set_value(current_flag_value)
|
||||
|
||||
flags.add_flag(input_flag)
|
||||
all_flags = [x.get_string_entity() for x in flags.get_flags()]
|
||||
if input_flag.get_string_entity() not in all_flags:
|
||||
flags.add_flag(input_flag)
|
||||
else:
|
||||
raise RepeatedInputFlagsException(input_flag)
|
||||
|
||||
current_flag_name = None
|
||||
current_flag_value = None
|
||||
|
||||
@@ -13,4 +13,12 @@ class IncorrectInputFlagException(Exception):
|
||||
return "Incorrect Input Flags"
|
||||
|
||||
|
||||
class RepeatedInputFlagsException(Exception):
|
||||
def __init__(self, flag: InputFlag):
|
||||
self.flag = flag
|
||||
def __str__(self):
|
||||
return ("Repeated Input Flags\n"
|
||||
f"Duplicate flag was detected in the input: '{self.flag.get_string_entity()}'")
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -6,20 +6,23 @@ class Flag:
|
||||
flag_prefix: Literal['-', '--', '---'] = '-',
|
||||
ignore_flag_value_register: bool = False,
|
||||
possible_flag_values: list[str] = False):
|
||||
self.flag_name = flag_name
|
||||
self.flag_prefix = flag_prefix
|
||||
self._flag_name = flag_name
|
||||
self._flag_prefix = flag_prefix
|
||||
self.possible_flag_values = possible_flag_values
|
||||
self.ignore_flag_value_register = ignore_flag_value_register
|
||||
|
||||
self._value = None
|
||||
|
||||
def get_string_entity(self):
|
||||
if self.ignore_flag_value_register:
|
||||
string_entity: str = self.flag_prefix + self.flag_name.lower()
|
||||
else:
|
||||
string_entity: str = self.flag_prefix + self.flag_name
|
||||
string_entity: str = self._flag_prefix + self._flag_name
|
||||
return string_entity
|
||||
|
||||
def get_flag_name(self):
|
||||
return self._flag_name
|
||||
|
||||
def get_flag_prefix(self):
|
||||
return self._flag_prefix
|
||||
|
||||
def get_value(self):
|
||||
return self._value
|
||||
|
||||
|
||||
@@ -2,8 +2,9 @@ from typing import Callable, Any
|
||||
from ..command.entity import Command
|
||||
from ..command.input_comand.entity import InputCommand
|
||||
from ..command.input_comand.exceptions import InvalidInputFlagException
|
||||
from ..command.params.flag.flags_group.entity import FlagsGroup
|
||||
from ..router.exceptions import (UnknownCommandHandlerHasAlreadyBeenCreatedException,
|
||||
RepeatedCommandException)
|
||||
RepeatedCommandException, RepeatedFlagNameException)
|
||||
|
||||
|
||||
class Router:
|
||||
@@ -82,6 +83,12 @@ class Router:
|
||||
if command_name.lower() in [x.lower() for x in self.get_all_commands()]:
|
||||
raise RepeatedCommandException()
|
||||
|
||||
flags: FlagsGroup = command.get_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()
|
||||
|
||||
|
||||
def set_router_as_main(self):
|
||||
if self.name == 'subordinate':
|
||||
@@ -125,3 +132,10 @@ class Router:
|
||||
all_commands.append(command_entity['command'].get_string_entity())
|
||||
|
||||
return all_commands
|
||||
|
||||
def get_all_flags(self) -> list[FlagsGroup]:
|
||||
all_flags: list[FlagsGroup] = []
|
||||
for command_entity in self._command_entities:
|
||||
all_flags.append(command_entity['command'].get_flags())
|
||||
|
||||
return all_flags
|
||||
|
||||
@@ -11,3 +11,8 @@ class UnknownCommandHandlerHasAlreadyBeenCreatedException(Exception):
|
||||
class RepeatedCommandException(Exception):
|
||||
def __str__(self):
|
||||
return "Commands in handler cannot be repeated"
|
||||
|
||||
|
||||
class RepeatedFlagNameException(Exception):
|
||||
def __str__(self):
|
||||
return "Repeated flag name in register command"
|
||||
|
||||
@@ -12,11 +12,15 @@ settings_router: Router = Router(title='Settings points:')
|
||||
|
||||
console = Console()
|
||||
|
||||
flagi =FlagsGroup(flags=[
|
||||
Flag(flag_name='host',
|
||||
flag_prefix='--',),
|
||||
Flag(flag_name='port',
|
||||
flag_prefix='--',)
|
||||
])
|
||||
|
||||
@work_router.command(command=Command(command='0', description='Get Help', flags=Flag(flag_name='host',
|
||||
flag_prefix='--',
|
||||
ignore_flag_value_register=True,
|
||||
possible_flag_values=['tester', 'ffmpeg'])))
|
||||
|
||||
@work_router.command(command=Command(command='0', description='Get Help', flags=flagi))
|
||||
def command_help(args: FlagsGroup):
|
||||
flags = args.get_flags()
|
||||
for flag in flags:
|
||||
|
||||
Reference in New Issue
Block a user