ConfigArgParse icon indicating copy to clipboard operation
ConfigArgParse copied to clipboard

`nargs=argparse.REMAINDER` can break config file option parsing

Open stxue1 opened this issue 1 year ago • 1 comments

Example:

import configargparse

parser = configargparse.ArgParser()
parser.add_argument("--config", is_config_file_arg=True, default="config.cfg")
parser.add_argument('--config_file_option', nargs="*", default=None) # also happens with action="append" and any of its derivatives
parser.add_argument("remainder_option", nargs=argparse.REMAINDER, default=None)
args = ["test"]
options = parser.parse_args(args)

With config.cfg:

config_file_option=Option from config file

remainder_option should be ["test"], but it swallows the config file and becomes ["test", "--config_file_option=Option from config file"], which results in all config file options not being set.

I believe it is due to these lines: https://github.com/bw2/ConfigArgParse/blob/ee77f44d03415f2afe73c82096bae2293db29a3b/configargparse.py#L948-L955

It looks like if there is any other argument with nargs or action="append", then it will put all config options after the remainder option instead of before.

Changing it to args = config_args + args fixes this, but will probably break #144.

stxue1 avatar Oct 09 '23 18:10 stxue1

Upstream removed the documentation for argparse.REMAINDER because of the issues it causes, so it is unlikely this issue will be fixed. https://github.com/python/cpython/pull/18661

mihaic avatar Oct 20 '23 17:10 mihaic