Help text produced by argparse often contains code fragments, such as file names, command lines and accepted values. Written in a sentence they blend into the surrounding words, so it is not obvious which ones should be typed exactly as shown. Python 3.14 introduced color for the help output, but it applied only to the parts argparse generates itself, such as the usage line, option names and metavariables. Text supplied by the author stayed plain.
Since Python 3.15, fragments wrapped in backticks are highlighted:
import argparse
parser = argparse.ArgumentParser(
description='Reads data from `input.csv` and writes ``output.json``.',
epilog='See `--help` for details.',
)
parser.add_argument('--format', help='Output format, such as `json`')
parser.parse_args()
Run the command:
python main.py --help
The markup works in three places: the parser description, the epilog, and the help text of an individual argument. Both single and double backticks are accepted, so a style borrowed from reStructuredText or Markdown can be used without adjustment.
Highlighting depends on color being active. When colors are enabled, the text inside backticks is displayed in a distinct color. When they are disabled, the backticks are left in place, which keeps the text readable as plain output.
Color itself is controlled by the color parameter of the parser, which was added in Python 3.14 and defaults to True. With color=True the result still depends on the environment and on whether the terminal supports color.
Leave a Comment
Cancel reply