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 does not write args that are initially empty #220

Closed missionfloyd closed 3 years ago

missionfloyd commented 3 years ago

I'm trying to update and write out args, but write_config_file will only write args that have a default value or were passed via commandline. Any that were None, or that were read from the config file are ignored.

Here's a example (test.py)

import configargparse

parser = configargparse.ArgumentParser(default_config_files=["test.conf"])
parser.add_argument("--arg1")
parser.add_argument("--arg2", default = "")
args = parser.parse_args()

args.arg1 = "foo"
args.arg2 = "bar"
parser.write_config_file(args, ["test.conf"])

After running python test.py, test.conf contains

arg2 = bar

But running python test.py --arg1 something results in

arg1 = foo
arg2 = bar

as expected.

bw2 commented 3 years ago

can you do something like args = parser.parse_args("--arg1 foo --arg2 bar") instead?

bw2 commented 3 years ago

fixing this will probably requiring modifying https://github.com/bw2/ConfigArgParse/blob/master/configargparse.py#L686-L716 if you'd like to submit a PR.

missionfloyd commented 3 years ago

can you do something like args = parser.parse_args("--arg1 foo --arg2 bar") instead?

That almost works. Setting just one like that clears the other. I guess I could pass the current values of all the others.

I was able work around it by setting a default and then treating it as None when validating input.