Repository: pypa/pip
File: src/pip/_internal/cli/parser.py (Python)

Code before:
import textwrap
from collections.abc import Generator
from contextlib import suppress
from typing import Any, NoReturn

from pip._vendor.rich.console import RenderableType
from pip._vendor.rich.markup import escape
from pip._vendor.rich.style import StyleType
from pip._vendor.rich.text import Text
from pip._vendor.rich.theme import Theme

from pip._internal.cli.status_codes import UNKNOWN_ERROR
from pip._internal.configuration import Configuration, ConfigurationError
from pip._internal.utils.logging import PipConsole
from pip._internal.utils.misc import redact_auth_from_url, strtobool

logger = logging.getLogger(__name__)


class PrettyHelpFormatter(optparse.IndentedHelpFormatter):
    """A prettier/less verbose help formatter for optparse."""

    styles: dict[str, StyleType] = {
        "optparse.shortargs": "green",
        "optparse.longargs": "cyan",
        "optparse.groups": "blue",
        "optparse.help": "default",
        "optparse.metavar": "yellow",
        "optparse.syntax": "bold",
        "optparse.text": "default",
    }
    highlights: list[str] = [
        r"(?:^|\s)(?P<shortargs>-{1}[\w]+[\w-]*)",  # highlight -letter as short args
        r"(?:^|\s)(?P<longargs>-{2}[\w]+[\w-]*)",  # highlight --words as long args
        r"`(?P<syntax>[^`]*)`",  # highlight `text in backquotes` as syntax
    ]

    def __init__(self, *args: Any, **kwargs: Any) -> None:
        # help position must be aligned with __init__.parseopts.description
        kwargs["max_help_position"] = 30
        kwargs["indent_increment"] = 1
        kwargs["width"] = shutil.get_terminal_size()[0] - 2
        super().__init__(*args, **kwargs)
        # This is unfortunate but necessary since arguments may have not been
        # parsed yet at this point, so detect --no-color manually.
        no_color = (
            "--no-color" in sys.argv
            or bool(strtobool(os.environ.get("PIP_NO_COLOR", "no") or "no"))
            or "NO_COLOR" in os.environ
        )
        self.console = PipConsole(theme=Theme(self.styles), no_color=no_color)

Review comment (suggestion):
CPython actually uses bold blue for the headers (https://github.com/python/cpython/blob/274a26cca8e3d2f4de0283d4acbc80be391a5f6a/Lib/_colorize.py#L159):

```suggestion
        "optparse.groups": "bold blue",
```

And also bold for the short (green) and long (cyan) options and (yellow) labels, but it's most needed for the headers.

<img width="573" height="415" alt="image" src="https://github.com/user-attachments/assets/46c5449a-7010-4ec0-886c-7c44fa81a87c" />


Code after:
import sys
import textwrap
from collections.abc import Generator
from contextlib import suppress
from typing import Any, NoReturn

from pip._vendor.rich.markup import escape
from pip._vendor.rich.theme import Theme

from pip._internal.cli.status_codes import UNKNOWN_ERROR
from pip._internal.configuration import Configuration, ConfigurationError
from pip._internal.utils.logging import PipConsole
from pip._internal.utils.misc import redact_auth_from_url, strtobool

logger = logging.getLogger(__name__)


class PrettyHelpFormatter(optparse.IndentedHelpFormatter):
    """A prettier/less verbose help formatter for optparse."""

    styles = {
        "optparse.shortargs": "green",
        "optparse.longargs": "cyan",
        "optparse.groups": "bold blue",
        "optparse.metavar": "yellow",
    }
    highlights = {
        r"\s(-{1}[\w]+[\w-]*)": "shortargs",  # highlight -letter as short args
        r"\s(-{2}[\w]+[\w-]*)": "longargs",  # highlight --words as long args
    }

    def __init__(self, *args: Any, **kwargs: Any) -> None:
        # help position must be aligned with __init__.parseopts.description
        kwargs["max_help_position"] = 30
        kwargs["indent_increment"] = 1
        kwargs["width"] = shutil.get_terminal_size()[0] - 2
        super().__init__(*args, **kwargs)

    def format_option_strings(self, option: optparse.Option) -> str:
        """Return a comma-separated list of option strings and metavars."""
        opts = []

        if option._short_opts:
            opts.append(f"[optparse.shortargs]{option._short_opts[0]}[/]")
        if option._long_opts:
            opts.append(f"[optparse.longargs]{option._long_opts[0]}[/]")
        if len(opts) > 1:
            opts.insert(1, ", ")

        if option.takes_value():
            assert option.dest is not None