Python › Command-Line Interfaces (argparse)
How do you customize argparse's automatically-generated usage and help text formatting, like preserving specific line breaks in the description?
AdvancedAnswer
Pass a custom 'formatter_class' (like argparse.RawDescriptionHelpFormatter or RawTextHelpFormatter) to ArgumentParser() to disable argparse's default text-wrapping/reformatting behavior, letting you control exact formatting of multi-line descriptions or examples in the help output.
parser = argparse.ArgumentParser(
description="""Deploy an application.
Examples:
mytool deploy --env prod
mytool deploy --env staging --dry-run""",
formatter_class=argparse.RawDescriptionHelpFormatter
)Real-world example Providing multi-line usage EXAMPLES in a CLI tool's --help output that preserve their intended formatting exactly.