Paragraph-preserving formatter for argparse
Project description
Argparse Paragraph Formatter
This project is a simple Python module that provides argparse formatters that support more flexibility in the display of description, help, and epilog text.
Background
By default, argparse will collapse and transform all sequences of whitespace into a single space, and then wrap the text to an appropriate line length. This causes all text to be transformed into a single paragraph.
There are optional RawDescriptionHelpFormatter and RawTextHelpFormatter classes that can be used to preserve paragraphs, but they do not perform any word wrapping. What is needed are formatters that support more flexibility while still providing word wrapping.
The ParagraphFormatter
This module provides the ParagraphFormatter class. If this class is passed to the argparse.ArgumentParser creation call as the formatter_class argument, then help and epilog paragraphs, separated by single blank lines, will be preserved. Word wrapping will be performed within the paragraphs.
Note that ParagraphFormatter is implemented by overriding two private methods in the default argparse formatter, and that the internals of that class are not considered part of the argparse API. There is therefore a small risk that the code may break with a future standard library release. The module has been tested across all supported Python 3 versions.
The FlexiFormatter
As it turns out, ParagraphFormatter is a more primitive version of a rich formatter that has been long proposed for inclusion in Python.
This module provides that FlexiFormatter class, with some modifications. This formatter preserves line feeds and indentation, and understands bullet lists.
FlexiFormatter has been submitted for inclusion in Python3.9 - PR22129. It would be good for this formatter to be merged - it eliminates the API-migration risk.
Demo
The script demo.py demonstrates the effect of using the ParagraphFormatter:
# ./demo.py
*************************
Using the Default formatter
*************************
usage: demo.py [-h] [--arg ARG]
optional arguments:
-h, --help show this help message and exit
--arg ARG This same feature would be useful for arguments that would
benefit from more explanation. Wouldn't it?
This is a multi-paragraph epilog. It is presenting data that would benefit by
being visually broken up into pieces. It sure would be nice if it was
represented that way.
*************************
Using the Paragraph formatter
*************************
usage: demo.py [-h] [--arg ARG]
optional arguments:
-h, --help show this help message and exit
--arg ARG This same feature would be useful for arguments that would
benefit from more explanation.
Wouldn't it?
This is a multi-paragraph epilog. It is presenting data that would benefit by
being visually broken up into pieces.
It sure would be nice if it was represented that way.
The flexidemo.py script shows the output for FlexiFormatter
*************************
Using the Default formatter
*************************
usage: flexidemo.py [-h] [--arg ARG]
optional arguments:
-h, --help show this help message and exit
--arg ARG This same feature would be useful for arguments that would
benefit from more explanation. 1. It looks nicer 2. It is easier
to read, even if some of the bullets get to be a little long.
This is a multi-paragraph epilog. It is presenting data that would benefit by
being visually broken up into pieces. It sure would be nice if it was
represented that way. 1. This is a pretty long line, in a bullet list -
getting longer and longer and longer 2. this isn't
*************************
Using the Flexi formatter
*************************
usage: flexidemo.py [-h] [--arg ARG]
optional arguments:
-h, --help show this help message and exit
--arg ARG This same feature would be useful for arguments that would
benefit from more explanation.
1. It looks nicer
2. It is easier to read, even if some of the bullets get to be
a little long.
This is a multi-paragraph epilog. It is presenting data that would benefit by
being visually broken up into pieces.
It sure would be nice if it was represented that way.
1. This is a pretty long line, in a bullet list - getting longer and longer
and longer
2. this isn't
Install
Install from PyPi with:
pip install argparse-formatter
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file argparse_formatter-1.4.tar.gz
.
File metadata
- Download URL: argparse_formatter-1.4.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.4.2 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.9.1+
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 35027941a1e75a1a4df21e5c40a3395d311777d9bb1cfd9744d6ff7cc28de216 |
|
MD5 | a7dd3f41dd25b959e77042d0612a53ad |
|
BLAKE2b-256 | f146095633f2da154b09938ab568c951ce67e67f2ae22a67f5dce922039c8202 |
File details
Details for the file argparse_formatter-1.4-py3-none-any.whl
.
File metadata
- Download URL: argparse_formatter-1.4-py3-none-any.whl
- Upload date:
- Size: 11.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.4.2 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.9.1+
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c1ce58a68ed83d5204e1515c6f8fb52f5f32fc21bc993faa80f0b17a78090265 |
|
MD5 | 63df5ac545bf411815c2455b3fb9a367 |
|
BLAKE2b-256 | f4d8768708df2d25cc6483083db56521f35e1b47538dd915339c90fa8fce2e77 |