Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConfigArgParse is not handling the -- seperator for positional arguments correctly #298

Open
agyoungs opened this issue Oct 31, 2024 · 0 comments

Comments

@agyoungs
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant