Open 8955c213-fd54-471c-9758-9cc5f49074db opened 13 years ago
I'm going to try to merge several closely related issues here. Basically, people would like better control over the usage message formatting so that you could:
One proposal from anatoly techtonik would be to allow a format string so that you could write something like
"""My Program, version 3.5 Usage: %(usage)s
Some description of my program
%(argument_groups)%
My epliog text """
This should be implemented as a HelpFormatter class, but we might have to expose a little more of the HelpFormatter API (which is currently documented as a no-public API) to make this possible.
Patches welcome. ;-)
+1 to the feature.
A closely-related use case is customizing the message displayed by error(), which is normally the usage string followed by the error message. I wanted to append instructions on how to invoke --help, and implemented it this way for CPython's regrtest:
http://hg.python.org/cpython/file/6ee721029fd5/Lib/test/regrtest.py#l205
Also take a look at how regrtest formats its usage string as another use case to satisfy:
http://hg.python.org/cpython/file/6ee721029fd5/Lib/test/regrtest.py#l9
It seems like many argparse customizations take the form of "override this method." Would it make sense for the API to be for customizers to override string-returning methods like make_usage() and make_error() (and that accept a dictionary)? That may give a bit more control than a format string.
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:
This patch has a 'custom_help' which, with a default template, is compatible with 'format_help' (i.e. it passes test_argparse). It also handles the sample template in this issue.
Due to long line wrapping issues, the 'Usage: ' string the test template has to be entered separately as a usage 'prefix'. Indenting of long wrapped values (like usage) is correct only if the '%(...)s' string is at the start of a line.
I see this as a test-of-concept patch.
That original template can also be implemented with a customized 'format_help':
def custom_help(self):
formatter = self._get_formatter()
formatter.add_text('My Program, version 3.5')
formatter.add_usage(self.usage, self._actions,
self._mutually_exclusive_groups,
prefix='Usage: ')
formatter.add_text('Some description of my program')
for action_group in self._action_groups:
with formatter.add_section(action_group.title):
formatter.add_text(action_group.description)
formatter.add_arguments(action_group._group_actions)
formatter.add_text('My epilog text')
return formatter.format_help()
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields: ```python assignee = None closed_at = None created_at =
labels = ['type-feature', 'library']
title = 'Improve argparse usage/help customization'
updated_at =
user = 'https://bugs.python.org/bethard'
```
bugs.python.org fields:
```python
activity =
actor = 'paul.j3'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation =
creator = 'bethard'
dependencies = []
files = ['35713']
hgrepos = []
issue_num = 11695
keywords = ['patch']
message_count = 5.0
messages = ['132322', '179522', '221063', '221123', '221187']
nosy_count = 6.0
nosy_names = ['bethard', 'chris.jerdonek', 'jcon', 'petri.lehtinen', 'berker.peksag', 'paul.j3']
pr_nums = []
priority = 'normal'
resolution = None
stage = 'needs patch'
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue11695'
versions = ['Python 2.7', 'Python 3.5']
```