#13922: argparse no longer incorrectly strips '--' after the first one.
authorR David Murray <rdmurray@bitdance.com>
Sun, 22 Jul 2012 02:48:35 +0000 (22:48 -0400)
committerR David Murray <rdmurray@bitdance.com>
Sun, 22 Jul 2012 02:48:35 +0000 (22:48 -0400)
Patch by Jeff Knupp.

Lib/argparse.py
Lib/test/test_argparse.py
Misc/ACKS
Misc/NEWS

index 25803b35195e7762b6104fbc06d0b51b8c4de211..0ee8c0865e80ecac3d805548b79b08b2a694feaa 100644 (file)
@@ -2186,9 +2186,12 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
     # Value conversion methods
     # ========================
     def _get_values(self, action, arg_strings):
-        # for everything but PARSER args, strip out '--'
+        # for everything but PARSER, REMAINDER args, strip out first '--'
         if action.nargs not in [PARSER, REMAINDER]:
-            arg_strings = [s for s in arg_strings if s != '--']
+            try:
+                arg_strings.remove('--')
+            except ValueError:
+                pass
 
         # optional argument produces a default when not present
         if not arg_strings and action.nargs == OPTIONAL:
index 1a72a13e30deee30ab34038369f7e884ed2b70a9..3a2976126f2f2cb62bab752ca5adde36c73d054f 100644 (file)
@@ -1761,6 +1761,14 @@ class TestAddSubparsers(TestCase):
         parser2.add_argument('-y', choices='123', help='y help')
         parser2.add_argument('z', type=complex, nargs='*', help='z help')
 
+        # add third sub-parser
+        parser3_kwargs = dict(description='3 description')
+        if subparser_help:
+            parser3_kwargs['help'] = '3 help'
+        parser3 = subparsers.add_parser('3', **parser3_kwargs)
+        parser3.add_argument('t', type=int, help='t help')
+        parser3.add_argument('u', nargs='...', help='u help')
+
         # return the main parser
         return parser
 
@@ -1790,6 +1798,10 @@ class TestAddSubparsers(TestCase):
             self.parser.parse_args('--foo 0.125 1 c'.split()),
             NS(foo=True, bar=0.125, w=None, x='c'),
         )
+        self.assertEqual(
+            self.parser.parse_args('-1.5 3 11 -- a --foo 7 -- b'.split()),
+            NS(foo=False, bar=-1.5, t=11, u=['a', '--foo', '7', '--', 'b']),
+        )
 
     def test_parse_known_args(self):
         self.assertEqual(
@@ -1824,15 +1836,15 @@ class TestAddSubparsers(TestCase):
 
     def test_help(self):
         self.assertEqual(self.parser.format_usage(),
-                         'usage: PROG [-h] [--foo] bar {1,2} ...\n')
+                         'usage: PROG [-h] [--foo] bar {1,2,3} ...\n')
         self.assertEqual(self.parser.format_help(), textwrap.dedent('''\
-            usage: PROG [-h] [--foo] bar {1,2} ...
+            usage: PROG [-h] [--foo] bar {1,2,3} ...
 
             main description
 
             positional arguments:
               bar         bar help
-              {1,2}       command help
+              {1,2,3}     command help
 
             optional arguments:
               -h, --help  show this help message and exit
@@ -1843,15 +1855,15 @@ class TestAddSubparsers(TestCase):
         # Make sure - is still used for help if it is a non-first prefix char
         parser = self._get_parser(prefix_chars='+:-')
         self.assertEqual(parser.format_usage(),
-                         'usage: PROG [-h] [++foo] bar {1,2} ...\n')
+                         'usage: PROG [-h] [++foo] bar {1,2,3} ...\n')
         self.assertEqual(parser.format_help(), textwrap.dedent('''\
-            usage: PROG [-h] [++foo] bar {1,2} ...
+            usage: PROG [-h] [++foo] bar {1,2,3} ...
 
             main description
 
             positional arguments:
               bar         bar help
-              {1,2}       command help
+              {1,2,3}     command help
 
             optional arguments:
               -h, --help  show this help message and exit
@@ -1862,15 +1874,15 @@ class TestAddSubparsers(TestCase):
     def test_help_alternate_prefix_chars(self):
         parser = self._get_parser(prefix_chars='+:/')
         self.assertEqual(parser.format_usage(),
-                         'usage: PROG [+h] [++foo] bar {1,2} ...\n')
+                         'usage: PROG [+h] [++foo] bar {1,2,3} ...\n')
         self.assertEqual(parser.format_help(), textwrap.dedent('''\
-            usage: PROG [+h] [++foo] bar {1,2} ...
+            usage: PROG [+h] [++foo] bar {1,2,3} ...
 
             main description
 
             positional arguments:
               bar         bar help
-              {1,2}       command help
+              {1,2,3}     command help
 
             optional arguments:
               +h, ++help  show this help message and exit
@@ -1879,18 +1891,19 @@ class TestAddSubparsers(TestCase):
 
     def test_parser_command_help(self):
         self.assertEqual(self.command_help_parser.format_usage(),
-                         'usage: PROG [-h] [--foo] bar {1,2} ...\n')
+                         'usage: PROG [-h] [--foo] bar {1,2,3} ...\n')
         self.assertEqual(self.command_help_parser.format_help(),
                          textwrap.dedent('''\
-            usage: PROG [-h] [--foo] bar {1,2} ...
+            usage: PROG [-h] [--foo] bar {1,2,3} ...
 
             main description
 
             positional arguments:
               bar         bar help
-              {1,2}       command help
+              {1,2,3}     command help
                 1         1 help
                 2         2 help
+                3         3 help
 
             optional arguments:
               -h, --help  show this help message and exit
@@ -2001,6 +2014,7 @@ class TestAddSubparsers(TestCase):
                 1 (1alias1, 1alias2)
                                     1 help
                 2                   2 help
+                3                   3 help
             """))
 
 # ============
index 95c06baffd18c850bee16e36493c6fbe790e8752..51b5996e7734c592f8e7c50e1eca1d6ffaad6885 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -511,6 +511,7 @@ Thomas Kluyver
 Kim Knapp
 Lenny Kneler
 Pat Knight
+Jeff Knupp
 Greg Kochanski
 Damon Kohler
 Marko Kohtala
index 57f3650f482b001d59b2b54a294de959be8cd71a..0b607db85825b551e91efda7eeeab17065f38ec3 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -98,6 +98,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #13922: argparse no longer incorrectly strips '--'s that appear
+  after the first one.
+
 - Issue #12353: argparse now correctly handles null argument values.
 
 - Issues #10017 and #14998: Fix TypeError using pprint on dictionaries with