Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

argparse: allow_abbrev=False stops -vv from working #71154

Closed
Meinersbur mannequin opened this issue May 5, 2016 · 13 comments
Closed

argparse: allow_abbrev=False stops -vv from working #71154

Meinersbur mannequin opened this issue May 5, 2016 · 13 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@Meinersbur
Copy link
Mannequin

Meinersbur mannequin commented May 5, 2016

BPO 26967
Nosy @blueyed, @encukou, @berkerpeksag, @zhangyangyu, @Meinersbur, @miss-islington, @lukash
PRs
  • bpo-26967: fix flag grouping with allow_abbrev=False #14316
  • [3.8] bpo-26967: fix flag grouping with allow_abbrev=False (GH-14316) #14759
  • bpo-39546: argparse: Honor allow_abbrev=False for specified prefix_chars #18337
  • [3.8] bpo-39546: argparse: Honor allow_abbrev=False for specified prefix_chars (GH-18337) #18543
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2019-07-14.07:40:56.320>
    created_at = <Date 2016-05-05.19:04:46.627>
    labels = ['3.7', '3.8', 'type-bug', 'library', '3.9']
    title = 'argparse: allow_abbrev=False stops -vv from working'
    updated_at = <Date 2020-02-18.11:14:11.272>
    user = 'https://github.com/Meinersbur'

    bugs.python.org fields:

    activity = <Date 2020-02-18.11:14:11.272>
    actor = 'miss-islington'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-07-14.07:40:56.320>
    closer = 'petr.viktorin'
    components = ['Library (Lib)']
    creation = <Date 2016-05-05.19:04:46.627>
    creator = 'meinersbur'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 26967
    keywords = ['patch', '3.5regression']
    message_count = 13.0
    messages = ['264915', '264930', '264931', '264975', '265004', '265161', '265167', '337379', '347169', '347864', '347865', '362184', '362194']
    nosy_count = 9.0
    nosy_names = ['bethard', 'blueyed', 'petr.viktorin', 'berker.peksag', 'paul.j3', 'xiang.zhang', 'meinersbur', 'miss-islington', 'lukash']
    pr_nums = ['14316', '14759', '18337', '18543']
    priority = 'high'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue26967'
    versions = ['Python 3.5', 'Python 3.6', 'Python 3.7', 'Python 3.8', 'Python 3.9']

    @Meinersbur
    Copy link
    Mannequin Author

    Meinersbur mannequin commented May 5, 2016

    #! /usr/bin/env python3
    import argparse
    
    parser = argparse.ArgumentParser(allow_abbrev=True)
    parser.add_argument('-v', '--verbose', action='count')
    print(parser.parse_args(['-vv']))
    
    parser = argparse.ArgumentParser(allow_abbrev=False)
    parser.add_argument('-v', '--verbose', action='count')
    print(parser.parse_args(['-vv']))

    Observed Output:
    Namespace(verbose=2)
    usage: test.py [-h] [-v]
    test.py: error: unrecognized arguments: -vv

    Expected Output:
    Namespace(verbose=2)
    Namespace(verbose=2)

    @Meinersbur Meinersbur mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels May 5, 2016
    @zhangyangyu
    Copy link
    Member

    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.

    @zhangyangyu
    Copy link
    Member

    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?

    @Meinersbur
    Copy link
    Mannequin Author

    Meinersbur mannequin commented May 6, 2016

    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.

    @zhangyangyu
    Copy link
    Member

    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.

    @paulj3
    Copy link
    Mannequin

    paulj3 mannequin commented May 8, 2016

    It's been 2 years since I worked on this patch. In the issue discussion http://bugs.python.org/issue14910 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.

    @paulj3
    Copy link
    Mannequin

    paulj3 mannequin commented May 8, 2016

    Someone needs to take the current argparse.py, 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?

    @lukash
    Copy link
    Mannequin

    lukash mannequin commented Mar 7, 2019

    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.

    @blueyed
    Copy link
    Mannequin

    blueyed mannequin commented Jul 2, 2019

    #14316 has a fix.

    @blueyed blueyed mannequin added 3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes labels Jul 2, 2019
    @miss-islington
    Copy link
    Contributor

    New changeset dffca9e by Miss Islington (bot) (Zac Hatfield-Dodds) in branch 'master':
    bpo-26967: fix flag grouping with allow_abbrev=False (GH-14316)
    dffca9e

    @encukou
    Copy link
    Member

    encukou commented Jul 14, 2019

    New changeset b1e4d1b by Petr Viktorin (Miss Islington (bot)) in branch '3.8':
    bpo-26967: fix flag grouping with allow_abbrev=False (GH-14316) (GH-14759)
    b1e4d1b

    @encukou encukou closed this as completed Jul 14, 2019
    @miss-islington
    Copy link
    Contributor

    New changeset 8edfc47 by Kyle Meyer in branch 'master':
    bpo-39546: argparse: Honor allow_abbrev=False for specified prefix_chars (GH-18337)
    8edfc47

    @miss-islington
    Copy link
    Contributor

    New changeset e412cbb 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)
    e412cbb

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants