cmds.projects.CmdCreateFile: Fix spurious newline and --format help #99

Merged
Jan Lindemann merged 2 commits from jan/fix/20260913-cmds-projects-cmdcreatefile-fix-spurious-newline-and-format-help into master 2026-09-13 22:27:27 +02:00 AGit
3 changed files with 32 additions and 19 deletions

View file

@ -1,6 +1,12 @@
from __future__ import annotations
import sys
from argparse import ArgumentParser, ArgumentTypeError, Namespace
from enum import Enum, auto
from typing import override
from typing import TYPE_CHECKING, override
if TYPE_CHECKING:
from typing import Iterable
from ...lib.log import WARNING, log
from ...lib.version.base import Syntax
@ -15,10 +21,6 @@ def key_value(s: str) -> tuple[str, str]:
raise ArgumentTypeError('Expected KEY=VALUE')
return key, value
# TODO: Put the more elaborate stuff into lib
class Fmt(Enum):
Pyright = auto()
class CmdCreateFile(Cmd): # export
def __jw_required(
@ -48,6 +50,11 @@ class CmdCreateFile(Cmd): # export
ret = [module, *ret]
return ret
@property
def __format_choices(self) -> Iterable[str]:
p = 'render_'
return [name.removeprefix(p) for name in dir(self) if name.startswith(p)]
def __render(
self,
template_name: str,
@ -98,12 +105,12 @@ class CmdCreateFile(Cmd): # export
@override
def add_arguments(self, parser: ArgumentParser) -> None:
super().add_arguments(parser)
format_choices = self.__format_choices
parser.add_argument(
'--format',
help = (
'Output format, for example: '
', '.join([fmt.name.lower() for fmt in Fmt])
)
choices = format_choices,
required = True,
help = 'Output format, one of: ' + ', '.join(format_choices)
)
parser.add_argument(
'--search-path',
@ -130,6 +137,6 @@ class CmdCreateFile(Cmd): # export
@override
async def _run(self, args: Namespace) -> None:
method = getattr(self, 'render_' + args.format, None)
if method is None: # Should be prevented by choices=[] but keeps linter happy
if method is None: # choices already restricts this; keeps the linter happy
raise Exception(f'Unsupported output format {args.format}')
print(method(args.module, args.field))
sys.stdout.write(method(args.module, args.field))

View file

@ -1,5 +1,7 @@
from __future__ import annotations
import sys
from typing import TYPE_CHECKING, override
from .Cmd import Cmd, Parent
@ -74,16 +76,19 @@ class CmdCreatePkgConfig(Cmd): # export
]
)
extra: list[str] = []
if args.cflags is not None:
contents += f'Cflags: {args.cflags}\n'
extra.append(f'Cflags: {args.cflags}')
if args.libflags is not None:
contents += f'Libs: {args.libflags}\n'
extra.append(f'Libs: {args.libflags}')
val = merged.get('requires_run')
if val is not None:
contents += f'Requires: {self.__cleanup_requires(val)}'
extra.append(f'Requires: {self.__cleanup_requires(val)}')
val = merged.get('requires_build')
if val is not None:
contents += (f'Requires.private: {self.__cleanup_requires(val)}')
extra.append(f'Requires.private: {self.__cleanup_requires(val)}')
# not sure what to do with requires_devel
print(contents)
if extra:
contents += '\n'.join(extra)
sys.stdout.write(contents)

View file

@ -332,7 +332,7 @@ List available commands
options:
-h, --help show this help message and exit
============= Running: jw-pkg.py -t ../../../.. --log-level info projects create-file --help
usage: jw-pkg.py projects create-file [-h] [--format FORMAT]
usage: jw-pkg.py projects create-file [-h] --format {pyright,tmpl}
[--search-path SEARCH_PATH]
[--template-name TEMPLATE_NAME]
[--quote] [-f KEY=VALUE]
@ -345,7 +345,8 @@ positional arguments:
options:
-h, --help show this help message and exit
--format FORMAT pyright (default: None)
--format {pyright,tmpl}
Output format, one of: pyright, tmpl
--search-path SEARCH_PATH
Template search path, colon separated (default:
/etc/opt/jw-pkg/templates)