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.
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:
ANSI colour escapes using the default HelpFormatter:
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file argparse_color_formatter-3.0.0-py3-none-any.whl.
File metadata
- Download URL: argparse_color_formatter-3.0.0-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
51b17e875b9ebe54403cd55797c670f1459c05ef0c8a6ede3f5e6d737ee689ff
|
|
| MD5 |
40e9be26d3ea3f3317eedfeb05c8565e
|
|
| BLAKE2b-256 |
77cb844daac6f30106891abfbe701effe6d4d392668e086078cf97da96a414e1
|