GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — argparse.ArgumentParser.add_mutually_exclusive_group : metavar create parenthesis undefined behavior · Issue #89743 · python/cpython

argparse.ArgumentParser.add_mutually_exclusive_group : metavar create parenthesis undefined behavior #89743

New issue
Copy link
New issue
Copy link
Closed
Closed
argparse.ArgumentParser.add_mutually_exclusive_group : metavar create parenthesis undefined behavior#89743
Copy link
Labels
3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes3.9 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

AbcSxyZ
mannequin
opened on Oct 22, 2021
Issue body actions
BPO 45580
Nosy @lysnikolaou, @pablogsal, @sobolevn, @AbcSxyZ

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 = None
created_at = <Date 2021-10-22.19:31:30.976>
labels = ['type-bug', 'library', '3.9', '3.10', '3.11']
title = 'argparse.ArgumentParser.add_mutually_exclusive_group : metavar create parenthesis undefined behavior'
updated_at = <Date 2021-10-25.16:32:34.545>
user = 'https://github.com/AbcSxyZ'

bugs.python.org fields:

activity = <Date 2021-10-25.16:32:34.545>
actor = 'paul.j3'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2021-10-22.19:31:30.976>
creator = 'AbcSxyZ'
dependencies = []
files = []
hgrepos = []
issue_num = 45580
keywords = []
message_count = 4.0
messages = ['404815', '404891', '404893', '404981']
nosy_count = 5.0
nosy_names = ['paul.j3', 'lys.nikolaou', 'pablogsal', 'sobolevn', 'AbcSxyZ']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue45580'
versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

