diff -r 44f455e6163d Lib/argparse.py --- a/Lib/argparse.py Thu Jun 27 12:23:29 2013 +0200 +++ b/Lib/argparse.py Tue Jul 01 22:53:00 2014 -0700 @@ -1903,6 +1903,11 @@ selected_pattern = arg_strings_pattern[start_index:] arg_counts = match_partial(positionals, selected_pattern) + if 'O' in selected_pattern: # better issue 15112 test + while arg_counts and arg_counts[-1] == 0: + arg_counts.pop() + + # slice off the appropriate arg strings for each Positional # and add the Positional and its args to the list for action, arg_count in zip(positionals, arg_counts): @@ -2188,8 +2193,9 @@ nargs_pattern = '(-*A[A-]*)' # allow any number of options or arguments + # an optional pattern that starts with an A elif nargs == REMAINDER: - nargs_pattern = '([-AO]*)' + nargs_pattern = '(-*(?:A[-AO]*)?)' # allow one argument followed by any number of options or arguments elif nargs == PARSER: diff -r 44f455e6163d Lib/test/test_argparse.py --- a/Lib/test/test_argparse.py Thu Jun 27 12:23:29 2013 +0200 +++ b/Lib/test/test_argparse.py Tue Jul 01 22:53:00 2014 -0700 @@ -1252,6 +1252,7 @@ ('-z Z X', NS(x='X', y=[], z='Z')), ('X A B -z Z', NS(x='X', y=['A', 'B', '-z', 'Z'], z=None)), ('X Y --foo', NS(x='X', y=['Y', '--foo'], z=None)), + ('X -z Z A B', NS(x='X', y=['A', 'B'], z='Z')), ]