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.

Title: argparse: allow_abbrev=False stops -vv from working
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.9, Python 3.8, Python 3.7, Python 3.6, Python 3.5
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: berker.peksag, bethard, blueyed, lukash, meinersbur, miss-islington, paul.j3, petr.viktorin, xiang.zhang
Priority: high Keywords: 3.5regression, patch

Created on 2016-05-05 19:04 by meinersbur, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 14316 merged Zac Hatfield-Dodds, 2019-06-23 03:34
PR 14759 merged miss-islington, 2019-07-14 05:36
PR 18337 merged kyleam, 2020-02-03 20:18
PR 18543 merged miss-islington, 2020-02-18 09:49
Messages (13)
msg264915 - (view) Author: Michael Kruse (meinersbur) Date: 2016-05-05 19:04
#! /usr/bin/env python3
import argparse

parser = argparse.ArgumentParser(allow_abbrev=True)
parser.add_argument('-v', '--verbose', action='count')

parser = argparse.ArgumentParser(allow_abbrev=False)
parser.add_argument('-v', '--verbose', action='count')

Observed Output:
usage: [-h] [-v] error: unrecognized arguments: -vv

Expected Output:
msg264930 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2016-05-06 03:19
This is a side effect introduced by 99302634d756. You have to set allow_abbrev to True to make such match succeed. I think this is a bug.
msg264931 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2016-05-06 04:14
After some research I suggest to document this behaviour that allow_abbrev=False will suspend option prefix matching. Simply fixing the count action behaviour is not enough since it also prevents you from creating custom actions that want to use option prefix matching. What's your advice berker?
msg264975 - (view) Author: Michael Kruse (meinersbur) Date: 2016-05-06 14:53
I think the allow_abbrev option should be orthogonal on how short options are parsed. I am using parse_known_args() to forward the unrecognized args to another program, therefore allow_abbrev=False is essential.

There is a special handling for short options in the consume_optional and _get_option_tuples to allow them being concatenated with one dash, as commonly done with short options (eg. "tar -czf file"). I interpret allow_abbrev as an option to avoid matching non-exiting options that should be forwarded to the other program; '-vv' is an existing option with the same meaning as '-v -v'.

This would also mean that parse_known_args(['-vz']) (where '-v' is a registered argument, but '-z' is not) matches '-v' and returns '-z' as unknown argument; but I don't know whether you want to go that far. It is difficult to interpret whether '-verify' should mean '-v -e -r -i -f -y' or '--verify' (but this is why there are double-dash options), especially when the first letter is not a registered short option.
msg265004 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2016-05-06 17:51
I agree with you. But right now, it seems parsing short options and allow_abbrev both rely on the same logic. If you turn off allow_abbrev, short options parsing also disabled. Separating them seems nontrivial.
msg265161 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2016-05-08 19:47
It's been 2 years since I worked on this patch.  In the issue discussion there was no mention of short v long options.  The unit tests only look at longs.

The result is that with the abbrev off, it disables all use of combined shorts, not just the count.  Not only if '-vv' disabled, so is '-vz', '-vz1', '-v -z1'.

We should have discussed that issue.

I can imagine modifying the 

    if self.allow_abbrev:

to something like

    if self.allow_abbrev or <argstring has only one dash>:
        <search for abbreviations>

But even if we don't go that far, we should add a documentation note.
msg265167 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2016-05-08 20:55
Someone needs to take the current, set the default value of this parameter to False, and run the unittest file.  This should turn up this case, and possibly others that fail when abbreviations are turned off.  Then we have to debate whether such failures are acceptable or not.

When we say 'disable abbreviations' do we mean, all abbreviations, or just a subset?
msg337379 - (view) Author: Lukáš Hrázký (lukash) Date: 2019-03-07 09:45
Sad to see this still wasn't fixed. The abbreviation "feature" creates ambiguity and room for error and this bug makes the switch unusable for a lot of applications.
msg347169 - (view) Author: daniel hahler (blueyed) * Date: 2019-07-02 23:04 has a fix.
msg347864 - (view) Author: miss-islington (miss-islington) Date: 2019-07-14 05:36
New changeset dffca9e925ee5c3072663cbe8d4d4768406d5307 by Miss Islington (bot) (Zac Hatfield-Dodds) in branch 'master':
bpo-26967: fix flag grouping with allow_abbrev=False (GH-14316)
msg347865 - (view) Author: Petr Viktorin (petr.viktorin) * (Python committer) Date: 2019-07-14 06:00
New changeset b1e4d1b6032d4c82b549233fa08a2c7cfe7e818b by Petr Viktorin (Miss Islington (bot)) in branch '3.8':
bpo-26967: fix flag grouping with allow_abbrev=False (GH-14316) (GH-14759)
msg362184 - (view) Author: miss-islington (miss-islington) Date: 2020-02-18 09:49
New changeset 8edfc47baec7ff4cb1b9db83dd35c8ffc1d498a4 by Kyle Meyer in branch 'master':
bpo-39546: argparse: Honor allow_abbrev=False for specified prefix_chars (GH-18337)
msg362194 - (view) Author: miss-islington (miss-islington) Date: 2020-02-18 11:14
New changeset e412cbba52e7cf6699720d99a4b88baef92db7b2 by Miss Islington (bot) in branch '3.8':
[3.8] bpo-39546: argparse: Honor allow_abbrev=False for specified prefix_chars (GH-18337) (GH-18543)
Date User Action Args
2022-04-11 14:58:30adminsetgithub: 71154
2020-02-18 11:14:11miss-islingtonsetmessages: + msg362194
2020-02-18 09:49:13miss-islingtonsetpull_requests: + pull_request17921
2020-02-18 09:49:07miss-islingtonsetmessages: + msg362184
2020-02-03 20:18:15kyleamsetpull_requests: + pull_request17710
2019-07-14 07:40:56petr.viktorinsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-07-14 06:00:04petr.viktorinsetnosy: + petr.viktorin
messages: + msg347865
2019-07-14 05:36:12miss-islingtonsetpull_requests: + pull_request14553
2019-07-14 05:36:03miss-islingtonsetnosy: + miss-islington
messages: + msg347864
2019-07-02 23:04:39blueyedsetnosy: + blueyed

messages: + msg347169
versions: + Python 3.7, Python 3.8, Python 3.9
2019-06-23 03:34:25Zac Hatfield-Doddssetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request14140
2019-03-07 09:45:36lukashsetnosy: + lukash
messages: + msg337379
2017-11-16 08:01:13berker.peksaglinkissue32027 superseder
2016-05-08 20:55:03paul.j3setmessages: + msg265167
2016-05-08 19:47:53paul.j3setpriority: normal -> high
2016-05-08 19:47:36paul.j3setnosy: + paul.j3
messages: + msg265161
2016-05-06 17:51:41xiang.zhangsetmessages: + msg265004
2016-05-06 14:53:26meinersbursetmessages: + msg264975
2016-05-06 04:14:59xiang.zhangsetnosy: + bethard
messages: + msg264931
2016-05-06 03:19:37xiang.zhangsetmessages: + msg264930
2016-05-06 02:55:10xiang.zhangsetnosy: + xiang.zhang
2016-05-05 19:16:04berker.peksagsetkeywords: + 3.5regression
nosy: + berker.peksag
stage: needs patch

versions: + Python 3.6
2016-05-05 19:04:46meinersburcreate