diff --git a/pyproject.toml b/pyproject.toml index 1941581..8eb4899 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,6 +30,7 @@ packages = ["src/command_parser"] [tool.ruff] line-length = 72 +builtins = ["_"] [tool.ruff.format] quote-style = "double" diff --git a/src/command_parser/option.py b/src/command_parser/option.py index e93f33f..795e50e 100644 --- a/src/command_parser/option.py +++ b/src/command_parser/option.py @@ -36,12 +36,6 @@ class Option: else: return self.single_char - def short(self) -> str: - """ - Return short version of the option (-h, -V, etc.) - """ - return "-{one}".format(one=self.one()) - def long(self) -> str: """ Return long version of the option (--help=, --version=, etc.) diff --git a/src/command_parser/parser.py b/src/command_parser/parser.py index 5dde734..ae8bc90 100644 --- a/src/command_parser/parser.py +++ b/src/command_parser/parser.py @@ -17,7 +17,7 @@ def parse(line: str, flags: list[Flag], values: list[Value]): _("parsing element {element}".format(element=element)) ) try: - if element[0] != "-": + if element[0] != "-": # don't start with - for flag in flags: if flag.one() in element: flag.apply() @@ -28,19 +28,38 @@ def parse(line: str, flags: list[Flag], values: list[Value]): flag.one(), "" ) for value in values: - if value.one() == element: - value.apply(elements[index + 1]) - index += 2 - elif element[1] != "-": + if value.one() in element: + if value.one() != element[0]: + continue # wait until it's the first one to pick the good associated value (hacky but work) + value.apply(elements.pop(index + 1)) + if len(element) == 1: + index += 1 # element already removed with pop + else: + elements[index] = element.replace( + value.one(), "" + ) + elif element[1] != "-": # start with one - for flag in flags: - if flag.short() == element: + if flag.one() in element: flag.apply() - index += 1 + if len(element) == 2: + index += 1 + else: + elements[index] = element.replace( + flag.one(), "" + ) for value in values: - if value.one() == element: - value.apply(elements[index + 1]) - index += 2 - else: + if value.one() in element: + if value.one() != element[1]: + continue # wait until it's the first one to pick the good associated value (hacky but work) + value.apply(elements.pop(index + 1)) + if len(element) == 2: + index += 1 + else: + elements[index] = element.replace( + value.one(), "" + ) + else: # start with more than one - for flag in flags: if flag.long() == element: flag.apply()