Mysterious crash in Python 2.7.5 argparse.add_argument () with custom formatter_class

I am trying to write a subclass of HelpFormatter for use with argparse. Formatting is easy; integrate it as a subclass no. I found a very useful example at stackoverflow.com/questions/3853722/, in response from Anthon.

Using Python 2.7.5 on Mac OS X 10.9.4. When I try to subclass HelpFormatter, I keep getting:

./testBlankLineHelpFormatter.py -q
******* LOADING MY CLASS
Instantiating argparse.ArgumentParser
Traceback (most recent call last):
  File "./testBlankLineHelpFormatter.py", line 15, in <module>
    formatter_class=BlankLineHelpFormatter
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 1600, in __init__
    help=_('show this help message and exit'))
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 1293, in add_argument
    raise ValueError("length of metavar tuple does not match nargs")
ValueError: length of metavar tuple does not match nargs

* Please note that an error occurs when creating an instance of my subclass, when the standard class tries to add the "--help" element - it never gets into any of my add_argument () calls. I clipped my subclass before this, and it still fails:

class BlankLineHelpFormatter(argparse.HelpFormatter):
    """
A formatter for argparse that just respects blank lines (as in, doesn't
wrap across them).

See also: http://bugs.python.org/issue12806

"""

    sys.stderr.write("******* LOADING MY CLASS\n")

    def __init__(self, *args, **kw):
        sys.stderr.write("******* IN MY INIT\n")
        super(BlankLineHelpFormatter, self).__init__(*args, **kw)

* I run it with a driver, which I also trimmed, before that:

#!/usr/bin/python
#
from __future__ import print_function
import argparse
import BlankLineHelpFormatter

print("Instantiating argparse.ArgumentParser")

parser = argparse.ArgumentParser(
    description="""
This work is licensed under a Creative Commons
Attribution-Share Alike 3.0 Unported License. For further information on
this license, look it up.
    """,
    formatter_class=BlankLineHelpFormatter
)

print("Adding --quiet\n")
parser.add_argument(
    "--quiet", "-q",      action='store_true',
    help='Suppress most messages.')

print("Instantiated, now trying parse_args")
args = parser.parse_args()

print("Back.")

print("You might want to try '-h'...")

sys.exit(0)

argparse, . , HelpFormatter, - ?

!

+4
2

BlankLineHelpFormatter , BlankLineHelpFormatter. argparse:

        # raise an error if the metavar does not match the type
        if hasattr(self, "_get_formatter"):
            try:
                self._get_formatter()._format_args(action, None)
            except TypeError:
                raise ValueError("length of metavar tuple does not match nargs")

_get_formatter() , TypeError - .

formatter_class=BlankLineHelpFormatter.BlankLineHelpFormatter
+2

argparse http://derose.net/steve/utilities/PY/MarkupHelpFormatter.py ( CCLI Attribution-Share-like). @user2357112 !

ANSI. (, - ), .

"MarkupHelpFormatter" mediaWiki, MarkDown POD ( ):

import MarkupHelpFormatter
MarkupHelpFormatter.InputOptions["mediawiki"] = True
parser = argparse.ArgumentParser(
    description="""...your text, with mediawiki markup...""",
    epilog='...',
    formatter_class=MarkupHelpFormatter.MarkupHelpFormatter
)

"ParagraphHelpFormatter" argparse, , . , , .. .

0

All Articles