[issue44748] argparse: a bool indicating if arg was encountered

Raymond Hettinger report at bugs.python.org
Thu Sep 2 16:55:34 EDT 2021


Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:

> With a config file loaded as part of the program, 
> overwrite the values loaded from the config file 
> if the argument was encountered in the argument vector.

It seems to me that default values can already be used for this purpose:

from argparse import ArgumentParser

config = {'w': 5, 'x': 10, 'y': False, 'z': True}

missing = object()
p = ArgumentParser()
p.add_argument('-x', type=int, default=missing)
p.add_argument('-y', action='store_true', default=missing)
ns = p.parse_args()

# update config for specified values
for parameter, value in vars(ns).items():
    if value is not missing:
        config[parameter] = value

print(config)

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44748>
_______________________________________


More information about the Python-bugs-list mailing list