diff -r b7f2d28260b4 Lib/argparse.py
--- a/Lib/argparse.py Tue Apr 09 17:23:27 2013 +0200
+++ b/Lib/argparse.py Sat Apr 13 23:33:22 2013 -0700
@@ -1907,6 +1907,15 @@
# and add the Positional and its args to the list
for action, arg_count in zip(positionals, arg_counts):
args = arg_strings[start_index: start_index + arg_count]
+ if action.nargs not in [PARSER, REMAINDER]:
+ pats = arg_strings_pattern[start_index: start_index + arg_count]
+ # remove '--' corresponding to a '-' in pattern
+ try:
+ ii = pats.index('-')
+ assert(args[ii]=='--')
+ del args[ii]
+ except ValueError:
+ pass
start_index += arg_count
take_action(action, args)
@@ -2212,11 +2221,12 @@
# ========================
def _get_values(self, action, arg_strings):
# for everything but PARSER, REMAINDER args, strip out first '--'
- if action.nargs not in [PARSER, REMAINDER]:
- try:
- arg_strings.remove('--')
- except ValueError:
- pass
+ #if action.nargs not in [PARSER, REMAINDER]:
+ # try:
+ # arg_strings.remove('--')
+ # except ValueError:
+ # pass
+ # we already removed '--' in consume_positionals()
# optional argument produces a default when not present
if not arg_strings and action.nargs == OPTIONAL:
diff -r b7f2d28260b4 Lib/test/test_argparse.py
--- a/Lib/test/test_argparse.py Tue Apr 09 17:23:27 2013 +0200
+++ b/Lib/test/test_argparse.py Sat Apr 13 23:33:22 2013 -0700
@@ -1281,6 +1281,50 @@
('-3 1 a', NS(x=None, y=1.0, z=['a'])),
]
+class TestDoubleDashRemoval(ParserTestCase):
+ """Test actions with multiple -- values"""
+
+ """argparse removed all '--'
+ a 3-2012 patch removed just the 1st -- of each positional group
+ this new patch removes just the 1st --
+ this change is most valuable when passing arg strings to another process"""
+ argument_signatures = [
+ Sig('-f', '--foo', help='an optional'),
+ Sig('cmd', help='a command'),
+ Sig('rest', nargs='*', help='zero or more args'),
+ ]
+ failures = ['cmd --foo bar 1 2 3', 'cmd -f1 2 3']
+ successes = [
+ ('-f1 1 -- 2 3', NS(cmd='1', foo='1', rest=['2', '3'])),
+ ('cmd -- --foo bar', NS(cmd='cmd', foo=None, rest=['--foo', 'bar'])),
+ ('cmd -- --foo -- -f2', NS(cmd='cmd', foo=None, rest=['--foo', '--', '-f2'])),
+
+ ('-- --foo -- --bar 2', NS(cmd='--foo', foo=None, rest=['--', '--bar', '2'])),
+ # NS(cmd='--foo', foo=None, rest=['--bar', '2']) old
+
+ ('-f1 -- -- 1 -- 2', NS(cmd='--', foo='1', rest=['1', '--', '2'])),
+ # NS(cmd=[], foo='1', rest=['1', '2']) older, note cmd=[]
+ # NS(cmd='--', foo='1', rest=['1', '2']) old
+
+ ('-- cmd -- -- --foo', NS(cmd='cmd', foo=None, rest=['--', '--', '--foo'])),
+ # NS(cmd='cmd', foo=None, rest=['--foo']) older
+ # NS(cmd='cmd', foo=None, rest=['--', '--foo']) old
+ ]
+
+class TestDoubleDashRemoval1(ParserTestCase):
+ """Test actions with multiple -- values, with '+' positional"""
+
+ argument_signatures = [
+ Sig('-f', '--foo', help='an optional'),
+ Sig('cmd', help='a command'),
+ Sig('rest', nargs='+', help='1 or more args'),
+ ]
+ failures = ['cmd -f1', '-f1 -- cmd', '-f1 cmd --']
+ successes = [
+ ('cmd -f1 2 3', NS(cmd='cmd', foo='1', rest=['2', '3'])),
+ ('cmd -f1 -- 2 3', NS(cmd='cmd', foo='1', rest=['2', '3'])),
+ ('-f1 -- cmd -- -f2 3', NS(cmd='cmd', foo='1', rest=['--', '-f2', '3'])),
+ ]
class TestDefaultSuppress(ParserTestCase):
"""Test actions with suppressed defaults"""