mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 10:05:28 +03:00
fix
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -30,10 +30,11 @@ 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):
|
||||
raise InvalidDescriptionInstanceException()
|
||||
if not (isinstance(self._flags, Flag) or isinstance(self._flags, FlagsGroup)):
|
||||
if not (isinstance(self._flags, Flag) or not isinstance(self._flags, FlagsGroup)) or not self._flags is None:
|
||||
raise InvalidFlagsInstanceException
|
||||
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -12,22 +12,39 @@ class ParseInputCommand:
|
||||
|
||||
@staticmethod
|
||||
def parse(raw_command: str) -> Command:
|
||||
list_of__ = raw_command.split()
|
||||
command = list_of__[0]
|
||||
list_of__.pop(0)
|
||||
list_of_tokens = raw_command.split()
|
||||
command_name = list_of_tokens[0]
|
||||
list_of_tokens.pop(0)
|
||||
|
||||
flags = []
|
||||
for k, _ in enumerate(list_of__):
|
||||
if not _.startswith('-') or len( _[:len(_.lstrip('-'))]) > 3:
|
||||
raise InvalidInputFlagsException()
|
||||
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
|
||||
else:
|
||||
flag_name: str = _.lstrip('-')
|
||||
flag_prefix = _[:len(flag_name)]
|
||||
current_flag_name = _
|
||||
else:
|
||||
if not current_flag_name:
|
||||
raise
|
||||
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))
|
||||
|
||||
current_flag_name = None
|
||||
current_flag_value = None
|
||||
|
||||
command = Command(command_name, flags)
|
||||
|
||||
return command
|
||||
|
||||
parse_flag = Flag(flag_name=flag_name,
|
||||
flag_prefix=flag_prefix)
|
||||
|
||||
flags.append(parse_flag)
|
||||
|
||||
if len(flags) == 0:
|
||||
return Command(command=command)
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import re
|
||||
from pprint import pprint
|
||||
|
||||
|
||||
class CommandParseError(Exception):
|
||||
pass
|
||||
|
||||
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