cmds.projects.CmdCreateFile: Fix --format help
All checks were successful
CI / Packaging - Kali Linux (pull_request) Successful in 4m42s
CI / Packaging - OpenSUSE Tumbleweed (pull_request) Successful in 4m30s
CI / Packaging test (pull_request) Successful in 0s
CI / Packaging - Kali Linux (push) Successful in 4m17s
CI / Packaging - OpenSUSE Tumbleweed (push) Successful in 4m24s
CI / Packaging test (push) Successful in 0s
All checks were successful
CI / Packaging - Kali Linux (pull_request) Successful in 4m42s
CI / Packaging - OpenSUSE Tumbleweed (pull_request) Successful in 4m30s
CI / Packaging test (pull_request) Successful in 0s
CI / Packaging - Kali Linux (push) Successful in 4m17s
CI / Packaging - OpenSUSE Tumbleweed (push) Successful in 4m24s
CI / Packaging test (push) Successful in 0s
The possible values of --format are defined by an Enum which is never really used as such. Derive them from class introspection instead, i.e. offer all formats that have a corresponding render_<format>() name. Also, turn the option into real argparse-backed choices, and make the argument mandatory, because that reflects the reality of the implementation - there is no default. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
174c6fba16
commit
a197e9c50d
2 changed files with 19 additions and 13 deletions
|
|
@ -1,8 +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
|
||||
|
|
@ -17,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(
|
||||
|
|
@ -50,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,
|
||||
|
|
@ -100,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',
|
||||
|
|
@ -132,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}')
|
||||
sys.stdout.write(method(args.module, args.field))
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue