This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author mhcptg
Recipients mhcptg
Date 2017-02-27.20:33:09
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1488227590.23.0.0462095560451.issue29670@psf.upfronthosting.co.za>
In-reply-to
Content
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)
History
Date User Action Args
2017-02-27 20:33:10mhcptgsetrecipients: + mhcptg
2017-02-27 20:33:10mhcptgsetmessageid: <1488227590.23.0.0462095560451.issue29670@psf.upfronthosting.co.za>
2017-02-27 20:33:10mhcptglinkissue29670 messages
2017-02-27 20:33:09mhcptgcreate