From: Zbigniew Jędrzejewski-Szmek Date: Tue, 6 Jun 2023 15:32:47 +0000 (+0200) Subject: ukify: use pager for --help X-Git-Tag: v254-rc1~207^2~10 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=6fa7913830d401a51ac35b35713fe5edf2063a64;p=thirdparty%2Fsystemd.git 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. --- diff --git a/src/ukify/ukify.py b/src/ukify/ukify.py index a9c21601df9..1320c46a718 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