bw2 / ConfigArgParse

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

Issue with argparse.BooleanOptionalAction #245

Closed ftesser closed 3 years ago

ftesser commented 3 years ago

Following #239, I just tested the new release (ConfigArgParse 1.5.2), using the same code reported in #239: now there is not the error reported in #239 (thanks!), but the functionality is not as I expected.

The issues are:

  1. when I try to save the config file without any indication about foo, the default False is not taken into consideration and the config.conf is empty:
$ python boolean_optional_action_test.py --generate_config_file config.conf
Wrote config file to config.conf
$ cat config.conf
  1. Setting the --foo in the command line:
    $ python boolean_optional_action_test.py --generate_config_file config.conf --foo
    Wrote config file to config.conf
    $ cat config.conf
    foo = true

    the config file correctly contains some info about foo (true), but this is not readable from the parser, in fact parsing this config file return the default foo = False and not the True value contained in the file:

    python boolean_optional_action_test.py --config_file config.conf
    Namespace(config_file='config.conf', write_out_config_file_to_this_path=None, foo=False)

Any idea of how to manage that?

bw2 commented 3 years ago

Sorry about the remaining issue(s). I'm not sure when I'll be able to work on this. In the mean time, a PR to fix it would be appreciated.

ftesser commented 3 years ago

Hello @bw2, I have just sent a PR that fix the second part of this issue (the reading from config file).

The problem that the default arguments are not saved in the config file is still there, but for this problem it might be better to open a separate issue, because I see that this problem is not related only to action=BooleanOptionalAction but also to action = " store_true ". I think that perhaps the _writing_out_config_file function was designed like that (do not save the default arguments).

ftesser commented 3 years ago

Close the issue after merging #248