[issue9849] Argparse needs better error handling for nargs
paul j3
report at bugs.python.org
Wed Apr 24 08:22:54 CEST 2013
paul j3 added the comment:
This patch adds a value test for nargs during add_argument. The core of the new test is in ArgumentParser._check_argument.
add_argument() now calls ArgumentParser._check_argument(), which calls _format_args(). If it gets a TypeError, it raises a metavar ValueError as before. But if it gets an ValueError, it raises an ArgumentError.
An argument group gets the _check_argument method from its container. This way, check_argument() works for both parsers and groups.
HelpFormater _format_args() now raises a ValueError if the nargs is not an integer (or one of the recognized strings).
What kind of error should we produce when there is a problem with nargs?
An ArgumentError has the advantage that it includes the action name. But the metavar tuple test was coded to raise ValueError. Plus the test_argparse.py TestInvalidArgumentConstructors class tests all check for TypeError or ValueError.
I have kept the metavar tuple case as ValueError. That way, test_argparse.py runs without a change. I still need to add tests for invalid string nargs values. And I think the case could be made for returning ArgumentValue errors.
----------
keywords: +patch
Added file: http://bugs.python.org/file29998/nargswarn.patch
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue9849>
_______________________________________
More information about the Python-bugs-list
mailing list