diff -r 5c716437a83a Lib/argparse.py --- a/Lib/argparse.py Tue May 24 12:05:19 2011 +0200 +++ b/Lib/argparse.py Thu May 26 19:42:26 2011 +0200 @@ -1969,17 +1969,12 @@ # if we didn't consume all the argument strings, there were extras extras.extend(arg_strings[stop_index:]) - # if we didn't use all the Positional objects, there were too few - # arg strings supplied. - if positionals: - self.error(_('too few arguments')) - # make sure all required actions were present - for action in self._actions: - if action.required: - if action not in seen_actions: - name = _get_action_name(action) - self.error(_('argument %s is required') % name) + required_actions = [_get_action_name(action) for action in self._actions + if action.required and action not in seen_actions] + if required_actions: + self.error(_('the following arguments are required: %s') % + ', '.join(required_actions)) # make sure all required groups had one option present for group in self._mutually_exclusive_groups: diff -r 5c716437a83a Lib/test/test_argparse.py --- a/Lib/test/test_argparse.py Tue May 24 12:05:19 2011 +0200 +++ b/Lib/test/test_argparse.py Thu May 26 19:42:26 2011 +0200 @@ -4480,6 +4480,40 @@ else: self.fail() +# ========================== +# ArgumentContentError tests +# ========================== + +class TestArgumentContentError(TestCase): + + def test_argument_missing(self): + parser = ErrorRaisingArgumentParser(prog='PROG') + parser.add_argument('x', type=str) + parser.add_argument('-y', type=int, required=True) + parser.add_argument('z', type=str, nargs='+') + + with self.assertRaisesRegex(ArgumentParserError, 'x, -y, z'): + parser.parse_args([]) + with self.assertRaisesRegex(ArgumentParserError, '-y, z'): + parser.parse_args(['myXargument']) + with self.assertRaisesRegex(ArgumentParserError, 'error:[\w ]+: z'): + parser.parse_args(['myXargument', '-y1']) + + def test_optional_argument_missing(self): + parser = ErrorRaisingArgumentParser(prog='PROG') + parser.add_argument('foo', type=str) + parser.add_argument('--spam', type=int, required=True) + parser.add_argument('--egg', type=bool, nargs='?', default=True) + parser.add_argument('--bar', type=int, nargs='?', default=1) + + with self.assertRaisesRegex(ArgumentParserError, 'foo, --spam'): + parser.parse_args([]) + with self.assertRaisesRegex(ArgumentParserError, '--spam'): + parser.parse_args(['first', '--egg false']) + with self.assertRaisesRegex(ArgumentParserError, 'foo'): + parser.parse_args(['--spam=1', '--egg', '--bar=22']) + + # ====================== # parse_known_args tests # ======================