Activity

  1. AbcSxyZ commented on Oct 22, 2021

    @AbcSxyZ
    AbcSxyZmannequin
    on Oct 22, 2021
    MannequinAuthor
    More actions

    Hi,

    I'm getting a kind of undefined behavior where parenthesis seem handled in a strange way. On display, it has a conflict between parenthesis of the option, and nested parenthesis within a metavar.

    ## Reproduction script

    import argparse
    
    def main():
        parser = argparse.ArgumentParser()
    
        group = parser.add_mutually_exclusive_group(required=True)
    
        group.add_argument("-p", "--path", metavar="/var/www/html", 
                help="DocumentRoot path")
        group.add_argument("-r", "--reverse", metavar="http)s(://Host:Port",
                help="Reverse proxy address")
    
        parser.add_argument("--last-args")
        return parser.parse_args()
    
    main()
    

    ## Output of help menu

    usage: crash.py [-h] (-p /var/www/html | -r http)s://Host:Port [--last-args LAST_ARGS]
    

    ## Expected behavior

    usage: crash.py [-h] (-p /var/www/html | -r http)s(://Host:Port) [--last-args LAST_ARGS]
    
  2. AbcSxyZ
    added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    3.9 (EOL)end of lifeend of life
    on Oct 22, 2021
  3. sobolevn commented on Oct 23, 2021

    @sobolevn
    sobolevn
    on Oct 23, 2021
    Member
    More actions

    I confirm this happens on all recent Python versions.

    The source of this problem is that argparse uses regex module to replace some substrings. Direct link: https://github.com/python/cpython/blame/8ce20bbdd6d2b1277a5e74154fcdcef2cb0fee49/Lib/argparse.py#L487

    Quick debug showed that without this line these tests fail:

    ======================================================================
    FAIL: test_help_when_required (test.test_argparse.TestMutuallyExclusiveFirstSuppressed)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/Users/sobolev/Desktop/cpython/Lib/test/test_argparse.py", line 2649, in test_help_when_required
        self.assertEqual(format_help(), textwrap.dedent(help))
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    AssertionError: 'usage: PROG [-h] (-y)\n\noptions:\n  -h, --help  show this[42 chars]lp\n' != 'usage: PROG [-h] -y\n\noptions:\n  -h, --help  show this h[40 chars]lp\n'
    - usage: PROG [-h] (-y)
    ?                  -  -
    + usage: PROG [-h] -y
      
      options:
        -h, --help  show this help message and exit
        -y          y help
    
    
    ======================================================================
    FAIL: test_usage_when_required (test.test_argparse.TestMutuallyExclusiveFirstSuppressed)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/Users/sobolev/Desktop/cpython/Lib/test/test_argparse.py", line 2639, in test_usage_when_required
        self.assertEqual(format_usage(), textwrap.dedent(expected_usage))
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    AssertionError: 'usage: PROG [-h] (-y)\n' != 'usage: PROG [-h] -y\n'
    - usage: PROG [-h] (-y)
    ?                  -  -
    + usage: PROG [-h] -y
    
    
    ======================================================================
    FAIL: test_help_when_required (test.test_argparse.TestMutuallyExclusiveFirstSuppressedParent)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/Users/sobolev/Desktop/cpython/Lib/test/test_argparse.py", line 2649, in test_help_when_required
        self.assertEqual(format_help(), textwrap.dedent(help))
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    AssertionError: 'usage: PROG [-h] (-y)\n\noptions:\n  -h, --help  show this[42 chars]lp\n' != 'usage: PROG [-h] -y\n\noptions:\n  -h, --help  show this h[40 chars]lp\n'
    - usage: PROG [-h] (-y)
    ?                  -  -
    + usage: PROG [-h] -y
      
      options:
        -h, --help  show this help message and exit
        -y          y help
    
    
    ======================================================================
    FAIL: test_usage_when_required (test.test_argparse.TestMutuallyExclusiveFirstSuppressedParent)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/Users/sobolev/Desktop/cpython/Lib/test/test_argparse.py", line 2639, in test_usage_when_required
        self.assertEqual(format_usage(), textwrap.dedent(expected_usage))
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    AssertionError: 'usage: PROG [-h] (-y)\n' != 'usage: PROG [-h] -y\n'
    - usage: PROG [-h] (-y)
    ?                  -  -
    + usage: PROG [-h] -y
    
    
    ----------------------------------------------------------------------
    Ran 1672 tests in 23.258s
    
    FAILED (failures=4)
    test test_argparse failed
    test_argparse failed (4 failures)
    
    == Tests result: FAILURE ==
    
    1 test failed:
        test_argparse
    
    Total duration: 25.6 sec
    Tests result: FAILURE
    
  4. sobolevn commented on Oct 23, 2021

    @sobolevn
    sobolevn
    on Oct 23, 2021
    Member
    More actions

    Maybe instead we can show users something like:

    usage: ex.py [-h] (-p '/var/www/html' | -r 'http)s(://Host:Port') [--last-args LAST_ARGS]
    

    ?

  5. sobolevn
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    3.10 (EOL)end of lifeend of life
    3.11only security fixesonly security fixes
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    and removed
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    on Oct 23, 2021
  6. paulj3 commented on Oct 25, 2021

    @paulj3
    paulj3mannequin
    on Oct 25, 2021
    Mannequin
    More actions

    The usage formatting is fragile, with many associated bug reports. Until someone does a major rewrite, it is best to avoid special characters, especially () and [] in the dest or metavar.

    Usage uses () to encolde mutually_exclusive_groups and [] to mark non-required arguments. Don't confuse your users (or argparse) with other uses of these characters.

  7. ezio-melotti
    transferred this issue fromon Apr 10, 2022
  8. erlend-aasland
    added this to Argparse issueson May 19, 2022
  9. erlend-aasland
    moved this to Bugs in Argparse issueson May 19, 2022
  10. hamdanal
    added a commit that references this issue on May 28, 2023

    Simplify argparse usage formatting

    This issue will close once commit 4336ac7 is merged into the 'main' branch.
    4336ac7
  11. encukou
    added a commit that references this issue on May 7, 2024

    gh-62090: Simplify argparse usage formatting (GH-105039)

    de1428f
  12. encukou commented on May 7, 2024

    @encukou
    encukou
    on May 7, 2024
    Member
    More actions

    This was fixed in #102318.

  13. encukou
    closed this as completedon May 7, 2024
  14. github-project-automation
    moved this from Bugs to Doc issues in Argparse issueson May 7, 2024
  15. SonicField
    added a commit that references this issue on May 8, 2024

    pythongh-62090: Simplify argparse usage formatting (pythonGH-105039)

    8047c0b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes3.9 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    • Argparse issues
      Status
      Doc issues
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.