Title: Wrong parse long argument
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.7
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: chr0139, eric.smith, paul.j3, xtreak
Priority: normal Keywords:

Created on 2019-06-11 08:53 by chr0139, last changed 2019-06-13 05:11 by paul.j3. This issue is now closed.

Messages (5)
msg345203 - (view) Author: (chr0139) Date: 2019-06-11 08:53
I have this script

import argparse
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument('-l', '--list', action='store_true', help="show help")
group.add_argument('-u', '--upgrade', action='store_true', help="show help")

and this is the unexpected result:

Namespace(list=True, upgrade=False)
msg345204 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2019-06-11 08:57
And what is your expected result, and why?
msg345206 - (view) Author: (chr0139) Date: 2019-06-11 09:16
I think --li is not the same argument as --list

So I expect that

parser.parse_args(['-l']) or parser.parse_args(['--list'])

should be only two options which should leads to the result

Namespace(list=True, upgrade=False)
msg345207 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-06-11 09:18
argparse allows abbreviations by default so --li matches --list as a prefix match. This is documented at

From python 3.5 allow_abbrev=False can be passed to ArgumentParser to disable this. See also issue10981 and issue14910 that implemented this.
msg345208 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2019-06-11 09:26
Although note that with allow_abbrev=False, -l will conflict with --long, which I'm not sure is a great design.

I'm closing this as not a bug, since it works as documented and there's a workaround (albeit not in 2.7, which is closed to new features).
Date User Action Args
2019-06-13 05:11:18paul.j3setnosy: + paul.j3
2019-06-11 09:26:10eric.smithsetstatus: open -> closed
resolution: not a bug
messages: + msg345208

stage: resolved
2019-06-11 09:18:48xtreaksetmessages: + msg345207
2019-06-11 09:16:12chr0139setmessages: + msg345206
2019-06-11 09:08:41xtreaksetnosy: + xtreak
2019-06-11 08:57:39eric.smithsetnosy: + eric.smith
messages: + msg345204
2019-06-11 08:53:06chr0139create