This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author paul.j3
Recipients Michael.Edwards, chris.jerdonek, eric.araujo, idank, jaraco, paul.j3, rr2do2
Date 2014-07-02.06:40:44
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1404283246.32.0.734277943178.issue14174@psf.upfronthosting.co.za>
In-reply-to
Content
Here's a possible solution to the problem (assuming there really is one):

- redefine REMAINDER so it matches a '((?:A[AO]*)?)' pattern (where O is a string that looks like an optional flag, A an argument string).  I've added the condition that the first match (if any) must be an A.  It ends up being closer to the pattern for PARSER.

I included a patch from issue 15112, which delays the consumption of a positional that matches with 0 strings.

In the sample case for this issue, results with this patch are:

    args = parser.parse_args(['app', '--config', 'bar'])
    # Namespace(app='app', app_args=[], config='bar')

    args = parser.parse_args(['--config', 'bar', 'app'])
    # Namespace(app='app', app_args=[], config='bar')

    args = parser.parse_args(['app', 'args', '--config', 'bar'])
    # Namespace(app='app', app_args=['args', '--config', 'bar'], config=None)

In the last case, 'app_args' gets the rest of the strings because the first is a plain 'args'.  I believe this is consistent with the intuition expressed in this issue.

I've added one test case to test_argparse.TestNargsRemainder.  This is a TestCase that is similar to the above example.

    argument_signatures = [Sig('x'), Sig('y', nargs='...'), Sig('-z')]
    failures = ['', '-z', '-z Z']
    successes = [
        ('X', NS(x='X', y=[], z=None)),
        ('-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')), # new case
    ]

This patch runs test_argparse fine.  But there is a slight possibility that this patch will cause backward compatibility problems.  Some user might expect y=['-z','Z',...].  But that expectation has not been enshrined the test_argparse.

It may require a slight change to the documentation as well.
History
Date User Action Args
2014-07-02 06:40:46paul.j3setrecipients: + paul.j3, jaraco, eric.araujo, chris.jerdonek, idank, rr2do2, Michael.Edwards
2014-07-02 06:40:46paul.j3setmessageid: <1404283246.32.0.734277943178.issue14174@psf.upfronthosting.co.za>
2014-07-02 06:40:46paul.j3linkissue14174 messages
2014-07-02 06:40:45paul.j3create