import argparse import textwrap def mywrap(text,**kwargs): # apply textwrap to each line individually text = text.splitlines() text = [textwrap.fill(line,**kwargs) for line in text] return '\n'.join(text) description = """\ This description help text will have this first long line wrapped to \ fit the target window size so that your text remains flexible. 1. But lines such as 2. this that that are indented beyond the first line's indent, 3. are reproduced verbatim, with no wrapping. or other formatting applied. You must use backslashes at the end of lines to indicate that you \ want the text to wrap instead of preserving the newline. It simpler if we avoid the use of leading spaces. """ epilog = """Epilog can also be wrapped in a custom way. a) point one b) point two rest of the epilog. """ arg_help = '''\ This argument's help text will have this first long line wrapped to \ fit the target window size so that your text remains flexible. 1. But lines such as 2. this that that are indented beyond the first line's indent, 3. are reproduced verbatim, with no wrapping. or other formatting applied. You must use backslashes at the end of lines to indicate that you \ want the text to wrap instead of preserving the newline. ''' p = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, description=mywrap(description), epilog=mywrap(epilog, width=40)) p.add_argument('--foo', help=mywrap(arg_help, width=50)) p.add_argument('bar', help='positional argument help') print(p.format_help()) """ 1922:~/mypy/argdev/arggit$ ../python3 wrap_sample.py usage: wrap_sample.py [-h] [--foo FOO] bar This description help text will have this first long line wrapped to fit the target window size so that your text remains flexible. 1. But lines such as 2. this that that are indented beyond the first line's indent, 3. are reproduced verbatim, with no wrapping. or other formatting applied. You must use backslashes at the end of lines to indicate that you want the text to wrap instead of preserving the newline. It simpler if we avoid the use of leading spaces. positional arguments: bar positional argument help optional arguments: -h, --help show this help message and exit --foo FOO This argument's help text will have this first long line wrapped to fit the target window size so that your text remains flexible. 1. But lines such as 2. this that that are indented beyond the first line's indent, 3. are reproduced verbatim, with no wrapping. or other formatting applied. You must use backslashes at the end of lines to indicate that you want the text to wrap instead of preserving the newline. Epilog can also be wrapped in a custom way. a) point one b) point two rest of the epilog. """