paul j3 added the comment:

Try `nargs='?'` or try providing a `default` along with the '*'.

Including your ARGUMENT action in the error message is intentional.

The test for this error message is:

        required_actions = []
        for action in self._actions:
            if action not in seen_actions:
                if action.required:

Originally the code just checked if `positionals` was empty.  That was the list 
of positional Actions.  Actions were popped as they were parsed.  Now it 
maintains a set `seen_actions`, and checks the `required` attribute.  This test 
applies to both positionals and optionals.

For optionals, `required` is set by the programmer.  But for positionals it is 
set with:

    def _get_positional_kwargs
         ...
        # mark positional arguments as required if at least one is
        # always required
        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

So for '?' argument, required is False.  But for '*', it must also have a 
'default' parameter (not just the default None).

So the proposed patch is overriding the 'required' value that was set during 
'add_argument'.  And issuing this error message is the main purpose of the 
'required' attribute.

I would not implement this patch.  

But it would be a good idea to check if this method of setting the required 
attribute has been discussed in other bug/issues.  (There is an open issue 
concerning how the 'required' is set/or not for the subparsers positional.)

Off hand I don't see anything about this in the documentation.  Maybe that's 
what needs to be patched.  (It's easier and safer to change the documentation 
than the working code.  Backward compatibility is a major concern when changing 
the code.)

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue28609>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to