lib.App: Add root command slot #66
1 changed files with 17 additions and 16 deletions
lib.App.add_cmd_to_parser -> .make_sub_parser()
Code beautification: add_cmd_to_parser() isn't very telling about its return type and the fact that it creates an object, hence the name change. Also, annotate its argument with a private argparse type to avoid a cast. My concern that argparse will break the private type at some point in the future is outweighed by the gained clode clarity in this function. Signed-off-by: Jan Lindemann <jan@janware.com>
commit
24e3059d92
|
|
@ -6,7 +6,9 @@ import os
|
||||||
import sys
|
import sys
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser, Namespace
|
from argparse import (
|
||||||
|
ArgumentDefaultsHelpFormatter, ArgumentParser, Namespace, _SubParsersAction
|
||||||
|
)
|
||||||
from typing import TYPE_CHECKING, Any, NamedTuple, cast, override
|
from typing import TYPE_CHECKING, Any, NamedTuple, cast, override
|
||||||
|
|
||||||
from .AsyncRunner import AsyncRunner
|
from .AsyncRunner import AsyncRunner
|
||||||
|
|
@ -151,21 +153,20 @@ class App: # export
|
||||||
|
|
||||||
def _build_parser(self, argv: list[str] | None = None) -> None:
|
def _build_parser(self, argv: list[str] | None = None) -> None:
|
||||||
|
|
||||||
def add_cmd_to_parser(cmd: AbstractCmd, parsers: Any) -> ArgumentParser:
|
def make_sub_parser(
|
||||||
parser = cast(
|
cmd: AbstractCmd, parsers: _SubParsersAction[ArgumentParser]
|
||||||
'ArgumentParser',
|
) -> ArgumentParser:
|
||||||
parsers.add_parser(
|
ret = parsers.add_parser(
|
||||||
cmd.name,
|
cmd.name,
|
||||||
help = cmd.help,
|
help = cmd.help,
|
||||||
description = cmd.description,
|
description = cmd.description,
|
||||||
aliases = cmd.aliases,
|
aliases = cmd.aliases,
|
||||||
formatter_class = ArgumentDefaultsHelpFormatter,
|
formatter_class = ArgumentDefaultsHelpFormatter,
|
||||||
)
|
|
||||||
)
|
)
|
||||||
parser.set_defaults(func = cmd.run)
|
ret.set_defaults(func = cmd.run)
|
||||||
cmd.add_arguments(parser)
|
cmd.add_arguments(ret)
|
||||||
cmd.set_parser(parser)
|
cmd.set_parser(ret)
|
||||||
return parser
|
return ret
|
||||||
|
|
||||||
def add_cmds_to_parser(
|
def add_cmds_to_parser(
|
||||||
parent: AbstractCmd | App,
|
parent: AbstractCmd | App,
|
||||||
|
|
@ -187,7 +188,7 @@ class App: # export
|
||||||
cmd.set_parent(parent)
|
cmd.set_parent(parent)
|
||||||
if cmd.name in scs:
|
if cmd.name in scs:
|
||||||
log(WARNING, f'Duplicate subcommand name: {cmd.name}')
|
log(WARNING, f'Duplicate subcommand name: {cmd.name}')
|
||||||
scs[cmd.name] = _SubCommand(cmd, add_cmd_to_parser(cmd, subparsers))
|
scs[cmd.name] = _SubCommand(cmd, make_sub_parser(cmd, subparsers))
|
||||||
for alias in cmd.aliases:
|
for alias in cmd.aliases:
|
||||||
if alias != cmd.name and alias in scs:
|
if alias != cmd.name and alias in scs:
|
||||||
log(
|
log(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue