[issue21666] Argparse exceptions should include which argument has a problem

paul j3 report at bugs.python.org
Sun Jun 8 08:03:05 CEST 2014


paul j3 added the comment:

In http://bugs.python.org/file30010/nargswarn.patch adding the '_expand_help(action)' line should test the help string (during add_argument).

    def _check_argument(self, action):
        # check action arguments
        # focus on the arguments that the parent container does not know about
        # check nargs and metavar tuple
        try:
            self._get_formatter()._format_args(action, None)
        except ValueError as e:
            raise ArgumentError(action, str(e))
        except TypeError:
            #raise ValueError("length of metavar tuple does not match nargs")
            raise ArgumentError(action, "length of metavar tuple does not match nargs")
        # check the 'help' string
        try:
            self._get_formatter()._expand_help(action)
        except (ValueError, TypeError, KeyError) as e:
            raise ArgumentError(action, 'badly formed help string')

The 'except' clause may need to be changed to capture all (or just most?) of the possible errors in the format string.  Besides your error I can imagine '%(error)s` (a KeyError).  We also need to pay attention to the differences between Py2 and Py3 errors.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue21666>
_______________________________________


More information about the Python-bugs-list mailing list