Skip to main content

Paragraph-preserving formatter for argparse

Project description

Build Status Coverage

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

argparse_formatter-1.4.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

argparse_formatter-1.4-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

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

Hashes for argparse_formatter-1.4.tar.gz
Algorithm Hash digest
SHA256 35027941a1e75a1a4df21e5c40a3395d311777d9bb1cfd9744d6ff7cc28de216
MD5 a7dd3f41dd25b959e77042d0612a53ad
BLAKE2b-256 f146095633f2da154b09938ab568c951ce67e67f2ae22a67f5dce922039c8202

See more details on using hashes here.

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

Hashes for argparse_formatter-1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 c1ce58a68ed83d5204e1515c6f8fb52f5f32fc21bc993faa80f0b17a78090265
MD5 63df5ac545bf411815c2455b3fb9a367
BLAKE2b-256 f4d8768708df2d25cc6483083db56521f35e1b47538dd915339c90fa8fce2e77

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page