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.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2023-06-12 11:12:02 +02:00
parent a3b227d28a
commit 6fa7913830
+31
View File
@@ -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