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 <rep...@bugs.python.org>
<http://bugs.python.org/issue9849>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to