Skip to content

argparse usage should preserve () in metavars such as range(20) #62549

Description

@paulj3
mannequin
BPO 18349
Nosy @iritkatriel
Files
  • metaparen.patch
  • 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 2013-07-03.06:16:51.100>
    labels = ['type-bug', 'library', '3.9', '3.10', '3.11']
    title = 'argparse usage should preserve () in metavars such as range(20)'
    updated_at = <Date 2021-12-10.15:48:45.716>
    user = 'https://bugs.python.org/paulj3'

    bugs.python.org fields:

    activity = <Date 2021-12-10.15:48:45.716>
    actor = 'iritkatriel'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2013-07-03.06:16:51.100>
    creator = 'paul.j3'
    dependencies = []
    files = ['30754']
    hgrepos = []
    issue_num = 18349
    keywords = ['patch']
    message_count = 5.0
    messages = ['192222', '192717', '193077', '193188', '408214']
    nosy_count = 2.0
    nosy_names = ['paul.j3', 'iritkatriel']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = None
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue18349'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    Activity

    1. paulj3 commented on Jul 3, 2013

      paulj3mannequin
      MannequinAuthor

      As discussed in bpo-16468, a metavar may be used to provide an alternative representation of a choices option. However if a metvar like 'range(20)' is used, usage formatter strips off the '()'.

          >>> parser.add_argument('foo', type=int, 
              choices=range(20), metavar='range(0,20)')
          >>> parser.format_usage()
          # expect: 'usage: PROG [-h] range(0,20)\n'
          # actual: 'usage: PROG [-h] range0,20\n'

      This is done by a line in the help formater that removes excess mutually exclusive group notation:

      HelpFormatter._format_actions_usage
         ...
         text = _re.sub(r'\(([^|]*)\)', r'\1', text)
      

      A solution is to change this line to distinguish between a case like ' (...)' and 'range(...)'

          text = _re.sub(r'( )\(([^|]*)\)', r'\1\2', text)
    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on Jul 3, 2013
    3. paulj3 commented on Jul 9, 2013

      paulj3mannequin
      MannequinAuthor

      I just posted a patch to http://bugs.python.org/issue16468 that uses (and tests) this fix.

    4. paulj3 commented on Jul 15, 2013

      paulj3mannequin
      MannequinAuthor

      This issue should also preserve a metavar like: '(one)two', i.e. '(' at the start.

      In http://bugs.python.org/issue10984 these _re replacements are applied to individual action strings as well as the whole usage line. So if () are to be removed from '[-h] (-y)', they should also be removed from '(-y)'.

    5. paulj3 commented on Jul 16, 2013

      paulj3mannequin
      MannequinAuthor

      I just submitted at patch to http://bugs.python.org/issue11874 that takes care of this issue as well.

      I rewrote _format_actions_usage() so it formats the parts directly, so there is no need cleanup or parse the full text string.

    6. iritkatriel commented on Dec 10, 2021

      @iritkatriel
      Member

      Reproduced on 3.11:

      >>> import argparse
      >>> parser = argparse.ArgumentParser()
      >>> parser.add_argument('foo', type=int, choices=range(20), metavar='range(0,20)')
      _StoreAction(option_strings=[], dest='foo', nargs=None, const=None, default=None, type=<class 'int'>, choices=range(0, 20), help=None, metavar='range(0,20)')
      >>> parser.format_usage()
      'usage: [-h] range0,20\n'
    7. transferred this issue fromon Apr 10, 2022
    8. added a commit that references this issue on May 28, 2023
      4336ac7
    9. added a commit that references this issue on May 7, 2024
    10. encukou commented on May 7, 2024

      @encukou
      Member

      This was fixed in #102318.

    11. moved this from Bugs to Doc issues in Argparse issueson May 7, 2024
    12. added a commit that references this issue on May 8, 2024
    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 life3.11only security fixes3.9 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

      Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions