Skip to content

argparse: comma in metavar causes assertion failure when formatting long usage message #60564

Closed

Description

@bgamari
mannequin
BPO 16360
Nosy @tirkarthi
Files
  • hi.py: Testcase
  • 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 2018-09-23.01:20:22.235>
    created_at = <Date 2012-10-29.16:49:08.803>
    labels = ['type-bug', 'library']
    title = 'argparse: comma in metavar causes assertion failure when formatting long usage message'
    updated_at = <Date 2018-09-23.01:20:22.233>
    user = 'https://bugs.python.org/bgamari'

    bugs.python.org fields:

    activity = <Date 2018-09-23.01:20:22.233>
    actor = 'paul.j3'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-09-23.01:20:22.235>
    closer = 'paul.j3'
    components = ['Library (Lib)']
    creation = <Date 2012-10-29.16:49:08.803>
    creator = 'bgamari'
    dependencies = []
    files = ['27778']
    hgrepos = []
    issue_num = 16360
    keywords = []
    message_count = 4.0
    messages = ['174130', '193735', '326097', '326123']
    nosy_count = 3.0
    nosy_names = ['bgamari', 'paul.j3', 'xtreak']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue16360'
    versions = ['Python 2.7']

    Activity

    1. bgamari commented on Oct 29, 2012

      bgamarimannequin
      MannequinAuthor

      argparse suffers from a failing assertion when formatting a long usage message with an option whose metavar contains a comma. This can be seen in the attached testcase, which fails with,

      Traceback (most recent call last):
        File "/home/ben/hi.py", line 24, in <module>
          args = parser.parse_args()
        File "/usr/lib/python2.7/argparse.py", line 1688, in parse_args
          args, argv = self.parse_known_args(args, namespace)
        File "/usr/lib/python2.7/argparse.py", line 1720, in parse_known_args
          namespace, args = self._parse_known_args(args, namespace)
        File "/usr/lib/python2.7/argparse.py", line 1937, in _parse_known_args
          self.error(_('too few arguments'))
        File "/usr/lib/python2.7/argparse.py", line 2360, in error
          self.print_usage(_sys.stderr)
        File "/usr/lib/python2.7/argparse.py", line 2322, in print_usage
          self._print_message(self.format_usage(), file)
        File "/usr/lib/python2.7/argparse.py", line 2278, in format_usage
          return formatter.format_help()
        File "/usr/lib/python2.7/argparse.py", line 279, in format_help
          help = self._root_section.format_help()
        File "/usr/lib/python2.7/argparse.py", line 209, in format_help
          func(*args)
        File "/usr/lib/python2.7/argparse.py", line 330, in _format_usage
          assert ' '.join(opt_parts) == opt_usage
      AssertionError

      This failure is apparently due to a regular expression used to wrap the usage message. The solution to this is unclear, but it seems to me using a regular expression here is a hack at best.

    2. added
      type-crash
      stdlib
      on Oct 29, 2012
    3. paulj3 commented on Jul 26, 2013

      paulj3mannequin
      Mannequin

      My rewrite of _format_actions_usage in http://bugs.python.org/issue11874 should take care of this issue. It keeps the groups and actions separate until the final formatting step, bypassing the regular expression parsing.

    4. added
      type-bug
      and removed
      type-crash
      on Jul 11, 2018
    5. tirkarthi commented on Sep 22, 2018

      @tirkarthi
      Member

      Thanks @paul.j3 for the PR. The PR in the linked issue was merged and I cannot reproduce the original assertion error in the latest 2.7 branch with the script.

      $ ./python.exe
      Python 2.7.15+ (remotes/upstream/2.7:10be1d3f80, Sep 22 2018, 22:10:28)
      [GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
      Type "help", "copyright", "credits" or "license" for more information.

      >>
      $ ./python.exe ../backups/bpo16360.py
      usage: bpo-16360.py [-h] [-w TIME] [-r N] [-j TIME] [-W TIME]
      [-y COUNT|max|avg] [-o FILE] [-c CHANNEL[=LABEL],...]
      FILE [FILE ...]
      bpo-16360.py: error: too few arguments

      Thanks

    6. paulj3 commented on Sep 23, 2018

      paulj3mannequin
      Mannequin
    7. transferred this issue fromon Apr 10, 2022
    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

      stdlibtype-bug

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions