Issue #9348: Raise an early error if argparse nargs and metavar don't match. (Merge from 3.2.)
diff --git a/Lib/argparse.py b/Lib/argparse.py
index 8e1722a..d4e691d 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -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):