[issue16878] argparse: positional args with nargs='*' defaults to []

paul j3 report at bugs.python.org
Thu Sep 19 00:16:57 CEST 2013


paul j3 added the comment:

On a related point, the 'action.required' value is set differently for '?' and '*' positionals.  

    if kwargs.get('nargs') not in [OPTIONAL, ZERO_OR_MORE]:
        kwargs['required'] = True
    if kwargs.get('nargs') == ZERO_OR_MORE and 'default' not in kwargs:
        kwargs['required'] = True

OPTIONAL is always not required, ZERO_OR_MORE is not required if it has a default.  But for reasons discussed here, that 'required' value makes little difference.  

`parse_args` checks that all 'required' arguments have been seen, but a ZERO_OR_MORE positional is always seen (i.e. it matches an empty string).  

Usage formatting always uses '[%s [%s ...]]' with ZERO_OR_MORE, regardless of the 'required' attribute.

The only place where this 'required' value seems to matter is when adding such an argument to a mutually exclusive group.  But if an unused '*' positional is going to get a '[]' value anyways, why should it be excluded from such a use?

If I remove the 

    if kwargs.get('nargs') == ZERO_OR_MORE and 'default' not in kwargs:

test, test_argparse.py still runs fine.

http://bugs.python.org/issue18943 is a possibly related issue, involving  a 'is not action.default' test in a mutually exclusive group.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue16878>
_______________________________________


More information about the Python-bugs-list mailing list