I don't see why subclassing a HelpFormatter
should be a problem. That isn't messing with the internals of ArgumentParser
. The documentation has examples of custom Action
and Type
classes (or functions). I take the 'there are four such classes'
line to be an invitation to write my own HelpFormatter if needed.
The provided HelpFormatter
subclasses make quite simple changes, changing just one function. So they can be easily copied or altered.
RawDescription
just changes:
def _fill_text(self, text, width, indent):
return ''.join(indent + line for line in text.splitlines(keepends=True))
In theory it could be changed without altering the API, but it's unlikely.
The defaults formatter just changes:
def _get_help_string(self, action):
help = action.help
if '%(default)' not in action.help:
if action.default is not SUPPRESS:
defaulting_nargs = [OPTIONAL, ZERO_OR_MORE]
if action.option_strings or action.nargs in defaulting_nargs:
help += ' (default: %(default)s)'
return help
You could get the same effect by just including %(default)s
in all of your argument help lines. In contrast to the Raw
subclasses, this is just a convenience class. It doesn't give you more control over the formatting.