From e9477b15664e5666c6e0082e1ae581f16c1d9dd6 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Mon, 9 Jan 2023 13:34:07 +0100 Subject: [PATCH] config: add optional --reconfigure parameter --- mylib/config.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/mylib/config.py b/mylib/config.py index 387ad96..3844add 100644 --- a/mylib/config.py +++ b/mylib/config.py @@ -774,7 +774,7 @@ class Config: # pylint: disable=too-many-instance-attributes """ Get ordered list of section names """ return sorted(self.sections.keys(), key=lambda section: self.sections[section].order) - def get_arguments_parser(self, **kwargs): + def get_arguments_parser(self, reconfigure=False, **kwargs): """ Get arguments parser """ if self.options_parser: return self.options_parser @@ -807,6 +807,14 @@ class Config: # pylint: disable=too-many-instance-attributes help='Save current configuration to file', ) + if reconfigure: + self.options_parser.add_argument( + '--reconfigure', + action='store_true', + dest='mylib_config_reconfigure', + help='Reconfigure and update configuration file', + ) + self.options_parser.add_argument( '-d', '--debug', @@ -912,6 +920,11 @@ class Config: # pylint: disable=too-many-instance-attributes if execute_callback: self._loaded() + if self.get_option('mylib_config_reconfigure', default=False): + if self.ask_values(set_it=True) and self.save(): + sys.exit(0) + sys.exit(1) + return options def load_options(self, options, execute_callback=True):