The approved way of customizing the help formatting is to subclass HelpFormatter. A user can do this without waiting for a future Python release.
This formatter implements the {}.format in 2 places.
class NewHelpFormatter(argparse.HelpFormatter):
# _format_usage - format usage, but only uses dict(prog=self._prog)
def _format_text(self, text):
# for description, epilog, version
if '{prog}' in text:
text = text.format(prog=self._prog) # change from %
text_width = self._width - self._current_indent
indent = ' ' * self._current_indent
return self._fill_text(text, text_width, indent) + '\n\n'
def _expand_help(self, action):
params = dict(vars(action), prog=self._prog)
for name in list(params):
if params[name] is argparse.SUPPRESS:
del params[name]
for name in list(params):
if hasattr(params[name], '__name__'):
params[name] = params[name].__name__
if params.get('choices') is not None:
choices_str = ', '.join([str(c) for c in params['choices']])
params['choices'] = choices_str
return self._get_help_string(action).format(**params) # change from %
For example:
parser = argparse.ArgumentParser(prog='NewFormatter',
formatter_class=NewHelpFormatter,
description='{prog} description')
parser.add_argument('foo',nargs=3, default=[1,2,3],
help='nargs:{nargs} prog:{prog!r} defaults:{default} last:{default[2]}')
parser.add_argument('--bar',choices=['yes','no'],
help='choices: {choices!r}')
parser.print_help()
produces:
usage: NewFormatter [-h] [--bar {yes,no}] foo foo foo
NewFormatter description
positional arguments:
foo nargs:3 prog:'NewFormatter' defaults:[1, 2, 3] last:3
optional arguments:
-h, --help show this help message and exit
--bar {yes,no} choices: 'yes, no'