Skip to content

Render usage through Rich on option errors#14143

Merged
ichard26 merged 4 commits into
pypa:mainfrom
2ykwang:fix-usage-markup-14136
Jul 5, 2026
Merged

Render usage through Rich on option errors#14143
ichard26 merged 4 commits into
pypa:mainfrom
2ykwang:fix-usage-markup-14136

Conversation

@2ykwang

@2ykwang 2ykwang commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Fixes #14136

print_usage() fell through to plain optparse and printed the Rich markup literally. override it to render through PipConsole, like print_help().

see below:

before

python -m pip install --updrade

[optparse.groups]Usage:[/]
  ../python -m pip install \[options] <requirement specifier> \[package-index-options] ...
  ../python -m pip install \[options] -r <requirements file> \[package-index-options] ...
  ../python -m pip install \[options] [-e] <vcs project url> ...
  ../python -m pip install \[options] [-e] <local project path> ...
  ../python -m pip install \[options] <archive url/path> ...

no such option: --updrade

after

PYTHONPATH=src python -m pip install --updrade

Usage:
  ../python -m pip install [options] <requirement specifier> [package-index-options] ...
  ../python -m pip install [options] -r <requirements file> [package-index-options] ...
  ../python -m pip install [options] [-e] <vcs project url> ...
  ../python -m pip install [options] [-e] <local project path> ...
  ../python -m pip install [options] <archive url/path> ...

no such option: --updrade

def print_usage(self, file: Any = None) -> None:
if self.usage:
console = self._create_console(file)
console.print(self.get_usage(), highlight=False)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The trailing newline from get_usage() is intentional, so no rstrip() here.

pip install [options] ...
    <-- blank line
no such option: --updrade

@KRRT7

KRRT7 commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

whilst I don't disagree, I do think this should build on top of #14026

@2ykwang

2ykwang commented Jul 4, 2026

Copy link
Copy Markdown
Contributor Author

thanks @KRRT7 for the suggestion took a look.

here's my concern.

def get_console_or_create() -> Console:
"""Return the stdout console, creating one if logging is not configured yet."""
global _stdout_console
if _stdout_console is None:
_stdout_console = PipConsole(
file=sys.stdout,
no_color=(
"--no-color" in sys.argv
or os.getenv("PIP_NO_COLOR") == "1"
or "NO_COLOR" in os.environ
),
soft_wrap=True,
force_terminal=True,
)
return _stdout_console

the two need different args though (file, theme, soft_wrap), so they pull in different directions.
I think keeping them separate is cleaner. what would we gain by merging?

@ichard26 ichard26 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works and looks great, thank you!

I renamed the _create_console method to _create_console_for_help just so it's clear that this console isn't the main console used by pip. Its only purpose is for highlighting help output.

I agree this is logically separate from the linked PR.

@ichard26
ichard26 enabled auto-merge (squash) July 5, 2026 18:56
@ichard26
ichard26 merged commit 639f233 into pypa:main Jul 5, 2026
41 checks passed
YakBizzarro pushed a commit to YakBizzarro/pip that referenced this pull request Jul 9, 2026
print_usage() fell through to plain optparse and printed the
Rich markup literally. override it to render through PipConsole,
like print_help().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Option errors print usage with Rich markup unrendered

3 participants