summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorAnthony Sottile <asottile@umich.edu>2017-09-20 21:35:27 (GMT)
committerÉric Araujo <merwok@users.noreply.github.com>2017-09-20 21:35:27 (GMT)
commitaaf6fc0982c916cb71d9e0afcd7dda4ba495793b (patch)
tree09cb0078fade182a8be0a108bb50b57a71156bd9 /Lib
parent19e4d9346db7fb65845b98a9cb9caacaaac8a81a (diff)
downloadcpython-aaf6fc0982c916cb71d9e0afcd7dda4ba495793b.zip
cpython-aaf6fc0982c916cb71d9e0afcd7dda4ba495793b.tar.gz
cpython-aaf6fc0982c916cb71d9e0afcd7dda4ba495793b.tar.bz2
bpo-26510: make argparse subparsers required by default (#3027)
This fixes a regression from Python 2. To get optional subparsers, use the new parameter ``add_subparsers(required=False)``. Patch by Anthony Sottile.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/argparse.py2
-rw-r--r--Lib/test/test_argparse.py37
2 files changed, 38 insertions, 1 deletions
diff --git a/Lib/argparse.py b/Lib/argparse.py
index d59e645..98bbed0 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -1066,6 +1066,7 @@ class _SubParsersAction(Action):
prog,
parser_class,
dest=SUPPRESS,
+ required=True,
help=None,
metavar=None):
@@ -1079,6 +1080,7 @@ class _SubParsersAction(Action):
dest=dest,
nargs=PARSER,
choices=self._name_parser_map,
+ required=required,
help=help,
metavar=metavar)
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py
index d8bcd73..c4440e4 100644
--- a/Lib/test/test_argparse.py
+++ b/Lib/test/test_argparse.py
@@ -1807,7 +1807,7 @@ class TestAddSubparsers(TestCase):
'bar', type=float, help='bar help')
# check that only one subparsers argument can be added
- subparsers_kwargs = {}
+ subparsers_kwargs = {'required': False}
if aliases:
subparsers_kwargs['metavar'] = 'COMMAND'
subparsers_kwargs['title'] = 'commands'
@@ -1907,6 +1907,41 @@ class TestAddSubparsers(TestCase):
self.assertEqual(NS(foo=False, bar='1', baz='2'),
parser.parse_args('1 2'.split()))
+ def _test_required_subparsers(self, parser):
+ # Should parse the sub command
+ ret = parser.parse_args(['run'])
+ self.assertEqual(ret.command, 'run')
+
+ # Error when the command is missing
+ self.assertArgumentParserError(parser.parse_args, ())
+
+ def test_required_subparsers_via_attribute(self):
+ parser = ErrorRaisingArgumentParser()
+ subparsers = parser.add_subparsers(dest='command')
+ subparsers.required = True
+ subparsers.add_parser('run')
+ self._test_required_subparsers(parser)
+
+ def test_required_subparsers_via_kwarg(self):
+ parser = ErrorRaisingArgumentParser()
+ subparsers = parser.add_subparsers(dest='command', required=True)
+ subparsers.add_parser('run')
+ self._test_required_subparsers(parser)
+
+ def test_required_subparsers_default(self):
+ parser = ErrorRaisingArgumentParser()
+ subparsers = parser.add_subparsers(dest='command')
+ subparsers.add_parser('run')
+ self._test_required_subparsers(parser)
+
+ def test_optional_subparsers(self):
+ parser = ErrorRaisingArgumentParser()
+ subparsers = parser.add_subparsers(dest='command', required=False)
+ subparsers.add_parser('run')
+ # No error here
+ ret = parser.parse_args(())
+ self.assertIsNone(ret.command)
+
def test_help(self):
self.assertEqual(self.parser.format_usage(),
'usage: PROG [-h] [--foo] bar {1,2,3} ...\n')