[issue21633] Argparse does not propagate HelpFormatter class to subparsers

Michael Cohen report at bugs.python.org
Mon Jun 2 01:32:36 CEST 2014


New submission from Michael Cohen:

Argparse has an option to set the custom help formatter class as a kwarg. For example one can define:


class MyHelpFormatter(argparse.RawDescriptionHelpFormatter):
    def add_argument(self, action):
        if action.dest != "SUPPRESS":
            super(RekallHelpFormatter, self).add_argument(action)


parser = ArguementParser(
   formatter_class=MyHelpFormatter)


But when one creates a subparser there is no way to define the formatter class for it - i.e. parser.add_subparsers() does not accept a formatter_class parameter. Instead we see this code:

    def add_subparsers(self, **kwargs):
        ...
        # add the parser class to the arguments if it's not present
        kwargs.setdefault('parser_class', type(self))

The only way to make this work is to extend ArguementParser to force it to use the formatter_class through inheritance:

class MyArgParser(argparse.ArgumentParser):

    def __init__(self, **kwargs):
        kwargs["formatter_class"] = MyHelpFormatter
	super(MyArgParser, self).__init__(**kwargs)

this is counter intuitive since formatter_class can be passed to the constructor but then it is not propagated to subparsers.

IMHO the expect action here is to have the formatter_class automatically propagates to subparsers as well. Short of that we need to be able to specific it in add_subparser() call.

----------
components: Extension Modules
messages: 219534
nosy: Michael.Cohen
priority: normal
severity: normal
status: open
title: Argparse does not propagate HelpFormatter class to subparsers
type: behavior
versions: Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue21633>
_______________________________________


More information about the Python-bugs-list mailing list