Raymond Hettinger <[email protected]> added the comment:
> I want the order of priority to fall back to the defaults,
> if no value is specified in the config file. And if an argument
> is passed via argv, then that value should take precedence
> over what is set in the config file.
from collections import ChainMap
from argparse import ArgumentParser
parser = ArgumentParser()
missing = object()
for arg in 'abcde':
parser.add_argument(f'-{arg}', default=missing)
system_defaults = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
config_file = {'a': 6, 'c': 7, 'e': 8}
command_line = vars(parser.parse_args('-a 8 -b 9'.split()))
command_line = {k: v for k, v in command_line.items() if v is not missing}
combined = ChainMap(command_line, config_file, system_defaults)
print(dict(combined))
> This is in the first message in this issue.
The feature request is clear. What problem you're trying to solve isn't clear.
What you're looking for is likely some permutation of the above code or
setting a argument default to a value in the ChainMap. I think you're ignoring
that we already have ways to set default values to anything that is needed and
we already have ways to tell is an argument was not encountered (but not both
at the same time).
[Paul J3]
> So unless someone comes up with a really clever idea,
> this is bigger request than it first impressions suggest.
I recommend rejecting this feature request. The module is not obliged to be
all things to all people. Most variations of the problem already have a
solution. We should leave it at that. Extending the namespace with extra
boolean arguments would just open a can of worms that would make most users
worse off, likely breaking any code that expects the namespace to contain
exactly what it already contains.
----------
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue44748>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com