[3.14] GH-139809: Fix argparse subcommand prog not respecting color environment variables (GH-139818) (#139866)

GH-139809: Fix argparse subcommand prog not respecting color environment variables (GH-139818)
(cherry picked from commit 9fc4366f09)

Co-authored-by: Savannah Ostrowski <savannah@python.org>
This commit is contained in:
Miss Islington (bot) 2025-10-09 19:19:54 +02:00 committed by GitHub
parent 2cc13ff304
commit af1cb1cdb8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 18 additions and 2 deletions

View file

@ -1960,7 +1960,9 @@ def add_subparsers(self, **kwargs):
# prog defaults to the usage message of this parser, skipping
# optional arguments and with no "usage:" prefix
if kwargs.get('prog') is None:
formatter = self._get_formatter()
# Create formatter without color to avoid storing ANSI codes in prog
formatter = self.formatter_class(prog=self.prog)
formatter._set_color(False)
positionals = self._get_positional_actions()
groups = self._mutually_exclusive_groups
formatter.add_usage(None, positionals, groups, '')