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 exclusive group inside required exclusive group displays incorrectly #89853

Closed
acooke mannequin opened this issue Nov 2, 2021 · 4 comments
Closed

Argparse exclusive group inside required exclusive group displays incorrectly #89853

acooke mannequin opened this issue Nov 2, 2021 · 4 comments
Labels
3.10 only security fixes stdlib Python modules in the Lib dir

Comments

@acooke
Copy link
Mannequin

acooke mannequin commented Nov 2, 2021

BPO 45690
Nosy @iritkatriel
Superseder
  • bpo-22047: Deprecate unsupported nesting of argparse groups
  • 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 2021-12-16.15:38:30.093>
    created_at = <Date 2021-11-02.13:39:20.906>
    labels = ['library', '3.10']
    title = 'Argparse exclusive group inside required exclusive group displays incorrectly'
    updated_at = <Date 2021-12-16.15:38:30.091>
    user = 'https://bugs.python.org/acooke'

    bugs.python.org fields:

    activity = <Date 2021-12-16.15:38:30.091>
    actor = 'iritkatriel'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-12-16.15:38:30.093>
    closer = 'iritkatriel'
    components = ['Library (Lib)']
    creation = <Date 2021-11-02.13:39:20.906>
    creator = 'acooke'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 45690
    keywords = []
    message_count = 4.0
    messages = ['405509', '405521', '405531', '408715']
    nosy_count = 3.0
    nosy_names = ['acooke', 'paul.j3', 'iritkatriel']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '22047'
    type = None
    url = 'https://bugs.python.org/issue45690'
    versions = ['Python 3.10']

    @acooke
    Copy link
    Mannequin Author

    acooke mannequin commented Nov 2, 2021

    The code below, when invoked with -h, prints:

    (.env) [andrew@localhost py]$ python -m tests_sa.argparse_bug -h
    usage: argparse_bug.py [-h] (-a A | [-b B | -c C)]

    options:
    -h, --help show this help message and exit
    -a A
    -b B
    -c C

    where the final two characters in the usage line are swapped. It should be

    usage: argparse_bug.py [-h] (-a A | [-b B | -c C])

    or maybe even

    usage: argparse_bug.py [-h] (-a A | (-b B | -c C))

    from argparse import ArgumentParser
    
    def main():
        parser = ArgumentParser()
        outer = parser.add_mutually_exclusive_group(required=True)
        outer.add_argument('-a')
        inner = outer.add_mutually_exclusive_group()
        inner.add_argument('-b')
        inner.add_argument('-c')
        parser.parse_args()
    
    
    if __name__ == '__main__':
        main()

    @acooke acooke mannequin added 3.10 only security fixes stdlib Python modules in the Lib dir labels Nov 2, 2021
    @paulj3
    Copy link
    Mannequin

    paulj3 mannequin commented Nov 2, 2021

    There was a bug/issue that addressed problems with nested mutually_exclusive_groups. It should be easy to find.

    The problem is that the usage formatter is brittle, building a string and then striping out "unnecessary" characters. I assume the fix handled the default case (not required), but apparently it missed this variation.

    I wasn't too involved with that, since in my opinion nesting these groups is useless, and should be avoided. You might as well use one union group. Test the parsing for yourself.

    @paulj3
    Copy link
    Mannequin

    paulj3 mannequin commented Nov 2, 2021

    https://bugs.python.org/issue29553
    Argparser does not display closing parentheses in nested mutex groups

    supposedly fixed the parentheses for nested groups. You can read its discussion and patches to see why it does not handle your case.

    I don't see any examples have required groups. [] is used for un-required, () for required. This patch did not change how the usage was generated. It just refined how excess [()] were removed.

    Proper handling of groups requires a major rewrite of the usage formatter.

    @iritkatriel
    Copy link
    Member

    Nesting argument groups and mutually exclusive groups is now deprecated (see bpo-22047). Thank you for the bug report.

    @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.10 only security fixes stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant