From 6fa7913830d401a51ac35b35713fe5edf2063a64 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Tue, 6 Jun 2023 17:32:47 +0200 Subject: [PATCH] ukify: use pager for --help 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 | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/ukify/ukify.py b/src/ukify/ukify.py index a9c21601df..1320c46a71 100755 --- a/src/ukify/ukify.py +++ b/src/ukify/ukify.py @@ -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 -- 2.25.1