|
89 | 89 | import os as _os |
90 | 90 | import re as _re |
91 | 91 | import sys as _sys |
92 | | - |
93 | | -from gettext import gettext as _, ngettext |
| 92 | +from gettext import gettext as _ |
| 93 | +from gettext import ngettext |
94 | 94 |
|
95 | 95 | SUPPRESS = '==SUPPRESS==' |
96 | 96 |
|
@@ -191,10 +191,10 @@ def __init__( |
191 | 191 |
|
192 | 192 | self._set_color(False) |
193 | 193 |
|
194 | | - def _set_color(self, color): |
| 194 | + def _set_color(self, color, *, file=None): |
195 | 195 | from _colorize import can_colorize, decolor, get_theme |
196 | 196 |
|
197 | | - if color and can_colorize(): |
| 197 | + if color and can_colorize(file=file): |
198 | 198 | self._theme = get_theme(force_color=True).argparse |
199 | 199 | self._decolor = decolor |
200 | 200 | else: |
@@ -1675,7 +1675,7 @@ def _get_optional_kwargs(self, *args, **kwargs): |
1675 | 1675 | option_strings = [] |
1676 | 1676 | for option_string in args: |
1677 | 1677 | # 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: |
1679 | 1679 | raise ValueError( |
1680 | 1680 | f'invalid option string {option_string!r}: ' |
1681 | 1681 | f'must start with a character {self.prefix_chars!r}') |
@@ -2455,7 +2455,7 @@ def _parse_optional(self, arg_string): |
2455 | 2455 | return None |
2456 | 2456 |
|
2457 | 2457 | # 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: |
2459 | 2459 | return None |
2460 | 2460 |
|
2461 | 2461 | # if the option string is present in the parser, return the action |
@@ -2717,14 +2717,16 @@ def _check_value(self, action, value): |
2717 | 2717 | # Help-formatting methods |
2718 | 2718 | # ======================= |
2719 | 2719 |
|
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() |
2722 | 2723 | formatter.add_usage(self.usage, self._actions, |
2723 | 2724 | self._mutually_exclusive_groups) |
2724 | 2725 | return formatter.format_help() |
2725 | 2726 |
|
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() |
2728 | 2730 |
|
2729 | 2731 | # usage |
2730 | 2732 | formatter.add_usage(self.usage, self._actions, |
@@ -2765,12 +2767,16 @@ def _get_validation_formatter(self): |
2765 | 2767 | def print_usage(self, file=None): |
2766 | 2768 | if file is None: |
2767 | 2769 | 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) |
2769 | 2773 |
|
2770 | 2774 | def print_help(self, file=None): |
2771 | 2775 | if file is None: |
2772 | 2776 | 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) |
2774 | 2780 |
|
2775 | 2781 | def _print_message(self, message, file=None): |
2776 | 2782 | if message: |
|
0 commit comments