From bca3f4f3470c84e719d5737637b93ec840721e86 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Thu, 21 Apr 2022 14:16:30 +0200 Subject: [PATCH] config.BooleanOption: fix handling argparse options The argparse option now reverse the default value set in configuration file instead of only the default value set on adding the configuration option. --- mylib/config.py | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/mylib/config.py b/mylib/config.py index 597f6c2..2cb6651 100644 --- a/mylib/config.py +++ b/mylib/config.py @@ -78,6 +78,14 @@ class BaseOption: # pylint: disable=too-many-instance-attributes """ Get option value from ConfigParser """ return self.config.config_parser.get(self.section.name, self.name) + @property + def _default_in_config(self): + """ Get option default value considering current value from configuration """ + return ( + self._from_config if self._isset_in_config_file + else self.default + ) + def isset(self): """ Check if option is defined in the loaded configuration file """ return self._isset_in_config_file or self._isset_in_options @@ -243,9 +251,26 @@ class BooleanOption(BaseOption): """ Format value as stored in configuration file """ return super().to_config(value).lower() + @property + def _isset_in_options(self): + """ Check if option is defined in registered arguments parser options """ + return ( + self.config.options + and not self.no_arg + and getattr(self.config.options, self.parser_dest) + ) + + @property + def _from_options(self): + """ Get option from arguments parser options """ + return ( + not self._default_in_config if self._isset_in_options + else self._default_in_config + ) + @property def parser_action(self): - return "store_" + str(bool(not self.default)).lower() + return "store_true" @property def parser_type(self): @@ -258,7 +283,7 @@ class BooleanOption(BaseOption): self.arg if self.arg else '--{0}-{1}-{2}'.format( self.section.name, - 'enable' if not self.default else 'disable', + 'enable' if not self._default_in_config else 'disable', self.name ).lower().replace('_', '-') )