bw2 / ConfigArgParse

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

how to append values from multiple default_config_files ? #280

Closed damienmas closed 1 year ago

damienmas commented 1 year ago

Hi,

Is there a way to append multiple key values when using multiple default_config_files ?

Example:

import configargparse

parser = configargparse.ArgParser(config_file_parser_class=configargparse.YAMLConfigFileParser,
                                  default_config_files=[
                                           'default.yaml',
                                           'user.yaml'
                                  ])

parser.add_argument('--host', '-H', action='append', required=True,
                    help='List of hosts.')

args, unknown_args = parser.parse_known_args()
print(args)

default.yaml

host:
  - host1
  - host2
  - host3

user.yaml

host:
  - jumpbox

Output:

Namespace(host=['jumpbox'])

At the end I'd like that host values are concatenated. is there a way to change the behavior of the parser ?

Expected output:

Namespace(host=['jumpbox', 'host1', 'host2', 'host3'])

I know it seems to be by design but just wondering if there is a way to tweak this behavior with some flags ...

from the doc (https://github.com/bw2/ConfigArgParse/blob/bca2c2fb8f13f45dea0f06d4b4c3f0b4336c2d54/configargparse.py#L694C1-L705C40):

            default_config_files: When specified, this list of config files will
                be parsed in order, with the values from each config file
                taking precedence over previous ones. This allows an application
                to look for config files in multiple standard locations such as
                the install directory, home directory, and current directory.
                Also, shell \* syntax can be used to specify all conf files in a
                directory. For example::

                    ["/etc/conf/app_config.ini",
                    "/etc/conf/conf-enabled/*.ini",
                    "~/.my_app_config.ini",
                    "./app_config.txt"]
bw2 commented 1 year ago

I think both the current behavior and your suggested behavior are reasonable, but making is configurable is too niche for the main version of configargparse IMO (unless multiple people request this). In the meantime, you could subclass or fork to add the desired behavior.