Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 00528e8

Browse files
committed
#13922: argparse no longer incorrectly strips '--' after the first one.
Patch by Jeff Knupp.
1 parent b94082a commit 00528e8

4 files changed

Lines changed: 35 additions & 14 deletions

File tree

Lib/argparse.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2186,9 +2186,12 @@ def _get_nargs_pattern(self, action):
21862186
# Value conversion methods
21872187
# ========================
21882188
def _get_values(self, action, arg_strings):
2189-
# for everything but PARSER args, strip out '--'
2189+
# for everything but PARSER, REMAINDER args, strip out first '--'
21902190
if action.nargs not in [PARSER, REMAINDER]:
2191-
arg_strings = [s for s in arg_strings if s != '--']
2191+
try:
2192+
arg_strings.remove('--')
2193+
except ValueError:
2194+
pass
21922195

21932196
# optional argument produces a default when not present
21942197
if not arg_strings and action.nargs == OPTIONAL:

Lib/test/test_argparse.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,6 +1761,14 @@ def _get_parser(self, subparser_help=False, prefix_chars=None,
17611761
parser2.add_argument('-y', choices='123', help='y help')
17621762
parser2.add_argument('z', type=complex, nargs='*', help='z help')
17631763

1764+
# add third sub-parser
1765+
parser3_kwargs = dict(description='3 description')
1766+
if subparser_help:
1767+
parser3_kwargs['help'] = '3 help'
1768+
parser3 = subparsers.add_parser('3', **parser3_kwargs)
1769+
parser3.add_argument('t', type=int, help='t help')
1770+
parser3.add_argument('u', nargs='...', help='u help')
1771+
17641772
# return the main parser
17651773
return parser
17661774

@@ -1790,6 +1798,10 @@ def test_parse_args(self):
17901798
self.parser.parse_args('--foo 0.125 1 c'.split()),
17911799
NS(foo=True, bar=0.125, w=None, x='c'),
17921800
)
1801+
self.assertEqual(
1802+
self.parser.parse_args('-1.5 3 11 -- a --foo 7 -- b'.split()),
1803+
NS(foo=False, bar=-1.5, t=11, u=['a', '--foo', '7', '--', 'b']),
1804+
)
17931805

17941806
def test_parse_known_args(self):
17951807
self.assertEqual(
@@ -1824,15 +1836,15 @@ def test_dest(self):
18241836

18251837
def test_help(self):
18261838
self.assertEqual(self.parser.format_usage(),
1827-
'usage: PROG [-h] [--foo] bar {1,2} ...\n')
1839+
'usage: PROG [-h] [--foo] bar {1,2,3} ...\n')
18281840
self.assertEqual(self.parser.format_help(), textwrap.dedent('''\
1829-
usage: PROG [-h] [--foo] bar {1,2} ...
1841+
usage: PROG [-h] [--foo] bar {1,2,3} ...
18301842
18311843
main description
18321844
18331845
positional arguments:
18341846
bar bar help
1835-
{1,2} command help
1847+
{1,2,3} command help
18361848
18371849
optional arguments:
18381850
-h, --help show this help message and exit
@@ -1843,15 +1855,15 @@ def test_help_extra_prefix_chars(self):
18431855
# Make sure - is still used for help if it is a non-first prefix char
18441856
parser = self._get_parser(prefix_chars='+:-')
18451857
self.assertEqual(parser.format_usage(),
1846-
'usage: PROG [-h] [++foo] bar {1,2} ...\n')
1858+
'usage: PROG [-h] [++foo] bar {1,2,3} ...\n')
18471859
self.assertEqual(parser.format_help(), textwrap.dedent('''\
1848-
usage: PROG [-h] [++foo] bar {1,2} ...
1860+
usage: PROG [-h] [++foo] bar {1,2,3} ...
18491861
18501862
main description
18511863
18521864
positional arguments:
18531865
bar bar help
1854-
{1,2} command help
1866+
{1,2,3} command help
18551867
18561868
optional arguments:
18571869
-h, --help show this help message and exit
@@ -1862,15 +1874,15 @@ def test_help_extra_prefix_chars(self):
18621874
def test_help_alternate_prefix_chars(self):
18631875
parser = self._get_parser(prefix_chars='+:/')
18641876
self.assertEqual(parser.format_usage(),
1865-
'usage: PROG [+h] [++foo] bar {1,2} ...\n')
1877+
'usage: PROG [+h] [++foo] bar {1,2,3} ...\n')
18661878
self.assertEqual(parser.format_help(), textwrap.dedent('''\
1867-
usage: PROG [+h] [++foo] bar {1,2} ...
1879+
usage: PROG [+h] [++foo] bar {1,2,3} ...
18681880
18691881
main description
18701882
18711883
positional arguments:
18721884
bar bar help
1873-
{1,2} command help
1885+
{1,2,3} command help
18741886
18751887
optional arguments:
18761888
+h, ++help show this help message and exit
@@ -1879,18 +1891,19 @@ def test_help_alternate_prefix_chars(self):
18791891

18801892
def test_parser_command_help(self):
18811893
self.assertEqual(self.command_help_parser.format_usage(),
1882-
'usage: PROG [-h] [--foo] bar {1,2} ...\n')
1894+
'usage: PROG [-h] [--foo] bar {1,2,3} ...\n')
18831895
self.assertEqual(self.command_help_parser.format_help(),
18841896
textwrap.dedent('''\
1885-
usage: PROG [-h] [--foo] bar {1,2} ...
1897+
usage: PROG [-h] [--foo] bar {1,2,3} ...
18861898
18871899
main description
18881900
18891901
positional arguments:
18901902
bar bar help
1891-
{1,2} command help
1903+
{1,2,3} command help
18921904
1 1 help
18931905
2 2 help
1906+
3 3 help
18941907
18951908
optional arguments:
18961909
-h, --help show this help message and exit
@@ -2001,6 +2014,7 @@ def test_alias_help(self):
20012014
1 (1alias1, 1alias2)
20022015
1 help
20032016
2 2 help
2017+
3 3 help
20042018
"""))
20052019

20062020
# ============

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ Thomas Kluyver
511511
Kim Knapp
512512
Lenny Kneler
513513
Pat Knight
514+
Jeff Knupp
514515
Greg Kochanski
515516
Damon Kohler
516517
Marko Kohtala

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ Core and Builtins
9898
Library
9999
-------
100100

101+
- Issue #13922: argparse no longer incorrectly strips '--'s that appear
102+
after the first one.
103+
101104
- Issue #12353: argparse now correctly handles null argument values.
102105

103106
- Issues #10017 and #14998: Fix TypeError using pprint on dictionaries with

0 commit comments

Comments
 (0)