From 8d9a4628c35811a803bde626c76573c722beea03 Mon Sep 17 00:00:00 2001 From: Steven Bethard Date: Sat, 26 Mar 2011 17:33:56 +0100 Subject: Issue #9348: Raise an early error if argparse nargs and metavar don't match. --- Lib/argparse.py | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'Lib/argparse.py') diff --git a/Lib/argparse.py b/Lib/argparse.py index de3cd114794..e46f919e024 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -1294,6 +1294,13 @@ class _ActionsContainer(object): 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): -- cgit v1.2.3