lee-pai-long
10/16/2019 - 9:42 AM

Argparse custom help formatter

Argparse custom help formatter

class HelpFormatter(
        argparse.RawDescriptionHelpFormatter,
        argparse.ArgumentDefaultsHelpFormatter
):
    """Custom formatter class.

    - RawDescriptionHelpFormatter indicates that description and epilog
      are already correctly formatted and should not be line-wrapped.
    - ArgumentDefaultsHelpFormatter will add information about the
      default value of each of the arguments.
    """
    def __init__(self, *args, **kwargs):
        """Override width default value.

        Use the COLUMNS environment variable to define the width,
        default to 99 which equal our max line length.
        """
        super(HelpFormatter, self).__init__(
            *args, width=int(os.environ.get('COLUMNS', 99)), **kwargs
        )