jw-python/tools/python/jwutils/Cmd.py
Jan Lindemann 33d3b7a7b3 jwutils.Cmds: Cmd.add_subcommands() fails for multiple commands
jwutils.Cmd.add_subcommands() had two issues. First, it failed for
more than one command, and with that fixed, it only added the first
command of a list of commands passed as an argument. This commit
fixes both issues and leaves some logging and cleanup in place, which
was used during debugging.

Signed-off-by: Jan Lindemann <jan@janware.com>
2020-04-07 09:23:13 +02:00

42 lines
1,021 B
Python

import abc
import argparse
from jwutils import log
# compatible with Python 2 *and* 3
ABC = abc.ABCMeta('ABC', (object,), {'__slots__': ()})
# full blown example of one level of nested subcommands
# git -C project remote -v show -n myremote
class Cmd(ABC): # export
@abc.abstractmethod
async def run(self, args):
pass
def __init__(self, name, help):
self.name = name
self.help = help
self.parent = None
self.children = []
self.child_classes = []
async def _run(self, args):
pass
def add_parser(self, parsers):
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):
if isinstance(cmd, list):
for c in cmd:
self.add_subcommands(c)
return
self.child_classes.append(cmd)
def add_arguments(self, parser):
pass