]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
ukify: use pager for --help
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 6 Jun 2023 15:32:47 +0000 (17:32 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 12 Jun 2023 09:12:02 +0000 (11:12 +0200)
The output is now too long to fit on one page, let's use a pager automatically
like in other places.

The implementation is copied from mkosi, but adjusted to follow what other
systemd tools do.

src/ukify/ukify.py

index a9c21601df9827363ba1a71360953fede2b8f250..1320c46a7186e84d3c4d06d13ed555e4e53cdc4c 100755 (executable)
@@ -33,6 +33,7 @@ import json
 import os
 import pathlib
 import pprint
+import pydoc
 import re
 import shlex
 import shutil
@@ -43,6 +44,7 @@ from typing import (Any,
                     Callable,
                     IO,
                     Optional,
+                    Sequence,
                     Union)
 
 import pefile  # type: ignore
@@ -88,6 +90,15 @@ def guess_efi_arch():
     return efi_arch
 
 
+def page(text: str, enabled: Optional[bool]) -> None:
+    if enabled:
+        # Initialize less options from $SYSTEMD_LESS or provide a suitable fallback.
+        os.environ['LESS'] = os.getenv('SYSTEMD_LESS', 'FRSXMK')
+        pydoc.pager(text)
+    else:
+        print(text)
+
+
 def shell_join(cmd):
     # TODO: drop in favour of shlex.join once shlex.join supports pathlib.Path.
     return ' '.join(shlex.quote(str(x)) for x in cmd)
@@ -1128,10 +1139,23 @@ def config_example():
             yield f'{key} = {value}'
 
 
+class PagerHelpAction(argparse._HelpAction):  # pylint: disable=protected-access
+    def __call__(
+        self,
+        parser: argparse.ArgumentParser,
+        namespace: argparse.Namespace,
+        values: Union[str, Sequence[Any], None] = None,
+        option_string: Optional[str] = None
+    ) -> None:
+        page(parser.format_help(), True)
+        parser.exit()
+
+
 def create_parser():
     p = argparse.ArgumentParser(
         description='Build and sign Unified Kernel Images',
         allow_abbrev=False,
+        add_help=False,
         usage='''\
 ukify [options…] [LINUX INITRD…]
 ''',
@@ -1145,6 +1169,13 @@ ukify [options…] [LINUX INITRD…]
     # Suppress printing of usage synopsis on errors
     p.error = lambda message: p.exit(2, f'{p.prog}: error: {message}\n')
 
+    # Make --help paged
+    p.add_argument(
+        '-h', '--help',
+        action=PagerHelpAction,
+        help='show this help message and exit',
+    )
+
     return p