◐ Shell
clean mode source ↗

Message 187178 - Python tracker

The problem isn't with REMAINDER, but with the distinction between optionals and arguments.  If you change '--def' to 'def', the parse should work:

>>> p = ArgumentParser(prog='test.py')
>>> p.add_argument('remainder', nargs=argparse.REMAINDER)
>>> p.parse_args(['def'])

'--def' would give problems with almost all of the nargs options, especially '*' and '?'.

The issue is that '--def' looks like an optional.  Since it is not found in the defined arguments, it is classed as an unknown extra and skipped (try p.parse_known_args(['--def'])).  All of this takes place before 'REMAINDER' has a chance to look at the argument strings.

In http://bugs.python.org/issue9334   I submitted a patch that defines a 'args_default_to_positional' parser option.  If this is True, that unrecognized '--def' would be classed as a 'positional' and would be captured by the REMAINDER.