Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: distinguish stdout or stderr when colorizing output in argparse
Signed-off-by: Frost Ming <me@frostming.com>
  • Loading branch information
frostming committed Dec 8, 2025
commit 1c6c8989ac0b5ffd2009005056883d321cf195e2
30 changes: 18 additions & 12 deletions Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@
import os as _os
import re as _re
import sys as _sys

from gettext import gettext as _, ngettext
from gettext import gettext as _
from gettext import ngettext

SUPPRESS = '==SUPPRESS=='

Expand Down Expand Up @@ -191,10 +191,10 @@ def __init__(

self._set_color(False)

def _set_color(self, color):
def _set_color(self, color, *, file=None):
from _colorize import can_colorize, decolor, get_theme

if color and can_colorize():
if color and can_colorize(file=file):
self._theme = get_theme(force_color=True).argparse
self._decolor = decolor
else:
Expand Down Expand Up @@ -1675,7 +1675,7 @@ def _get_optional_kwargs(self, *args, **kwargs):
option_strings = []
for option_string in args:
# error on strings that don't start with an appropriate prefix
if not option_string[0] in self.prefix_chars:
if option_string[0] not in self.prefix_chars:
raise ValueError(
f'invalid option string {option_string!r}: '
f'must start with a character {self.prefix_chars!r}')
Expand Down Expand Up @@ -2455,7 +2455,7 @@ def _parse_optional(self, arg_string):
return None

# if it doesn't start with a prefix, it was meant to be positional
if not arg_string[0] in self.prefix_chars:
if arg_string[0] not in self.prefix_chars:
return None

# if the option string is present in the parser, return the action
Expand Down Expand Up @@ -2717,14 +2717,16 @@ def _check_value(self, action, value):
# Help-formatting methods
# =======================

def format_usage(self):
formatter = self._get_formatter()
def format_usage(self, formatter=None):
if formatter is None:
formatter = self._get_formatter()
formatter.add_usage(self.usage, self._actions,
self._mutually_exclusive_groups)
return formatter.format_help()

def format_help(self):
formatter = self._get_formatter()
def format_help(self, formatter=None):
if formatter is None:
formatter = self._get_formatter()

# usage
formatter.add_usage(self.usage, self._actions,
Expand Down Expand Up @@ -2765,12 +2767,16 @@ def _get_validation_formatter(self):
def print_usage(self, file=None):
if file is None:
file = _sys.stdout
self._print_message(self.format_usage(), file)
formatter = self._get_formatter()
formatter._set_color(self.color, file=file)
self._print_message(self.format_usage(formatter=formatter), file)

def print_help(self, file=None):
if file is None:
file = _sys.stdout
self._print_message(self.format_help(), file)
formatter = self._get_formatter()
formatter._set_color(self.color, file=file)
self._print_message(self.format_help(formatter=formatter), file)

def _print_message(self, message, file=None):
if message:
Expand Down