bw2 / ConfigArgParse

A drop-in replacement for argparse that allows options to also be set via config files and/or environment variables.
MIT License
722 stars 121 forks source link

`write_config_file` saves all items coming from config file as strings #241

Open ipcoder opened 3 years ago

ipcoder commented 3 years ago

Resulting saved config file looks differently when argument is defined on command line or in the config file, which always uses strings for values. The code treats specifically does different things for different sources

            if source == _COMMAND_LINE_SOURCE_KEY:
                _, existing_command_line_args = settings['']
                for action in self._actions:
                    config_file_keys = self.get_possible_config_keys(action)
                    if config_file_keys and not action.is_positional_arg and \
                        already_on_command_line(existing_command_line_args,
                                                action.option_strings,
                                                self.prefix_chars):
                        value = getattr(parsed_namespace, action.dest, None)
                        if value is not None:
                            if isinstance(value, bool):
                                value = str(value).lower()
                            config_file_items[config_file_keys[0]] = value

            elif source == _ENV_VAR_SOURCE_KEY:
                for key, (action, value) in settings.items():
                    config_file_keys = self.get_possible_config_keys(action)
                    if config_file_keys:
                        value = getattr(parsed_namespace, action.dest, None)
                        if value is not None:
                            config_file_items[config_file_keys[0]] = value
            elif source.startswith(_CONFIG_FILE_SOURCE_KEY):
                for key, (action, value) in settings.items():
                    config_file_items[key] = value

That is problematic for config files management and also directly contradicts design goals declared by this project.

Is there any particular reason for this behavior?

Thanks

bw2 commented 3 years ago

I don't remember exactly which issues this was intended to address. If you feel like you can create a backward-compatible fix, a PR to fix this would be appreciated.