paul j3 added the comment:

I think that this string falls through to the last case in 
'parser._parse_optional' (the first parsing loop)

        # it was meant to be an optional but there is no such option
        # in this parser (though it might be a valid option in a subparser)
        return None, arg_string, None

It has the format of a optional flag, not a positional argument.  If preceded 
by '--' it gets classed as argument.

(In the second, main, parsing loop) Since it doesn't match any defined Actions 
it gets put in the list of 'extras' (as returned by 'parse_known_args').  But 
the parser also runs a check on required arguments, and finds the positional, 
'first', was not filled.  So that's the error that's raised.

For example if I provide another string that fills the positional:

    In [5]: parser.parse_known_args(['-_','other'])
    Out[5]: (Namespace(first='other'), ['-_'])

'parse_args' would produce a 'error: unrecognized arguments: -_' error.

I don't see how the error message could be improved without some major changes 
in the testing and parsing.  It would either have to disallow unmatched 
optional's flags (and maybe break subparsers) or deduce that this 'extra' was 
meant for the unfilled positional.  Bernard has argued that it is better to 
raise an error in ambiguous cases, than to make too many assumptions about what 
the user intended.

----------

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

Reply via email to