From ffe0cfd41d134da2d286b21a090a42d5a4fd1408 Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Sat, 13 Jun 2026 18:27:59 +0200 Subject: [PATCH] lib.App: Allow _run() without subcommands Overriding the _run() method entirely in App subclasses is currently only possible if the application supports a subcommand structure. Make it possible to use it as an abstraction for a single-command application. Signed-off-by: Jan Lindemann --- src/python/jw/pkg/lib/App.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/python/jw/pkg/lib/App.py b/src/python/jw/pkg/lib/App.py index 2b3e958c..b4320df1 100644 --- a/src/python/jw/pkg/lib/App.py +++ b/src/python/jw/pkg/lib/App.py @@ -199,10 +199,6 @@ class App: # export exit_status = 0 - if not hasattr(self.__args, 'func'): - self.__parser.print_help() - return None - pr = None if self.__args.write_profile is None else cProfile.Profile() if pr is not None: pr.enable() @@ -231,8 +227,14 @@ class App: # export if exit_status: sys.exit(exit_status) - # Run sub-command. Overwrite if you want to do anything before or after + # Do the main work. Tries to run sub-commands by default. Overwrite if you + # want to do something else, for instance if you don't have sub-commands, + # or if want to do anything before and / or after the subcommands. async def _run(self, args: Namespace) -> None | int: + if not hasattr(self.__args, 'func'): + self.__parser.print_help() + return None + # Run sub-command. Overwrite if you want to do anything before or after return await self.args.func(args) def call_async(self, awaitable: Awaitable[T], timeout: float | None = None) -> T: