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

nargs=argparse.REMAINDER can break config file option parsing #285

Open
stxue1 opened this issue Oct 9, 2023 · 1 comment
Open

nargs=argparse.REMAINDER can break config file option parsing #285

stxue1 opened this issue Oct 9, 2023 · 1 comment

Comments

@stxue1
Copy link

stxue1 commented Oct 9, 2023

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:

if (action and action.nargs or
isinstance(action, argparse._AppendAction)):
nargs = True
if nargs:
args = args + config_args
else:
args = config_args + args

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.

@mihaic
Copy link

mihaic commented Oct 20, 2023

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

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

2 participants