Skip to main content

argparse-color-formatter

An ANSI-aware formatter_class for argparse. It calculates layout using the visible width of help text, without counting ANSI escape sequences as displayed characters.

That script's help text is so cool...

PYPI

Tests Coverage

Tests Coverage

Tests Coverage

Tests Coverage

Tests Coverage

Tests Coverage

Ruff

Install

$ pip install argparse-color-formatter

What it does

This package does not decide which text to colour. Your application supplies the ANSI-coloured program names, metavars, descriptions, help strings, or epilogues. ColorHelpFormatter then:

  • wraps descriptions, help strings, and usage text at their visible width;
  • aligns option and positional argument labels correctly; and
  • preserves ANSI sequences in the formatted output.

Without an ANSI-aware formatter, invisible escape sequences can make argparse wrap text too early or misalign help columns.

Do I need this on Python 3.14?

Python 3.14 added native colour support to argparse. If you only want the standard argparse colour theme, you do not need this package.

This package is still useful when your application embeds its own ANSI sequences in descriptions, help strings, program names, or metavars. Python 3.14 accounts for its own theme in some layout calculations, but still wraps arbitrary application-coloured descriptions and help strings using their encoded length. ColorHelpFormatter handles those sequences by their visible width and preserves the native Python 3.14 theme alongside them.

Python 3.9 to 3.13

These Python versions do not add colours to argparse help. Use a library such as ansicolors or generate ANSI sequences directly, then use ColorHelpFormatter to keep the resulting output aligned and wrapped correctly.

Usage

Pass ColorHelpFormatter to an argument parser as formatter_class. The example uses ansicolors, which is installed as a dependency of this package.

import argparse

from colors import bold

from argparse_color_formatter import ColorHelpFormatter

output = bold("FILE")
parser = argparse.ArgumentParser(
    description=f"Write the generated report to {output}.",
    formatter_class=ColorHelpFormatter,
)
parser.add_argument(
    "--output",
    metavar=output,
    help=f"save the report as {output}",
)

Development

Setup

pipenv install --dev
pre-commit install
pre-commit install --hook-type commit-msg

Build

pipenv run build

Test

pipenv run test

After and before

ANSI colour escapes using this library's ColorHelpFormatter:

after screenshot

ANSI colour escapes using the default HelpFormatter:

before screenshot

Metadata

Release files for argparse-color-formatter 3.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for argparse-color-formatter 3.0.0
File Interpreter ABI Platform Actions
argparse_color_formatter-3.0.0-py3-none-any.whl Python 3 none any Details for argparse_color_formatter-3.0.0-py3-none-any.whl

Release files / argparse_color_formatter-3.0.0-py3-none-any.whl

Download URL argparse_color_formatter-3.0.0-py3-none-any.whl
Size 8.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
51b17e875b9ebe54403cd55797c670f1459c05ef0c8a6ede3f5e6d737ee689ff
BLAKE2b-256 checksum
How to use checksums
77cb844daac6f30106891abfbe701effe6d4d392668e086078cf97da96a414e1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Security for argparse-color-formatter 3.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page