]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Precompute long options
authorGeorges Discry <georges@discry.be>
Fri, 15 Sep 2023 19:18:51 +0000 (21:18 +0200)
committerJörg Behrmann <behrmann@physik.fu-berlin.de>
Sun, 17 Sep 2023 17:13:21 +0000 (19:13 +0200)
Like the `name` attribute used for the INI files, precompute the `long`
attribute used by argparse when it is derived from a setting's `dest`.

mkosi/config.py

index 1da591e02a73319c0c0b720420fa6f64ef61a69c..f5c61e08bc991505abec328efada913536c62cec 100644 (file)
@@ -504,7 +504,7 @@ class MkosiConfigSetting:
 
     # settings for argparse
     short: Optional[str] = None
-    long: Optional[str] = None
+    long: str = ""
     choices: Optional[Any] = None
     metavar: Optional[str] = None
     nargs: Optional[str] = None
@@ -514,6 +514,8 @@ class MkosiConfigSetting:
     def __post_init__(self) -> None:
         if not self.name:
             object.__setattr__(self, 'name', ''.join(x.capitalize() for x in self.dest.split('_') if x))
+        if not self.long:
+            object.__setattr__(self, "long", f"--{self.dest.replace('_', '-')}")
 
 
 @dataclasses.dataclass(frozen=True)
@@ -1815,8 +1817,7 @@ def create_argument_parser(*, settings: bool) -> argparse.ArgumentParser:
             group = parser.add_argument_group(f"{s.section} configuration options")
             last_section = s.section
 
-        long = s.long if s.long else f"--{s.dest.replace('_', '-')}"
-        opts = [s.short, long] if s.short else [long]
+        opts = [s.short, s.long] if s.short else [s.long]
 
         group.add_argument(    # type: ignore
             *opts,