mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-python
synced 2026-01-15 18:03:31 +01:00
Cmd.add_subcommand(): Add support for passing cmd argument as string
Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
3cd7926693
commit
4b8d67b8ee
1 changed files with 12 additions and 2 deletions
|
|
@ -1,9 +1,10 @@
|
|||
from __future__ import annotations
|
||||
from typing import List, Type, Union, TypeVar
|
||||
import abc
|
||||
import argparse
|
||||
from abc import ABC
|
||||
from argparse import ArgumentParser, _SubParsersAction
|
||||
from typing import List, Type, Union, TypeVar
|
||||
import inspect, sys, re
|
||||
|
||||
from jwutils import log
|
||||
|
||||
|
|
@ -32,7 +33,16 @@ class Cmd(ABC): # export
|
|||
r.set_defaults(func=self.run)
|
||||
return r
|
||||
|
||||
def add_subcommands(self, cmd: Union[Type[Cmd], List[Type[Cmd]]]) -> None:
|
||||
def add_subcommands(self, cmd: Union[str, Type[Cmd], List[Type[Cmd]]]) -> None:
|
||||
if isinstance(cmd, str):
|
||||
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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue