diff options
author | Chris St. Pierre <chris.a.st.pierre@gmail.com> | 2014-03-19 15:27:36 -0400 |
---|---|---|
committer | Chris St. Pierre <chris.a.st.pierre@gmail.com> | 2014-03-19 15:27:36 -0400 |
commit | de02045a38f13035bb7e372d1ba1d1241e2ea4f0 (patch) | |
tree | 210a554e06f2ece73a2cb3929d5a4a341eb576f3 | |
parent | 8b1d8d83ef6afd408c8d3777f52111919cb31253 (diff) | |
parent | 2e537cbc66486e3adcc550b19e496e723d61a0b6 (diff) | |
download | bcfg2-de02045a38f13035bb7e372d1ba1d1241e2ea4f0.tar.gz bcfg2-de02045a38f13035bb7e372d1ba1d1241e2ea4f0.tar.bz2 bcfg2-de02045a38f13035bb7e372d1ba1d1241e2ea4f0.zip |
Merge pull request #159 from fennm/fix-config-file-options-getting-default
Fix config file options getting default
-rw-r--r-- | src/lib/Bcfg2/Options/Parser.py | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/src/lib/Bcfg2/Options/Parser.py b/src/lib/Bcfg2/Options/Parser.py index f05205143..48f3c5056 100644 --- a/src/lib/Bcfg2/Options/Parser.py +++ b/src/lib/Bcfg2/Options/Parser.py @@ -126,7 +126,7 @@ class Parser(argparse.ArgumentParser): if hasattr(component, "options"): self.add_options(getattr(component, "options")) - def _set_defaults(self): + def _set_defaults_from_config(self): """ Set defaults from the config file for all options that can come from the config file, but haven't yet had their default set """ @@ -181,7 +181,7 @@ class Parser(argparse.ArgumentParser): self._reset_namespace() self._cfp.read([cfile]) self._defaults_set = [] - self._set_defaults() + self._set_defaults_from_config() if reparse: self._parse_config_options() self._config_files.append(dest) @@ -254,10 +254,28 @@ class Parser(argparse.ArgumentParser): # iteration, set defaults from config file/environment # variables _debug("Option parsing phase 3: Main parser loop") + # _set_defaults_from_config must be called before _parse_config_options + # This is due to a tricky interaction between the two methods: + # + # (1) _set_defaults_from_config does what its name implies, it updates + # the "default" property of each Option based on the value that exists + # in the config. + # + # (2) _parse_config_options will look at each option and set it to the + # default value that is _currently_ defined. If the option does not + # exist in the namespace, it will be added. The method carefully + # avoids overwriting the value of an option that is already defined in + # the namespace. + # + # Thus, if _set_defaults_from_config has not been called yet when + # _parse_config_options is called, all config file options will get set + # to their hardcoded defaults. This process defines the options in the + # namespace and _parse_config_options will never look at them again. + self._set_defaults_from_config() self._parse_config_options() while not self.parsed: self.parsed = True - self._set_defaults() + self._set_defaults_from_config() self.parse_known_args(args=self.argv, namespace=self.namespace) self._parse_config_options() self._finalize() |