[issue9849] Argparse needs better error handling for nargs

paul j3 report at bugs.python.org
Wed Apr 17 05:12:25 CEST 2013


paul j3 added the comment:

It does shift the error from parse_args to add_argument, but the message 'ValueError: length of metavar tuple does not match nargs', indicates that it's a side effect of checking on the tuple form of `metavar`.
http://bugs.python.org/issue9348

There is still room for cleaning up these tests.  There are 2 functions that define what are acceptable values for nargs, [None, OPTIONAL, ZERO_OR_MORE, ONE_OR_MORE, REMAINDER, PARSER, integer].  Should one or other explicitly check nargs is an integer if it does not match one of the other strings?

And the test in _ActionContainer.add_argument()

    # raise an error if the metavar does not match the type
    if hasattr(self, "_get_formatter"):
        try:
            self._get_formatter()._format_args(action, None)
        except TypeError:
            raise ValueError("length of metavar tuple does not match nargs")

uses an ArgumentParser._get_formatter method  (though 'hasattr' prevents runtime errors).  In fact both functions that use nargs belong to the parser, not the container.  I wonder if `add_argument` should be moved to ArgumentParser.

----------
nosy: +paul.j3

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


More information about the Python-bugs-list mailing list