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

[bug]Cannot override yaml settings on the command line when using the bool option with strtobool. #259

Open
MasanoriYamada opened this issue Dec 10, 2021 · 1 comment

Comments

@MasanoriYamada
Copy link

MasanoriYamada commented Dec 10, 2021

Since bool is difficult to handle on argparse, there is a way to use the strtobool function.

check.py

import configargparse
from distutils.util import strtobool

def strtointtobool(x):
    return bool(strtobool(x))


def parse_args():
    cp = configargparse.ArgumentParser(default_config_files=[])
    cp.add_argument('-c', '--conf', dest='config', is_config_file=True, help='config file path')
    cp.add_argument('-f', '--flag', dest='flag', type=strtointtobool)
    cp.add_argument('-a', '--array', dest='array', type=int, nargs='+', default=[], help='array parama')
    return cp.parse_args()

args = parse_args()
print(args)
print(type(args.flag))

It works intuitively.

➜ python check.py -f True
Namespace(config=None, flag=True, array=[])
<class 'bool'>
➜ python check.py -f False
Namespace(config=None, flag=False, array=[])
<class 'bool'>

Issue

If you set a value in setting.yaml, it cannot be overridden by command line options. This is different from the normal behavior of configargparse. This happens when used with array type

setting.yaml

array: [1, 12, 35, 40]
f: True
➜  python check.py -c setting.yaml
Namespace(config='setting.yaml', flag=True, array=[1, 12, 35, 40])
<class 'bool'>
➜ python check.py -c setting.yaml -f True
Namespace(config='setting.yaml', flag=True, array=[1, 12, 35, 40])
<class 'bool'>
➜ python check.py -c setting.yaml -f False  # problem
Namespace(config='setting.yaml', flag=True, array=[1, 12, 35, 40])
<class 'bool'>

The -f False option on the command line has been overwritten by setting.yaml

If you don't use the array option, it will work as expected.

setting.yaml

f: True
➜ python check.py -c setting.yaml -f False
Namespace(config='setting.yaml', flag=False, array=[])
<class 'bool'>

gist: https://gist.github.com/MasanoriYamada/a50dd8276b0f5cf56e32aa5932b93ae5

@MasanoriYamada MasanoriYamada changed the title [bug]The bool option on the command line using strtobool will be overwritten in yaml. [bug]Cannot override yaml settings on the command line when using the bool option with strtobool. Dec 10, 2021
@MasanoriYamada
Copy link
Author

MasanoriYamada commented Dec 10, 2021

If you use only the --f option as in cp.add_argument('--f', dest='flag', type=strtointtobool) , it works as expected

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