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:
@@ -1,35 +1,40 @@
|
||||
from argenta.command.input_comand.exceptions import InvalidInputFlagsException
|
||||
from ..input_comand.exceptions import IncorrectInputFlagException
|
||||
from ..entity import Command
|
||||
from ..params.flags_group.entity import FlagsGroup
|
||||
from ..params.input_flag.entity import InputFlag
|
||||
from ..params.flag.flags_group.entity import FlagsGroup
|
||||
from ..params.flag.input_flag.entity import InputFlag
|
||||
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
|
||||
class InputCommand(Command):
|
||||
def set_input_flags(self, input_flags: list[InputFlag]):
|
||||
T = TypeVar('T')
|
||||
|
||||
|
||||
class InputCommand(Command, Generic[T]):
|
||||
def set_input_flags(self, input_flags: FlagsGroup):
|
||||
self._input_flags = input_flags
|
||||
|
||||
def get_input_flags(self) -> list[InputFlag]:
|
||||
def get_input_flags(self) -> FlagsGroup:
|
||||
return self._input_flags
|
||||
|
||||
@staticmethod
|
||||
def parse(raw_command: str) -> Command:
|
||||
def parse(raw_command: str) -> 'InputCommand[T]':
|
||||
list_of_tokens = raw_command.split()
|
||||
command = list_of_tokens[0]
|
||||
list_of_tokens.pop(0)
|
||||
|
||||
flags = []
|
||||
flags: FlagsGroup = FlagsGroup()
|
||||
current_flag_name = None
|
||||
current_flag_value = None
|
||||
for _ in list_of_tokens:
|
||||
flag_prefix_last_symbol_index = _.rfind('-')
|
||||
if _.startswith('-'):
|
||||
if current_flag_name or len(_) < 2 or len(_[:flag_prefix_last_symbol_index]) > 3:
|
||||
raise InvalidInputFlagsException
|
||||
raise IncorrectInputFlagException()
|
||||
else:
|
||||
current_flag_name = _
|
||||
else:
|
||||
if not current_flag_name:
|
||||
raise InvalidInputFlagsException
|
||||
raise IncorrectInputFlagException()
|
||||
else:
|
||||
current_flag_value = _
|
||||
if current_flag_name and current_flag_value:
|
||||
@@ -40,15 +45,15 @@ class InputCommand(Command):
|
||||
flag_prefix=flag_prefix)
|
||||
input_flag.set_value(current_flag_value)
|
||||
|
||||
flags.append(input_flag)
|
||||
flags.add_flag(input_flag)
|
||||
|
||||
current_flag_name = None
|
||||
current_flag_value = None
|
||||
|
||||
if len(flags) == 0:
|
||||
return Command(command=command)
|
||||
if len(flags.get_flags()) == 0:
|
||||
return InputCommand(command=command)
|
||||
else:
|
||||
flags = FlagsGroup(flags=flags)
|
||||
return Command(command=command, flags=flags)
|
||||
input_command = InputCommand(command=command)
|
||||
input_command.set_input_flags(flags)
|
||||
return input_command
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user