]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
mkosi: supress printing of argparse help on error 1243/head
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 27 Oct 2022 08:39:11 +0000 (10:39 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 27 Oct 2022 08:53:05 +0000 (10:53 +0200)
We build a precise error message, but then bury it under a wall of text
produced by print_usage(). The printing of help (or some subset of it) on
parsing error is just useless. Most likely the user made a typo in an option,
and printing a few dozen lines (and more in the future) of unhelpful
semi-related information is counterproductive.

I'm surprised that argparse doesn't make this configurable, but looking at the
code, it seems that the behaviour is hardcoded. Docs and stackoverflow also
yield no hints.

mkosi/__init__.py

index 5e9474175eda0477e2521d992f8e8a4ac60da961..2fbd6f3d6b36bf443e01fafdcf2580bb4c030bb5 100644 (file)
@@ -53,6 +53,7 @@ from typing import (
     Iterator,
     List,
     NamedTuple,
+    NoReturn,
     Optional,
     Sequence,
     Set,
@@ -5024,6 +5025,10 @@ class ArgumentParserMkosi(argparse.ArgumentParser):
         # return the modified argument list
         return new_arg_strings
 
+    def error(self, message: str) -> NoReturn:
+        # This is a copy of super's method but with self.print_usage() removed
+        self.exit(2, f'{self.prog}: error: {message}\n')
+
 
 COMPRESSION_ALGORITHMS = "zlib", "lzo", "zstd", "lz4", "xz"