Skip to content

Conversation

savannahostrowski
Copy link
Member

@savannahostrowski savannahostrowski commented Oct 9, 2025

Lib/argparse.py Outdated
groups = self._mutually_exclusive_groups
formatter.add_usage(None, positionals, groups, '')
kwargs['prog'] = formatter.format_help().strip()
kwargs['prog'] = formatter._decolor(formatter.format_help().strip())
Copy link
Contributor

Choose a reason for hiding this comment

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

Just to make sure this removes to the coloring independent if coloring is on or not, yes?

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, _decolor strips ANSI codes when coloring is enabled. When coloring is disabled, it's a no-op since no ANSI codes should exist.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's not going to be enough. I mentioned before, but prog is a public metadata on the parser that contains the name of the application. We should not color it until it's about to be printed later on, otherwise will not fix my issue here https://github.com/tox-dev/sphinx-argparse-cli/blob/main/src/sphinx_argparse_cli/_logic.py#L340-L342. Thanks!

Copy link
Member Author

@savannahostrowski savannahostrowski Oct 9, 2025

Choose a reason for hiding this comment

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

Hmm, I see your point. I think another approach here could be to create a separate formatter explicitly set with color=False, so that prog wouldn't get colored in the first place (rather than having to decolor it).

Copy link
Contributor

@gaborbernat gaborbernat Oct 9, 2025

Choose a reason for hiding this comment

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

Unrelated but I should mention it here that I was surprised that setting color to False for the formatter (see here https://github.com/tox-dev/sphinx-argparse-cli/blob/main/src/sphinx_argparse_cli/_logic.py#L402-L404) doesn't actually disables color printing, had to set NO_COLOR during format_usage to not color that output 🤔 Seems the color argument of the parser is instead used 🤔 not sure if that's a bug or a feature, but suprised me.

By the way I love the colors in the CLI ❤️ (definitely the best feature of 3.14) , just they are not needed when you're writing a sphinx plugin to render it for other outputs 😆

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, this seems like a bug. HelpFormatter accepts color, but it is then seemingly overridden in _get_formatter. This should probably be respected, or we should remove it to avoid confusion.

@python-cla-bot
Copy link

python-cla-bot bot commented Oct 9, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

Copy link
Member

@hugovk hugovk left a comment

Choose a reason for hiding this comment

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

Thanks!

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

LGTM. 👍

There are helpers for temporary monkey-patching in test.support and unittest.mock. You can use them if you prefer.

@savannahostrowski savannahostrowski enabled auto-merge (squash) October 9, 2025 16:27
@savannahostrowski savannahostrowski merged commit 9fc4366 into python:main Oct 9, 2025
45 checks passed
@miss-islington-app
Copy link

Thanks @savannahostrowski for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 9, 2025
…vironment variables (pythonGH-139818)

(cherry picked from commit 9fc4366)

Co-authored-by: Savannah Ostrowski <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Oct 9, 2025

GH-139866 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Oct 9, 2025
savannahostrowski added a commit that referenced this pull request Oct 9, 2025
…nvironment variables (GH-139818) (#139866)

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

Co-authored-by: Savannah Ostrowski <[email protected]>
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)
Copy link
Contributor

Choose a reason for hiding this comment

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

Just double checking, sub-commands still get correctly colorized later on, yeah? Before they were just double colored?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, that's right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants