Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(77215)

Unified Diff: Lib/test/test_argparse.py

Issue 9253: argparse: optional subparsers
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Lib/argparse.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
--- a/Lib/test/test_argparse.py Tue Apr 09 17:23:27 2013 +0200
+++ b/Lib/test/test_argparse.py Fri Apr 12 22:49:30 2013 -0700
@@ -1833,6 +1833,31 @@
args = args_str.split()
self.assertArgumentParserError(self.parser.parse_args, args)
+ def test_parse_args_missing_cmd(self):
+ # test error msg when cmd is missing
+ for args_str,reg_str in [['0.5','the following arguments are required']]:
+ args = args_str.split()
+ self.assertRaisesRegexp(ArgumentParserError, reg_str,
+ self.parser.parse_args, args)
+ # was - no error (effectively optional argument)
+
+ def test_parse_args_invalid_choice(self):
+ # test error msg when cmd is wrong choice
+ for args_str,reg_str in [['0.5 0','error: argument {1,2,3}: invalid choice:']]:
+ args = args_str.split()
+ self.assertRaisesRegexp(ArgumentParserError, reg_str,
+ self.parser.parse_args, args)
+ # was - 'error: invalid choice:'
+
+ def test_required(self):
+ # test ability to change 'required'; default is True
+ parser = ErrorRaisingArgumentParser()
+ parser.add_argument('--foo', action='store_true')
+ subparsers = parser.add_subparsers(required=False)
+ parser1 = subparsers.add_parser('1')
+ parser1.add_argument('baz')
+ self.assertEqual(NS(foo=False), parser.parse_args([]))
+
def test_parse_args(self):
# check some non-failure cases:
self.assertEqual(
@@ -2042,6 +2067,10 @@
parser = self._get_parser(aliases=True)
self.assertArgumentParserError(parser.parse_args,
'0.5 1alias3 b'.split())
+ # with metavar, the error message has not changed
+ self.assertRaisesRegexp(ArgumentParserError,
+ 'error: argument COMMAND: invalid choice:',
+ parser.parse_args, '0.5 1alias3 b'.split())
def test_alias_help(self):
parser = self._get_parser(aliases=True, subparser_help=True)
« no previous file with comments | « Lib/argparse.py ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+