diff --git a/.gitignore b/.gitignore index 48c0136..3e452dd 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,11 @@ argenta/router/__pycache__/ argenta/app/__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 poetry.lock tests/__pycache__ @@ -13,3 +18,4 @@ tests/mock_app/handlers/handlers_implementation/__pycache__/ tests/mock_app/handlers/__pycache__/ tests/mock_app/business_logic/__pycache__/ tests/mock_app/__pycache__/ + diff --git a/argenta/app/entity.py b/argenta/app/entity.py index 6e42047..d31447b 100644 --- a/argenta/app/entity.py +++ b/argenta/app/entity.py @@ -1,8 +1,8 @@ from typing import Callable 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 ..command.parse_input_command.exceptions import InvalidInputFlagsException from .exceptions import (InvalidRouterInstanceException, InvalidDescriptionMessagePatternException, OnlyOneMainRouterIsAllowedException, @@ -68,7 +68,7 @@ class App: raw_command: str = input() try: - command: Command = ParseInputCommand(raw_command=raw_command) + command: Command = InputCommand.parse(raw_command=raw_command) except InvalidInputFlagsException: self.print_func(self.line_separate) self.print_func(self.command_group_description_separate) diff --git a/argenta/command/__pycache__/__init__.cpython-313.pyc b/argenta/command/__pycache__/__init__.cpython-313.pyc deleted file mode 100644 index 7a8b8f9..0000000 Binary files a/argenta/command/__pycache__/__init__.cpython-313.pyc and /dev/null differ diff --git a/argenta/command/__pycache__/entity.cpython-313.pyc b/argenta/command/__pycache__/entity.cpython-313.pyc deleted file mode 100644 index e8c89ad..0000000 Binary files a/argenta/command/__pycache__/entity.cpython-313.pyc and /dev/null differ diff --git a/argenta/command/__pycache__/exceptions.cpython-313.pyc b/argenta/command/__pycache__/exceptions.cpython-313.pyc deleted file mode 100644 index b333e71..0000000 Binary files a/argenta/command/__pycache__/exceptions.cpython-313.pyc and /dev/null differ diff --git a/argenta/command/entity.py b/argenta/command/entity.py index 93c17ae..e457a54 100644 --- a/argenta/command/entity.py +++ b/argenta/command/entity.py @@ -13,6 +13,8 @@ class Command: self._description = description self._flags = flags + self._input_flags = None + def get_string_entity(self): return self._command @@ -30,7 +32,6 @@ class Command: self._command = command def validate_commands_params(self): - print(self._flags) if not isinstance(self._command, str): raise InvalidCommandInstanceException(self._command) if not isinstance(self._description, str): diff --git a/argenta/command/parse_input_command/__init__.py b/argenta/command/input_comand/__init__.py similarity index 100% rename from argenta/command/parse_input_command/__init__.py rename to argenta/command/input_comand/__init__.py diff --git a/argenta/command/parse_input_command/entity.py b/argenta/command/input_comand/entity.py similarity index 58% rename from argenta/command/parse_input_command/entity.py rename to argenta/command/input_comand/entity.py index d54000c..4d2ed93 100644 --- a/argenta/command/parse_input_command/entity.py +++ b/argenta/command/input_comand/entity.py @@ -1,19 +1,20 @@ -from typing import Literal, LiteralString -from argenta.command.entity import Command -from argenta.command.params.flag.entity import Flag -from argenta.command.params.flags_group.entity import FlagsGroup -from .exceptions import InvalidInputFlagsException +from argenta.command.input_comand.exceptions import InvalidInputFlagsException +from ..entity import Command +from ..params.flags_group.entity import FlagsGroup +from ..params.input_flag.entity import InputFlag -class ParseInputCommand: - def __new__(cls, *args, **kwargs): - raw_command = kwargs['raw_command'] - return ParseInputCommand.parse(raw_command) +class InputCommand(Command): + def set_input_flags(self, input_flags: list[InputFlag]): + self._input_flags = input_flags + + def get_input_flags(self) -> list[InputFlag]: + return self._input_flags @staticmethod def parse(raw_command: str) -> Command: list_of_tokens = raw_command.split() - command_name = list_of_tokens[0] + command = list_of_tokens[0] list_of_tokens.pop(0) flags = [] @@ -23,33 +24,31 @@ class ParseInputCommand: flag_prefix_last_symbol_index = _.rfind('-') if _.startswith('-'): if current_flag_name or len(_) < 2 or len(_[:flag_prefix_last_symbol_index]) > 3: - raise + raise InvalidInputFlagsException else: current_flag_name = _ else: if not current_flag_name: - raise + raise InvalidInputFlagsException else: current_flag_value = _ if current_flag_name and current_flag_value: flag_prefix = _[: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_value = None - command = Command(command_name, flags) - - return command - - - if len(flags) == 0: return Command(command=command) - elif len(flags) == 1: - return Command(command=command, flags=flags[0]) else: flags = FlagsGroup(flags=flags) return Command(command=command, flags=flags) + + diff --git a/argenta/command/parse_input_command/exceptions.py b/argenta/command/input_comand/exceptions.py similarity index 100% rename from argenta/command/parse_input_command/exceptions.py rename to argenta/command/input_comand/exceptions.py diff --git a/argenta/command/params/__pycache__/__init__.cpython-313.pyc b/argenta/command/params/__pycache__/__init__.cpython-313.pyc deleted file mode 100644 index e7facaa..0000000 Binary files a/argenta/command/params/__pycache__/__init__.cpython-313.pyc and /dev/null differ diff --git a/argenta/command/params/flag/__pycache__/__init__.cpython-313.pyc b/argenta/command/params/flag/__pycache__/__init__.cpython-313.pyc deleted file mode 100644 index 38c8200..0000000 Binary files a/argenta/command/params/flag/__pycache__/__init__.cpython-313.pyc and /dev/null differ diff --git a/argenta/command/params/flag/__pycache__/entity.cpython-313.pyc b/argenta/command/params/flag/__pycache__/entity.cpython-313.pyc deleted file mode 100644 index 0d607b6..0000000 Binary files a/argenta/command/params/flag/__pycache__/entity.cpython-313.pyc and /dev/null differ diff --git a/argenta/command/params/flags_group/__pycache__/__init__.cpython-313.pyc b/argenta/command/params/flags_group/__pycache__/__init__.cpython-313.pyc deleted file mode 100644 index 6c90035..0000000 Binary files a/argenta/command/params/flags_group/__pycache__/__init__.cpython-313.pyc and /dev/null differ diff --git a/argenta/command/params/flags_group/__pycache__/entity.cpython-313.pyc b/argenta/command/params/flags_group/__pycache__/entity.cpython-313.pyc deleted file mode 100644 index 06c8237..0000000 Binary files a/argenta/command/params/flags_group/__pycache__/entity.cpython-313.pyc and /dev/null differ diff --git a/argenta/command/params/input_flag/__init__.py b/argenta/command/params/input_flag/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/argenta/command/params/input_flag/entity.py b/argenta/command/params/input_flag/entity.py new file mode 100644 index 0000000..7ceb968 --- /dev/null +++ b/argenta/command/params/input_flag/entity.py @@ -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 + + diff --git a/argenta/command/parse_input_command/__pycache__/__init__.cpython-313.pyc b/argenta/command/parse_input_command/__pycache__/__init__.cpython-313.pyc deleted file mode 100644 index 3ae01c0..0000000 Binary files a/argenta/command/parse_input_command/__pycache__/__init__.cpython-313.pyc and /dev/null differ diff --git a/argenta/command/parse_input_command/__pycache__/entity.cpython-313.pyc b/argenta/command/parse_input_command/__pycache__/entity.cpython-313.pyc deleted file mode 100644 index 51b6d51..0000000 Binary files a/argenta/command/parse_input_command/__pycache__/entity.cpython-313.pyc and /dev/null differ diff --git a/argenta/command/parse_input_command/__pycache__/exceptions.cpython-313.pyc b/argenta/command/parse_input_command/__pycache__/exceptions.cpython-313.pyc deleted file mode 100644 index f6ee0b8..0000000 Binary files a/argenta/command/parse_input_command/__pycache__/exceptions.cpython-313.pyc and /dev/null differ diff --git a/tests/test.py b/tests/test.py index 39f49a9..4fe3d3c 100644 --- a/tests/test.py +++ b/tests/test.py @@ -1,53 +1,6 @@ -import re -from pprint import pprint +from typing import Literal, LiteralString - -class CommandParseError(Exception): - pass - -def parse_command(command: str) -> dict: - pattern = re.compile(r""" - (?P^\S+) - | - (?P-{1,3})(?P\w+) - | - (?P\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) +x: LiteralString = '---' +m: LiteralString = input() +if m == '-': + x = m