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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

argparse_color_formatter-3.0.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file argparse_color_formatter-3.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for argparse_color_formatter-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 51b17e875b9ebe54403cd55797c670f1459c05ef0c8a6ede3f5e6d737ee689ff
MD5 40e9be26d3ea3f3317eedfeb05c8565e
BLAKE2b-256 77cb844daac6f30106891abfbe701effe6d4d392668e086078cf97da96a414e1

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

3.0.0 This release

1 file

2.0.0.post1

1 file

2.0.0

1 file

1.2.2.post2

2 files

1.2.1

1 file

1.2.0

1 file

1.1.1

1 file

1.1.0

1 file

1.0.0

1 file

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