Skip to content

Commit 1c6c898

Browse files
committed
fix: distinguish stdout or stderr when colorizing output in argparse
Signed-off-by: Frost Ming <me@frostming.com>
1 parent ef51a7c commit 1c6c898

1 file changed

Lines changed: 18 additions & 12 deletions

File tree

Lib/argparse.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@
8989
import os as _os
9090
import re as _re
9191
import sys as _sys
92-
93-
from gettext import gettext as _, ngettext
92+
from gettext import gettext as _
93+
from gettext import ngettext
9494

9595
SUPPRESS = '==SUPPRESS=='
9696

@@ -191,10 +191,10 @@ def __init__(
191191

192192
self._set_color(False)
193193

194-
def _set_color(self, color):
194+
def _set_color(self, color, *, file=None):
195195
from _colorize import can_colorize, decolor, get_theme
196196

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

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

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

2720-
def format_usage(self):
2721-
formatter = self._get_formatter()
2720+
def format_usage(self, formatter=None):
2721+
if formatter is None:
2722+
formatter = self._get_formatter()
27222723
formatter.add_usage(self.usage, self._actions,
27232724
self._mutually_exclusive_groups)
27242725
return formatter.format_help()
27252726

2726-
def format_help(self):
2727-
formatter = self._get_formatter()
2727+
def format_help(self, formatter=None):
2728+
if formatter is None:
2729+
formatter = self._get_formatter()
27282730

27292731
# usage
27302732
formatter.add_usage(self.usage, self._actions,
@@ -2765,12 +2767,16 @@ def _get_validation_formatter(self):
27652767
def print_usage(self, file=None):
27662768
if file is None:
27672769
file = _sys.stdout
2768-
self._print_message(self.format_usage(), file)
2770+
formatter = self._get_formatter()
2771+
formatter._set_color(self.color, file=file)
2772+
self._print_message(self.format_usage(formatter=formatter), file)
27692773

27702774
def print_help(self, file=None):
27712775
if file is None:
27722776
file = _sys.stdout
2773-
self._print_message(self.format_help(), file)
2777+
formatter = self._get_formatter()
2778+
formatter._set_color(self.color, file=file)
2779+
self._print_message(self.format_help(formatter=formatter), file)
27742780

27752781
def _print_message(self, message, file=None):
27762782
if message:

0 commit comments

Comments
 (0)