jw-pkg.sh: Print help for missing subcommands

Print a help message if no subcommand is specified for one of the
comamnds "distro" and "projects".

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-02-23 07:19:45 +01:00
commit c8036ad216
4 changed files with 16 additions and 2 deletions

View file

@ -43,4 +43,6 @@ class CmdDistro(CmdBase): # export
return self.__interactive
async def _run(self, args):
raise Exception("Running with args", args)
# Missing subcommand
self.parser.print_help()
sys.exit(1)

View file

@ -16,4 +16,6 @@ class CmdProjects(CmdBase): # export
super().add_arguments(p)
async def _run(self, args):
raise Exception("Running with args", args)
# Missing subcommand
self.parser.print_help()
sys.exit(1)

View file

@ -22,6 +22,7 @@ class App: # export
parser = parsers.add_parser(cmd.name, help=cmd.help, formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.set_defaults(func=cmd.run)
cmd.add_arguments(parser)
cmd.set_parser(parser)
return parser
def add_cmds_to_parser(parent, parser, cmds, all=False):

View file

@ -20,6 +20,7 @@ class Cmd(abc.ABC): # export
self.__help = help
self.__children: list[Cmd] = []
self.__child_classes: list[type[Cmd]] = []
self.__parser: ArgumentParser|None = None
@abc.abstractmethod
async def _run(self, args) -> None:
@ -50,6 +51,14 @@ class Cmd(abc.ABC): # export
parent = parent.__parent
return self.__app
# Don't use a setter decorator to force using a grepable method
def set_parser(self, parser: ArgumentParser):
self.__parser = parser
@property
def parser(self) -> str:
return self.__parser
@property
def name(self) -> str:
return self.__name