New submission from Matthew Hall:
I have some Python code which takes advantage of the ability to prepopulate the
argparse Namespace before passing it to argparse. This allows me to read
sensitive settings such as usernames and passwords from DBs, environment
variables, etc. in addition to the CLI for security so they don't appear as
process arguments anyone could see.
Some of these scripts have usernames and passwords as required arguments since
they cannot function without them. Unfortunately you hit this bug when you load
them into the namespace yourself from some secure place of your choice and then
pass them in:
args = parser.parse_args(argv, namespace)
The following argparse code which doesn't respect that they were present
already and throws a fatal error. In addition, the parse_known_args function is
246 lines of code which uses ugly nested cheater functions, and the
seen_actions variable is in the stack of the function not a member variable, so
there is no way you can cleanly override or work around the behavior with a
subclass, either by replacing the function (too massive) or by fixing up the
seen_actions variable (can't get to it on the stack from outside).
So, I suggest that this code should to be fixed so that it will respect any
existing values in the Namespace if they are present:
# make sure all required actions were present, and convert defaults.
for action in self._actions:
if action not in seen_actions:
if action.required:
name = _get_action_name(action)
self.error(_('argument %s is required') % name)
----------
components: Library (Lib)
messages: 288667
nosy: mhcptg
priority: normal
severity: normal
status: open
title: argparse: does not respect required args pre-populated into namespace
type: behavior
versions: Python 2.7
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue29670>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com