ConfigArgParse icon indicating copy to clipboard operation
ConfigArgParse copied to clipboard

ConfigArgParse is not handling the `--` seperator for positional arguments correctly

Open agyoungs opened this issue 1 year ago • 7 comments

Argparse allows you to use -- to specify the end of optional arguments and begin parsing positional arguments. This is handy if you have an optional argument that takes a list of values. ConfigArgParse seems to fail to handle this when using a configuration. Consider this trivial example:

A program (test.py):

#! /usr/bin/env python3

import configargparse

def main():
    parser = configargparse.ArgumentParser()
    parser.add_argument('positional_arg', nargs='+', default='')
    parser.add_argument('-c', '--config', is_config_file=True)
    parser.add_argument('--list', nargs='+')

    args = parser.parse_args()
    print(args)

if __name__ == "__main__":
    main()

And a config file (test.toml) with:

list = [ 1, 2, 3 ]

Passing the optional list works on command line. Note that the -- is needed here to separate the positional arg from the optional list

> ./test.py --list a b c -- foo
Namespace(positional_arg=['foo'], config=None, list=['a', 'b', 'c'])

And passing the config file also works, while overriding the list also works.

./test.py -c test.toml --list a b c -- foo
Namespace(positional_arg=['foo'], config='test.toml', list=['a', 'b', 'c'])

However, using -- while just using the config option fails. It works if you omit the -- but there may be other optional lists you want to override.

> ./test.py -c test.toml -- foo
Namespace(positional_arg=['foo', '--list', '1', '2', '3'], config='test.toml', list=None)

You can always place the config file last to make sure you don't end with an optional list before the positional arguments, but I think it would be nice to handle this case.

agyoungs avatar Oct 31 '24 17:10 agyoungs