1
0
mirror of https://github.com/openSUSE/osc.git synced 2025-08-11 17:54:06 +02:00

argparse: Prefer sub-command options over global options

This commit is contained in:
2022-09-29 17:44:52 +02:00
parent b92f9f76ef
commit f7af6329b5

View File

@@ -169,15 +169,16 @@ class Cmdln:
help=help_text, help=help_text,
description=help_desc, description=help_desc,
prog=self.get_subcommand_prog(cmd_name), prog=self.get_subcommand_prog(cmd_name),
formatter_class=HelpFormatter formatter_class=HelpFormatter,
conflict_handler="resolve",
) )
# add hidden copy of global options so they can be used in any place # add hidden copy of global options so they can be used in any place
self.add_global_options(subparser, suppress=True) self.add_global_options(subparser, suppress=True)
# add sub-command options, overriding hidden copies of global options if needed (due to conflict_handler="resolve")
for option_args, option_kwargs in options: for option_args, option_kwargs in options:
try: subparser.add_argument(*option_args, **option_kwargs)
subparser.add_argument(*option_args, **option_kwargs)
except argparse.ArgumentError as e:
print(f"WARNING: Could not add argument '{e.argument_name}' to the '{cmd_name}' sub-command: {e}", file=sys.stderr)
def argparse_error(self, *args, **kwargs): def argparse_error(self, *args, **kwargs):
""" """