mirror of
ssh://git.janware.com/janware/proj/jw-pkg
synced 2026-04-25 09:35:54 +02:00
jw.pkg.cmds.Cmd: Derive from lib.Cmd
The body of Cmd is pretty much entirely obviated by its base class. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
e39047e0dd
commit
1f26391452
2 changed files with 6 additions and 48 deletions
|
|
@ -78,8 +78,7 @@ class App(Base):
|
|||
async def _run(self, args: argparse.Namespace) -> None:
|
||||
|
||||
self.opt_debug = args.debug
|
||||
if len(args.os):
|
||||
self.opt_os = args.os[0]
|
||||
self.opt_os = args.os
|
||||
|
||||
if args.prefix is not None:
|
||||
self.projs_root = args.prefix
|
||||
|
|
|
|||
|
|
@ -5,54 +5,13 @@ from typing import Type, Union, TypeVar
|
|||
import inspect, abc, argparse
|
||||
from argparse import ArgumentParser
|
||||
|
||||
class Cmd(abc.ABC): # export
|
||||
from ..lib.Cmd import Cmd as Base
|
||||
|
||||
class Cmd(Base): # export
|
||||
|
||||
def __init__(self, name: str, help: str) -> None:
|
||||
super().__init__(name, help)
|
||||
from ..App import App
|
||||
self.name = name
|
||||
self.help = help
|
||||
self.parent = None
|
||||
self.children: list[Cmd] = []
|
||||
self.child_classes: list[Type[Cmd]] = []
|
||||
self.app: App|None = None
|
||||
|
||||
@abc.abstractmethod
|
||||
def _run(self, args):
|
||||
pass
|
||||
|
||||
def run(self, args):
|
||||
async def run(self, args):
|
||||
return self._run(args)
|
||||
|
||||
def add_parser(self, parsers) -> ArgumentParser:
|
||||
r = parsers.add_parser(self.name, help=self.help,
|
||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
||||
r.set_defaults(func=self.run)
|
||||
return r
|
||||
|
||||
def add_subcommands(self, cmd: Union[str, Type[Cmd], list[Type[Cmd]]]) -> None:
|
||||
if isinstance(cmd, str):
|
||||
import sys, re
|
||||
sc = []
|
||||
for name, obj in inspect.getmembers(sys.modules[self.__class__.__module__]):
|
||||
if inspect.isclass(obj):
|
||||
if re.search(cmd, str(obj)):
|
||||
sc.append(obj)
|
||||
log.slog(log.DEBUG, f"Found subcommand {obj}")
|
||||
self.add_subcommands(sc)
|
||||
return
|
||||
if isinstance(cmd, list):
|
||||
for c in cmd:
|
||||
self.add_subcommands(c)
|
||||
return
|
||||
self.child_classes.append(cmd)
|
||||
|
||||
# To be overridden by derived class in case the command does take arguments.
|
||||
# Will be called from App base class constructor and set up the parser hierarchy
|
||||
def add_arguments(self, parser: ArgumentParser) -> None:
|
||||
pass
|
||||
|
||||
def conf_value(self, path, default=None):
|
||||
ret = None if self.app is None else self.app.conf_value(path, default)
|
||||
if ret is None and default is not None:
|
||||
return default
|
||||
return ret
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue