From d2a1deb3927cd3103c0f9903df85c349c4f18872 Mon Sep 17 00:00:00 2001
From: linarphy <linarphy@linarphy.net>
Date: Tue, 18 Jun 2024 16:23:07 +0200
Subject: [PATCH] Fix parsing & remove useless short version

---
 pyproject.toml               |  1 +
 src/command_parser/option.py |  6 ------
 src/command_parser/parser.py | 41 ++++++++++++++++++++++++++----------
 3 files changed, 31 insertions(+), 17 deletions(-)

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()