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 charlie.proctor, paul.j3, rrt
Date 2016-11-06.20:35:17
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1478464518.53.0.973389389782.issue28609@psf.upfronthosting.co.za>
In-reply-to
Content
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.)
History
Date User Action Args
2016-11-06 20:35:18paul.j3setrecipients: + paul.j3, charlie.proctor, rrt
2016-11-06 20:35:18paul.j3setmessageid: <1478464518.53.0.973389389782.issue28609@psf.upfronthosting.co.za>
2016-11-06 20:35:18paul.j3linkissue28609 messages
2016-11-06 20:35:17paul.j3create