Message221063
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) |
|
Date |
User |
Action |
Args |
2014-06-20 03:09:55 | paul.j3 | set | recipients:
+ paul.j3, bethard, chris.jerdonek, jcon, petri.lehtinen, berker.peksag |
2014-06-20 03:09:55 | paul.j3 | set | messageid: <1403233795.64.0.993608620911.issue11695@psf.upfronthosting.co.za> |
2014-06-20 03:09:55 | paul.j3 | link | issue11695 messages |
2014-06-20 03:09:54 | paul.j3 | create | |
|