ArgumentParser epilog and description formatting in conjunction with ArgumentDefaultsHelpFormatter

argparse, python

Solution

I just tried a multiple inheritance approach, and it works:

class CustomFormatter(argparse.ArgumentDefaultsHelpFormatter, argparse.RawDescriptionHelpFormatter):
    pass

parser = argparse.ArgumentParser(description='test\ntest\ntest.',
                                 epilog='test\ntest\ntest.',
                                 formatter_class=CustomFormatter)

This may break if the internals of these classes change though.

Problem

I'm using argparse to take in command line input and also to produce help text. I want to use `ArgumentDefaultsHelpFormatter` as the `formatter_class`, however this prevents me from also using `RawDescriptionHelpFormatter` which would allow me to add custom formatting to my description or epilog. Is there a sensible method of achieving this aside from writing code to produce text for default values myself? According to the argparse docs, all internals of `ArgumentParser` are considered implementation details, not public API, so sub-classing isn't an attractive option.

Original source

Related problems