1
0
mirror of https://github.com/openSUSE/osc.git synced 2024-12-26 18:06:13 +01:00

Add global options to subcommands so they can be specified in any place

This commit is contained in:
Daniel Mach 2022-08-26 16:23:48 +02:00
parent 99937100df
commit b5491911ea
2 changed files with 83 additions and 28 deletions

View File

@ -117,6 +117,7 @@ class Cmdln:
)
self.pre_argparse()
self.add_global_options(self.argparser)
# map command name to `do_*` function that runs the command
self.cmd_map = {}
@ -169,6 +170,8 @@ class Cmdln:
prog=self.get_subcommand_prog(cmd_name),
formatter_class=HelpFormatter
)
# add hidden copy of global options so they can be used in any place
self.add_global_options(subparser, suppress=True)
for option_args, option_kwargs in options:
subparser.add_argument(*option_args, **option_kwargs)
@ -190,6 +193,12 @@ class Cmdln:
"""
pass
def add_global_options(self, parser, suppress=False):
"""
Add options to the main argument parser and all subparsers.
"""
pass
def post_argparse(self):
"""
Hook method executed after `.main()` calls `parse_args()`.

View File

@ -3,6 +3,7 @@
# and distributed under the terms of the GNU General Public Licence,
# either version 2, or version 3 (at your option).
import argparse
import importlib.util
import inspect
import os
@ -69,35 +70,80 @@ class Osc(cmdln.Cmdln):
return project.replace(conf.config['project_separator'], ':')
return project
def pre_argparse(self):
"""Add global options to the parser (options that are not specific to any subcommand)"""
def add_global_options(self, parser, suppress=False):
optparser = self.argparser
optparser.add_argument('--debugger', action='store_true',
help='jump into the debugger before executing anything')
optparser.add_argument('--post-mortem', action='store_true',
help='jump into the debugger in case of errors')
optparser.add_argument('-t', '--traceback', action='store_true',
help='print call trace in case of errors')
optparser.add_argument('-H', '--http-debug', action='store_true',
help='debug HTTP traffic (filters some headers)')
optparser.add_argument('--http-full-debug', action='store_true',
help='debug HTTP traffic (filters no headers)')
optparser.add_argument('-d', '--debug', action='store_true',
help='print info useful for debugging')
optparser.add_argument('-A', '--apiurl', dest='apiurl',
metavar='URL/alias',
help='specify URL to access API server at or an alias')
optparser.add_argument('-c', '--config', dest='conffile',
metavar='FILE',
help='specify alternate configuration file')
optparser.add_argument('--no-keyring', action='store_true',
help='disable usage of desktop keyring system')
verbose_group = optparser.add_mutually_exclusive_group()
verbose_group.add_argument('-v', '--verbose', action='store_true',
help='increase verbosity')
verbose_group.add_argument('-q', '--quiet', action='store_true',
help='be quiet, not verbose')
def _add_parser_arguments_from_data(argument_parser, data):
for kwargs in data:
args = kwargs.pop("names")
if suppress:
kwargs["help"] = argparse.SUPPRESS
kwargs["default"] = argparse.SUPPRESS
argument_parser.add_argument(*args, **kwargs)
arguments = []
arguments.append(dict(
names=['--debugger'],
action='store_true',
help='jump into the debugger before executing anything',
))
arguments.append(dict(
names=['--post-mortem'],
action='store_true',
help='jump into the debugger in case of errors',
))
arguments.append(dict(
names=['--traceback'],
action='store_true',
help='print call trace in case of errors',
))
arguments.append(dict(
names=['-H', '--http-debug'],
action='store_true',
help='debug HTTP traffic (filters some headers)',
))
arguments.append(dict(
names=['--http-full-debug'],
action='store_true',
help='debug HTTP traffic (filters no headers)',
))
arguments.append(dict(
names=['--debug'],
action='store_true',
help='print info useful for debugging',
))
arguments.append(dict(
names=['-A', '--apiurl'],
metavar='URL/alias',
help='specify URL to access API server at or an alias',
))
arguments.append(dict(
names=['--config'],
dest='conffile',
metavar='FILE',
help='specify alternate configuration file',
))
arguments.append(dict(
names=['--no-keyring'],
action='store_true',
help='disable usage of desktop keyring system',
))
_add_parser_arguments_from_data(parser, arguments)
verbose_group = parser.add_mutually_exclusive_group()
verbose_group_arguments = []
verbose_group_arguments.append(dict(
names=['-v', '--verbose'],
action='store_true',
help='increase verbosity',
))
verbose_group_arguments.append(dict(
names=['-q', '--quiet'],
action='store_true',
help='be quiet, not verbose',
))
_add_parser_arguments_from_data(verbose_group, verbose_group_arguments)
def post_argparse(self):
"""merge commandline options into the config"""