]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Directly ignore deprecated command-line options
authorGeorges Discry <georges@discry.be>
Fri, 22 Sep 2023 13:35:09 +0000 (15:35 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 25 Sep 2023 11:10:28 +0000 (13:10 +0200)
A new argparse action is used for the deprecated command-line options.
There is no more need to post-process the parsed arguments as the action
directly takes care of displaying a deprecation warning while leaving
the namespace untouched.

A deprecated option is now obvious at its declaration (at the
`parser.add_argument()` call) and it can be dropped by simply deleting
that declaration.

mkosi/config.py

index cdbec18884953d42ce0a93d8fecec7da51036d94..7c9f81872b05e00c70dd3a69cd740d4d0e7688d9 100644 (file)
@@ -573,6 +573,29 @@ def parse_chdir(path: str) -> Optional[Path]:
     return Path.cwd()
 
 
+class IgnoreAction(argparse.Action):
+    """Argparse action for deprecated options that can be ignored."""
+
+    def __init__(
+        self,
+        option_strings: Sequence[str],
+        dest: str,
+        nargs: Union[int, str, None] = None,
+        default: Any = argparse.SUPPRESS,
+        help: Optional[str] = argparse.SUPPRESS,
+    ) -> None:
+        super().__init__(option_strings, dest, nargs=nargs, default=default, help=help)
+
+    def __call__(
+        self,
+        parser: argparse.ArgumentParser,
+        namespace: argparse.Namespace,
+        values: Union[str, Sequence[Any], None],
+        option_string: Optional[str] = None
+    ) -> None:
+        logging.warning(f"{option_string} is no longer supported")
+
+
 def config_make_action(settings: Sequence[MkosiConfigSetting]) -> type[argparse.Action]:
     lookup = {s.dest: s for s in settings}
 
@@ -1814,19 +1837,20 @@ def create_argument_parser() -> argparse.ArgumentParser:
         default=DocFormat.auto,
         type=DocFormat,
     )
+    # These can be removed once mkosi v15 is available in LTS distros and compatibility with <= v14
+    # is no longer needed in build infrastructure (e.g.: OBS).
     parser.add_argument(
         "--nspawn-keep-unit",
-        action="store_true",
-        help=argparse.SUPPRESS,
+        nargs=0,
+        action=IgnoreAction,
     )
     parser.add_argument(
         "--default",
-        help=argparse.SUPPRESS,
+        action=IgnoreAction,
     )
     parser.add_argument(
         "--cache",
-        metavar="PATH",
-        help=argparse.SUPPRESS,
+        action=IgnoreAction,
     )
 
     parser.add_argument(
@@ -1878,22 +1902,6 @@ def create_argument_parser() -> argparse.ArgumentParser:
     return parser
 
 
-def backward_compat_stubs(namespace: argparse.Namespace) -> None:
-    # These can be removed once mkosi v15 is available in LTS distros and compatibility with <= v14
-    # is no longer needed in build infrastructure (e.g.: OBS).
-    if getattr(namespace, "nspawn_keep_unit", None):
-        delattr(namespace, "nspawn_keep_unit")
-        logging.warning("--nspawn-keep-unit is no longer supported")
-
-    if getattr(namespace, "default", None):
-        delattr(namespace, "default")
-        logging.warning("--default is no longer supported")
-
-    if getattr(namespace, "cache", None):
-        delattr(namespace, "cache")
-        logging.warning("--cache is no longer supported")
-
-
 def resolve_deps(presets: Sequence[MkosiConfig], include: Sequence[str]) -> list[MkosiConfig]:
     graph = {p.preset: p.dependencies for p in presets}
 
@@ -2113,10 +2121,6 @@ def parse_config(argv: Sequence[str] = ()) -> tuple[MkosiArgs, tuple[MkosiConfig
     if not presets:
         die("No presets defined in mkosi.presets/")
 
-    # Manipulate some old settings to make them work with the new settings, for those typically used in
-    # infrastructure scripts rather than image-specific configuration.
-    backward_compat_stubs(namespace)
-
     presets = [load_config(ns) for ns in presets]
     presets = resolve_deps(presets, include)