馃悰 fix: expand argparse format specifiers in help text - #355
Merged
Conversation
gaborbernat
force-pushed
the
help-format-specifiers
branch
9 times, most recently
from
August 27, 2026 16:35
7fd0a31 to
75b403c
Compare
Help strings such as "count (default: %(default)s)" and descriptions with "%(prog)s" rendered with the literal specifier, followed by the extension's own (default: "3") suffix. A help string starting with "Default: 3" got the same duplicate because the existing-default check was case-sensitive and demanded a space after the word. Reimplement argparse's _expand_help on the public Action attributes, apply the %(prog)s substitution from _format_text to descriptions and epilogs of the root, groups and sub-commands, and detect an existing default mention with a case-insensitive word match.
gaborbernat
force-pushed
the
help-format-specifiers
branch
from
August 27, 2026 16:37
75b403c to
2acd115
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Help strings written for argparse use its format specifiers, so
help="count (default: %(default)s)"anddescription="%(prog)s does things"are common. The directive handed those strings to docutils untouched. The page showed the literal%(default)sand%(prog)s, followed by a second(default: "3")that the extension appends on its own. A help string starting withDefault: 3got the same duplicate, since the check for an existing default mention was case-sensitive and required a space after the word.Help now goes through the same expansion
argparse.HelpFormatter._expand_helpperforms, reimplemented on the publicActionattributes so it does not depend on which formatter class the parser ended up with. It expands%(prog)s,%(default)s,%(choices)sjoined with commas,%(type)sas the callable name, and%%. Descriptions and epilogs of the root, of argument groups, and of sub-commands get the%(prog)ssubstitution argparse applies in_format_text, each with its own parser's prog. 馃ЗThe generated default suffix is skipped whenever the expanded help contains the word
default, in any case and with any punctuation after it. Pages whose help already spelled out the default lose the duplicate.