diff Lib/argparse.py @ 68969:4bb651eb7539 2.7

Issue #9348: Raise an early error if argparse nargs and metavar don't match. (Merge from 3.2.)
author Steven Bethard <steven.bethard@gmail.com>
date Sat, 26 Mar 2011 17:57:52 +0100
parents e82501812ae3
children 75ec20b4c50e
line wrap: on
line diff
--- a/Lib/argparse.py	Sat Mar 26 12:18:00 2011 +0000
+++ b/Lib/argparse.py	Sat Mar 26 17:57:52 2011 +0100
@@ -1284,6 +1284,13 @@
         if not _callable(type_func):
             raise ValueError('%r is not callable' % type_func)
 
+        # 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")
+
         return self._add_action(action)
 
     def add_argument_group(self, *args, **kwargs):