Skip to main content

Paragraph-preserving formatter for argparse

Project description

PyPI version Argparse_Formatter Tests Coverage

12/20/25 - FlexiHelpFormatter is making progress in the review process. I'm replacing the formatter code here with that submission, with aliases with the old names, for use with older Python versions.

Argparse Paragraph Formatter

This project is a simple Python module that provides an improved paragraph lovin formatter for argparse. This formatter respects paragraphs and bullet lists, providing consistent word wrapping.

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 (vanishingly) 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 ParagraphHelpFormatter

This module provides that FlexiFormatter class, with some modifications. This formatter preserves line feeds and indentation, and understands bullet lists.

Supported list styles are as follows:

    * item
    - item
    + item
    > item
    A. item
    1. item
    key: item

ParagraphHelpFormatter has been submitted for inclusion in Python3.15 - PR22129.

Demo

The flexidemo.py script shows the output for ParagraphHelpFormatter

*************************
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.
               
                * It looks nicer
                * 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.5.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

argparse_formatter-1.5-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file argparse_formatter-1.5.tar.gz.

File metadata

  • Download URL: argparse_formatter-1.5.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for argparse_formatter-1.5.tar.gz
Algorithm Hash digest
SHA256 877164d0a983689d3f48070f959c2d165dffc6074813b4479088d105b6a3dbff
MD5 94cc7ef5821a0df6c1033215e6fc971c
BLAKE2b-256 1785b57378b2be4e14fdf6a03d6bbf2147e1c9e5d6e812dd1a89e0382fe91153

See more details on using hashes here.

File details

Details for the file argparse_formatter-1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for argparse_formatter-1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 52b48e7834e40d4a61d22b8e13f9eff9bd6e170b1be5717861c546fc6722f635
MD5 37e67fa0d36cd9b9e5dfe93a2f843532
BLAKE2b-256 e79e55d62e096f32596bbf9dfc046c4fb9b3b0edffe915a98eb63f4cc392b4ae

See more details on using hashes here.

Supported by

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