paul j3 added the comment:

Here's a function that implements the format string:

    def custom_help(template):
        def usage(self):
            formatter = self._get_formatter()
            formatter.add_usage(self.usage, self._actions,
                self._mutually_exclusive_groups, prefix='')
            return formatter.format_help().strip()
        def groups(self):
            formatter = self._get_formatter()
            for action_group in self._action_groups:
                 formatter.start_section(action_group.title)
                 formatter.add_text(action_group.description)
                 formatter.add_arguments(action_group._group_actions)
                 formatter.end_section()
            astr = formatter.format_help().rstrip()
            return astr
        dd = dict(
            usage=usage(parser),
            argument_groups=groups(parser),
            )
        return template%dd

     template = """My Program, version 3.5
     Usage: %(usage)s

     Some description of my program

     %(argument_groups)s

     My epilog text
     """
     print(custom_help(template))

This replaces 'parser.format_help' rather than the 'HelpFormatter' class.  It 
in effect uses pieces from 'format_help' to format strings like 'usage', and 
plugs those into the template.

While a template based formatter could be implemented as Formatter subclass, it 
seems to be an awkward fit.  In the current structure, the 'parser' method 
determines the overall layout of 'help', while the 'formatter' generates the 
pieces.  The proposed template deals with the layout, not the pieces.

'format_help' could cast into this form, using a default template.  

Possible generalization include:
- methods to format other parts of the help
- handling of multiline indented blocks
- utilizing other templating tools (string.Template, Py3 format, Mako)

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue11695>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to