Title: argparse: comma in metavar causes assertion failure when formatting long usage message
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: bgamari, paul.j3, xtreak
Priority: normal Keywords:

Created on 2012-10-29 16:49 by bgamari, last changed 2018-09-23 01:20 by paul.j3. This issue is now closed.

File name Uploaded Description Edit bgamari, 2012-10-29 16:49 Testcase
Messages (4)
msg174130 - (view) Author: Ben Gamari (bgamari) Date: 2012-10-29 16:49
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/", line 24, in <module>
    args = parser.parse_args()
  File "/usr/lib/python2.7/", line 1688, in parse_args
    args, argv = self.parse_known_args(args, namespace)
  File "/usr/lib/python2.7/", line 1720, in parse_known_args
    namespace, args = self._parse_known_args(args, namespace)
  File "/usr/lib/python2.7/", line 1937, in _parse_known_args
    self.error(_('too few arguments'))
  File "/usr/lib/python2.7/", line 2360, in error
  File "/usr/lib/python2.7/", line 2322, in print_usage
    self._print_message(self.format_usage(), file)
  File "/usr/lib/python2.7/", line 2278, in format_usage
    return formatter.format_help()
  File "/usr/lib/python2.7/", line 279, in format_help
    help = self._root_section.format_help()
  File "/usr/lib/python2.7/", line 209, in format_help
  File "/usr/lib/python2.7/", line 330, in _format_usage
    assert ' '.join(opt_parts) == opt_usage

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.
msg193735 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2013-07-26 17:26
My rewrite of _format_actions_usage in should take care of this issue.  It keeps the groups and actions separate until the final formatting step, bypassing the regular expression parsing.
msg326097 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2018-09-22 16:44
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/
usage: [-h] [-w TIME] [-r N] [-j TIME] [-W TIME]
                   [-y COUNT|max|avg] [-o FILE] [-c CHANNEL[=LABEL],...]
                   FILE [FILE ...] error: too few arguments

msg326123 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2018-09-23 01:20
I'm closing this since the fix also handles this issue.
Date User Action Args
2018-09-23 01:20:22paul.j3setstatus: open -> closed
resolution: fixed
messages: + msg326123

stage: resolved
2018-09-22 16:44:28xtreaksetnosy: + xtreak
messages: + msg326097
2018-07-11 07:33:21serhiy.storchakasettype: crash -> behavior
2013-07-26 17:26:03paul.j3setnosy: + paul.j3
messages: + msg193735
2012-10-29 16:49:08bgamaricreate