Author mblahay
Recipients bethard, mblahay, paul.j3, rgov
Date 2019-05-10.19:21:20
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1557516080.62.0.546192763277.issue35495@roundup.psfhosted.org>
In-reply-to
Content
Here is another take on the issue, this time illustrated through the lens of optional arguments.

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--foo', nargs=1,default=['none'])
parser.add_argument('--baz', nargs='*', default=['nada'])
parser.add_argument('--bar', nargs=argparse.REMAINDER,default=['nothing'])
parser.parse_args('--foo a --bar b --baz c'.split())

Out[9]: Namespace(bar=['b', '--baz', 'c'], baz=['nada'], foo=['a'])

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--foo', nargs=1,default=['none'])
parser.add_argument('--baz', nargs='*', default=['nada'])
parser.add_argument('--bar', nargs=argparse.REMAINDER,default=['nothing'])
parser.parse_args('--foo a --baz b --bar c'.split())

Out[10]: Namespace(bar=['c'], baz=['b'], foo=['a'])

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--foo', nargs=1,default=['none'])
parser.add_argument('--baz', nargs='*', default=['nada'])
parser.add_argument('--bar', nargs=argparse.REMAINDER,default=['nothing'])
parser.parse_args([])

Out[11]: Namespace(bar=['nothing'], baz=['nada'], foo=['none'])

It is important to note that when an optional argument is not present then the default is always used, including for one using nargs=argparse.REMAINDER. In all three tests, bar is the argument using REMAIDER. In the first test, one can see that when bar isn't the last argument then anything else, including other arguments, are swept up as being arguments of bar. This greedy behavior for REMAINDER is something that * does not share (test 2).
History
Date User Action Args
2019-05-10 19:21:20mblahaysetrecipients: + mblahay, bethard, paul.j3, rgov
2019-05-10 19:21:20mblahaysetmessageid: <1557516080.62.0.546192763277.issue35495@roundup.psfhosted.org>
2019-05-10 19:21:20mblahaylinkissue35495 messages
2019-05-10 19:21:20mblahaycreate