From 3a43b8d07db334f754c65c4d1a44833fa60d5857 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Tue, 10 Jan 2023 10:51:48 +0100 Subject: [PATCH] Config.parse_arguments_options(): add hardcoded_options argument --- mylib/config.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/mylib/config.py b/mylib/config.py index 688c007..c7dbfba 100644 --- a/mylib/config.py +++ b/mylib/config.py @@ -847,7 +847,8 @@ class Config: # pylint: disable=too-many-instance-attributes return self.options_parser def parse_arguments_options(self, argv=None, parser=None, create=True, ask_values=True, - exit_after_created=True, execute_callback=True): + exit_after_created=True, execute_callback=True, + hardcoded_options=None): """ Parse arguments options @@ -862,6 +863,11 @@ class Config: # pylint: disable=too-many-instance-attributes creation (default: True) :param execute_callback: Sections's loaded callbacks will be executed only if True (default: True) + :param hardcoded_options: Optional hard-coded options to set after loading arguments + and configuration file. These options are passed using an + list of tuple of 3 elements: the section and the option + names and the value. + [('section1', 'option1', value), ...] """ parser = parser if parser else self.get_arguments_parser() argcomplete.autocomplete(parser) @@ -899,6 +905,17 @@ class Config: # pylint: disable=too-many-instance-attributes elif options.verbose: logging.getLogger().setLevel(logging.INFO) + if hardcoded_options: + assert isinstance(hardcoded_options, list), ( + 'hardcoded_options must be a list of tuple of 3 elements: ' + 'the section and the option names and the value.') + for opt_info in hardcoded_options: + assert isinstance(opt_info, tuple) and len(opt_info) == 3, ( + 'Invalid hard-coded option value: it must be a tuple of 3 ' + 'elements: the section and the option names and the value.' + ) + self.set(*opt_info) + if self.get('console', 'enabled'): stdout_console_handler = logging.StreamHandler( sys.stderr if self.get('console', 'force_stderr')