Cmds / Cmd: Add comments and debug logging

Add some comments and a little debug logging to clarify operation.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2025-02-02 14:01:21 +01:00
commit 859bb8000f
2 changed files with 8 additions and 1 deletions

View file

@ -49,5 +49,7 @@ class Cmd(ABC): # export
return
self.child_classes.append(cmd)
# To be overriden 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

View file

@ -15,7 +15,11 @@ from jwutils.log import *
class Cmds: # export
def __instantiate(self, cls):
try:
r = cls()
except Exception as e:
slog(ERR, f'Failed to instantiate command of type {cls}: {e}')
raise
r.cmds = self # TODO: Rename Cmds class to App, "Cmds" isn't very self-explanatory
r.app = self
return r
@ -86,6 +90,7 @@ class Cmds: # export
cmds = [cmd for cmd in self.__cmds if type(cmd) not in subcmds]
subparsers = self.__parser.add_subparsers(title='Available commands', metavar='')
for cmd in cmds:
slog(DEBUG, f'Adding top-level command {cmd} to parser')
self.__add_cmd_to_parser(cmd, subparsers)
async def __run(self, argv=None):