[issue16142] ArgumentParser inconsistent with parse_known_args

paul j3 report at bugs.python.org
Thu Apr 18 00:38:07 CEST 2013


paul j3 added the comment:

parser = argparse.ArgumentParser()
    parser.add_argument('-k','--known',action='store_true')
    print(parser.parse_known_args(['-k','-u']))
    print(parser.parse_known_args(['-ku']))
    print(parser.parse_known_args(['-uk']))

I think you want these 3 cases to produce the same output:

    (Namespace(known=True), ['-u'])

With the attached patch, '-ku' produces the same result as '-k -u'. Instead of raising the "ignored explicit argument 'u'" error, if puts '-u' in the 'extras' list.  'parse_args' then raises a "error: unrecognized arguments: u" error.

That's an easy change, and does not break anything in the 'test_argparse.py' file.  But keep in mind that this test file mostly uses 'parse_args', and usually it ignores the failure messages.

Getting '-uk' to work this way would be much harder.  While it isn't obvious from the documentation, '-uk' is a valid option string, and '-u' is a valid abbreviation. Notice in 16.4.4.1. of the documentation, the difference between long and short options is based on the number of characters, not whether there it starts with '-' or '--'.  So identifying what is wrong with '-uk' would require ambiguous reasoning.

I wonder what optparse does.

----------
keywords: +patch
nosy: +paul.j3
Added file: http://bugs.python.org/file29910/dashku.patch

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


More information about the Python-bugs-list mailing list