From ee332c9d70824ca027cd603aa175aa9e1c09327b Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Mon, 17 Aug 2026 09:07:29 +0200 Subject: [PATCH] lib.App: Fix --help with required top-level args _build_parser() does an early parse_known_args() to configure logging from the command line, but that call also enforces the top-level required arguments. --help is registered only after the early parse so the subcommands are present in the rendered help, so an app with a required top-level positional, such as a root Cmd that takes a config-file, exits with "required: config-file" on --help before the final parse_args() in __run() can handle it. Skip the early parse when help or shell completion is requested (the add_all_parsers flag, already set for -h, --help, and argcomplete). The subcommands and --help are still registered, and the final parse_args() shows the help without enforcing the required arguments. The log-flag configuration and the running-command debug line move inside the guard; neither help nor completion logs, so they do not need them. Assisted-by: pi Signed-off-by: Jan Lindemann --- src/python/jw/pkg/lib/App.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/python/jw/pkg/lib/App.py b/src/python/jw/pkg/lib/App.py index ebd3fd6d..a778f73c 100644 --- a/src/python/jw/pkg/lib/App.py +++ b/src/python/jw/pkg/lib/App.py @@ -229,11 +229,16 @@ class App: # export ) self._add_arguments(self.__parser) - args, _ = self.__parser.parse_known_args(argv) - set_log_flags(args.log_flags) - set_log_level(args.log_level) - - log(DEBUG, f'-------------- Running: >{pretty_cmd(cmdline)}<') + if not add_all_parsers: + # Parse known args and configure logging, but only if we're not on + # an interactive usage mission. The problem with parse_known_args() + # is that it fails over missing but required arguments, and we + # don't want that to happen and never get to the point where + # '--help' is registered. + args, _ = self.__parser.parse_known_args(argv) + set_log_flags(args.log_flags) + set_log_level(args.log_level) + log(DEBUG, f'-------------- Running: >{pretty_cmd(cmdline)}<') add_cmds_to_parser(self, self.__parser, self.__cmds, all = add_all_parsers)