mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 18:15:28 +03:00
work on v0.3.0
This commit is contained in:
@@ -3,6 +3,11 @@
|
|||||||
argenta/router/__pycache__/
|
argenta/router/__pycache__/
|
||||||
argenta/app/__pycache__/
|
argenta/app/__pycache__/
|
||||||
argenta/__pycache__/
|
argenta/__pycache__/
|
||||||
|
argenta/command/__pycache__
|
||||||
|
argenta/command/params/__pycache__
|
||||||
|
argenta/command/params/flag/__pycache__
|
||||||
|
argenta/command/params/flags_group/__pycache__
|
||||||
|
argenta/command/params/input_flag/__pycache__
|
||||||
dist
|
dist
|
||||||
poetry.lock
|
poetry.lock
|
||||||
tests/__pycache__
|
tests/__pycache__
|
||||||
@@ -13,3 +18,4 @@ tests/mock_app/handlers/handlers_implementation/__pycache__/
|
|||||||
tests/mock_app/handlers/__pycache__/
|
tests/mock_app/handlers/__pycache__/
|
||||||
tests/mock_app/business_logic/__pycache__/
|
tests/mock_app/business_logic/__pycache__/
|
||||||
tests/mock_app/__pycache__/
|
tests/mock_app/__pycache__/
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
from typing import Callable
|
from typing import Callable
|
||||||
from ..command.entity import Command
|
from ..command.entity import Command
|
||||||
from argenta.command.parse_input_command.entity import ParseInputCommand
|
from argenta.command.input_comand.entity import InputCommand
|
||||||
|
from argenta.command.input_comand.exceptions import InvalidInputFlagsException
|
||||||
from ..router.entity import Router
|
from ..router.entity import Router
|
||||||
from ..command.parse_input_command.exceptions import InvalidInputFlagsException
|
|
||||||
from .exceptions import (InvalidRouterInstanceException,
|
from .exceptions import (InvalidRouterInstanceException,
|
||||||
InvalidDescriptionMessagePatternException,
|
InvalidDescriptionMessagePatternException,
|
||||||
OnlyOneMainRouterIsAllowedException,
|
OnlyOneMainRouterIsAllowedException,
|
||||||
@@ -68,7 +68,7 @@ class App:
|
|||||||
|
|
||||||
raw_command: str = input()
|
raw_command: str = input()
|
||||||
try:
|
try:
|
||||||
command: Command = ParseInputCommand(raw_command=raw_command)
|
command: Command = InputCommand.parse(raw_command=raw_command)
|
||||||
except InvalidInputFlagsException:
|
except InvalidInputFlagsException:
|
||||||
self.print_func(self.line_separate)
|
self.print_func(self.line_separate)
|
||||||
self.print_func(self.command_group_description_separate)
|
self.print_func(self.command_group_description_separate)
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -13,6 +13,8 @@ class Command:
|
|||||||
self._description = description
|
self._description = description
|
||||||
self._flags = flags
|
self._flags = flags
|
||||||
|
|
||||||
|
self._input_flags = None
|
||||||
|
|
||||||
def get_string_entity(self):
|
def get_string_entity(self):
|
||||||
return self._command
|
return self._command
|
||||||
|
|
||||||
@@ -30,7 +32,6 @@ class Command:
|
|||||||
self._command = command
|
self._command = command
|
||||||
|
|
||||||
def validate_commands_params(self):
|
def validate_commands_params(self):
|
||||||
print(self._flags)
|
|
||||||
if not isinstance(self._command, str):
|
if not isinstance(self._command, str):
|
||||||
raise InvalidCommandInstanceException(self._command)
|
raise InvalidCommandInstanceException(self._command)
|
||||||
if not isinstance(self._description, str):
|
if not isinstance(self._description, str):
|
||||||
|
|||||||
+20
-21
@@ -1,19 +1,20 @@
|
|||||||
from typing import Literal, LiteralString
|
from argenta.command.input_comand.exceptions import InvalidInputFlagsException
|
||||||
from argenta.command.entity import Command
|
from ..entity import Command
|
||||||
from argenta.command.params.flag.entity import Flag
|
from ..params.flags_group.entity import FlagsGroup
|
||||||
from argenta.command.params.flags_group.entity import FlagsGroup
|
from ..params.input_flag.entity import InputFlag
|
||||||
from .exceptions import InvalidInputFlagsException
|
|
||||||
|
|
||||||
|
|
||||||
class ParseInputCommand:
|
class InputCommand(Command):
|
||||||
def __new__(cls, *args, **kwargs):
|
def set_input_flags(self, input_flags: list[InputFlag]):
|
||||||
raw_command = kwargs['raw_command']
|
self._input_flags = input_flags
|
||||||
return ParseInputCommand.parse(raw_command)
|
|
||||||
|
def get_input_flags(self) -> list[InputFlag]:
|
||||||
|
return self._input_flags
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def parse(raw_command: str) -> Command:
|
def parse(raw_command: str) -> Command:
|
||||||
list_of_tokens = raw_command.split()
|
list_of_tokens = raw_command.split()
|
||||||
command_name = list_of_tokens[0]
|
command = list_of_tokens[0]
|
||||||
list_of_tokens.pop(0)
|
list_of_tokens.pop(0)
|
||||||
|
|
||||||
flags = []
|
flags = []
|
||||||
@@ -23,33 +24,31 @@ class ParseInputCommand:
|
|||||||
flag_prefix_last_symbol_index = _.rfind('-')
|
flag_prefix_last_symbol_index = _.rfind('-')
|
||||||
if _.startswith('-'):
|
if _.startswith('-'):
|
||||||
if current_flag_name or len(_) < 2 or len(_[:flag_prefix_last_symbol_index]) > 3:
|
if current_flag_name or len(_) < 2 or len(_[:flag_prefix_last_symbol_index]) > 3:
|
||||||
raise
|
raise InvalidInputFlagsException
|
||||||
else:
|
else:
|
||||||
current_flag_name = _
|
current_flag_name = _
|
||||||
else:
|
else:
|
||||||
if not current_flag_name:
|
if not current_flag_name:
|
||||||
raise
|
raise InvalidInputFlagsException
|
||||||
else:
|
else:
|
||||||
current_flag_value = _
|
current_flag_value = _
|
||||||
if current_flag_name and current_flag_value:
|
if current_flag_name and current_flag_value:
|
||||||
flag_prefix = _[:flag_prefix_last_symbol_index]
|
flag_prefix = _[:flag_prefix_last_symbol_index]
|
||||||
flag_name = _[flag_prefix_last_symbol_index:]
|
flag_name = _[flag_prefix_last_symbol_index:]
|
||||||
|
|
||||||
flags.append(Flag(flag_name=flag_name, flag_prefix=flag_prefix))
|
input_flag = InputFlag(flag_name=flag_name,
|
||||||
|
flag_prefix=flag_prefix)
|
||||||
|
input_flag.set_value(current_flag_value)
|
||||||
|
|
||||||
|
flags.append(input_flag)
|
||||||
|
|
||||||
current_flag_name = None
|
current_flag_name = None
|
||||||
current_flag_value = None
|
current_flag_value = None
|
||||||
|
|
||||||
command = Command(command_name, flags)
|
|
||||||
|
|
||||||
return command
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if len(flags) == 0:
|
if len(flags) == 0:
|
||||||
return Command(command=command)
|
return Command(command=command)
|
||||||
elif len(flags) == 1:
|
|
||||||
return Command(command=command, flags=flags[0])
|
|
||||||
else:
|
else:
|
||||||
flags = FlagsGroup(flags=flags)
|
flags = FlagsGroup(flags=flags)
|
||||||
return Command(command=command, flags=flags)
|
return Command(command=command, flags=flags)
|
||||||
|
|
||||||
|
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,11 @@
|
|||||||
|
from ...params.flag.entity import Flag
|
||||||
|
|
||||||
|
|
||||||
|
class InputFlag(Flag):
|
||||||
|
def set_value(self, value: str):
|
||||||
|
self._value = value
|
||||||
|
|
||||||
|
def get_value(self) -> str:
|
||||||
|
return self._value
|
||||||
|
|
||||||
|
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
+5
-52
@@ -1,53 +1,6 @@
|
|||||||
import re
|
from typing import Literal, LiteralString
|
||||||
from pprint import pprint
|
|
||||||
|
|
||||||
|
x: LiteralString = '---'
|
||||||
class CommandParseError(Exception):
|
m: LiteralString = input()
|
||||||
pass
|
if m == '-':
|
||||||
|
x = m
|
||||||
def parse_command(command: str) -> dict:
|
|
||||||
pattern = re.compile(r"""
|
|
||||||
(?P<command>^\S+)
|
|
||||||
|
|
|
||||||
(?P<arg_prefix>-{1,3})(?P<arg>\w+)
|
|
||||||
|
|
|
||||||
(?P<value>\S+)
|
|
||||||
""", re.VERBOSE)
|
|
||||||
|
|
||||||
tokens = pattern.findall(command)
|
|
||||||
if not tokens:
|
|
||||||
raise CommandParseError("Invalid command format")
|
|
||||||
|
|
||||||
result = {}
|
|
||||||
args = {}
|
|
||||||
current_arg = None
|
|
||||||
|
|
||||||
for token in tokens:
|
|
||||||
command_name, arg_prefix, arg_name, value = token
|
|
||||||
|
|
||||||
if command_name:
|
|
||||||
if "command" in result:
|
|
||||||
raise CommandParseError("Multiple command names found")
|
|
||||||
result["command"] = command_name
|
|
||||||
|
|
||||||
elif arg_name:
|
|
||||||
if current_arg:
|
|
||||||
raise CommandParseError(f"Argument {current_arg} has no value")
|
|
||||||
current_arg = f"{arg_prefix}{arg_name}"
|
|
||||||
|
|
||||||
elif value:
|
|
||||||
if not current_arg:
|
|
||||||
raise CommandParseError(f"Unexpected value: {value}")
|
|
||||||
args[current_arg] = value
|
|
||||||
current_arg = None
|
|
||||||
|
|
||||||
if current_arg:
|
|
||||||
raise CommandParseError(f"Argument {current_arg} has no value")
|
|
||||||
|
|
||||||
result["args"] = args
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
command_str = "run"
|
|
||||||
parsed = (command_str)
|
|
||||||
pprint(parsed)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user