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

Support for argparse.BooleanOptionalAction #239

Closed ftesser closed 3 years ago

ftesser commented 3 years ago

Hi, newer versions of Python and argparse support BooleanOptionalAction (https://docs.python.org/3/library/argparse.html):

>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', action=argparse.BooleanOptionalAction)
>>> parser.parse_args(['--no-foo'])
Namespace(foo=False)

I tested that with ConfigArgParse this action works at command line level, also it works the writing of the config file, but then if I try to load an exiting configuration there are some errors.

Below an example code (tested with python 3.9.6 and ConfigArgParse 1.5.1

from configargparse import ArgumentParser
import argparse

if __name__ == '__main__':
    base_parser = ArgumentParser(
        add_config_file_help=True,
        description='test',
        args_for_writing_out_config_file=['--generate_config_file'],
        args_for_setting_config_path=['--config_file'])
    base_parser.add_argument('--foo', action=argparse.BooleanOptionalAction, default=False)
    args = base_parser.parse_args()
    print(args)
$ python boolean_optional_action_test.py --generate_config_file config.conf
Wrote config file to config.conf
$ cat config.conf
foo = False
$ python boolean_optional_action_test.py --config_file config.conf 
usage: boolean_optional_action_test.py [-h] [--config_file CONFIG_FILE] [--generate_config_file CONFIG_OUTPUT_PATH] [--foo | --no-foo]
boolean_optional_action_test.py: error: argument --foo/--no-foo: ignored explicit argument 'False'

The error probably is derived from the fact that in the config file contains "foo = False" (boolean value) but the BooleanOptionalAction needs just --foo or --no-foo.

Any idea on how to add the support argparse.BooleanOptionalAction in config files of ConfigArgParse?

By the way, this issue should be linked also to this one https://github.com/bw2/ConfigArgParse/issues/209

Thanks!

ftesser commented 3 years ago

Hi @bw2, I just tested the new release (ConfigArgParse 1.5.2), using the same code reported above, now there is not the error (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?