Skip to content

馃悰 fix: expand argparse format specifiers in help text - #355

Merged
gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:help-format-specifiers
Aug 27, 2026
Merged

馃悰 fix: expand argparse format specifiers in help text#355
gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:help-format-specifiers

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Aug 27, 2026

Copy link
Copy Markdown
Member

Help strings written for argparse use its format specifiers, so help="count (default: %(default)s)" and description="%(prog)s does things" are common. The directive handed those strings to docutils untouched. The page showed the literal %(default)s and %(prog)s, followed by a second (default: "3") that the extension appends on its own. A help string starting with Default: 3 got 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_help performs, reimplemented on the public Action attributes so it does not depend on which formatter class the parser ended up with. It expands %(prog)s, %(default)s, %(choices)s joined with commas, %(type)s as the callable name, and %%. Descriptions and epilogs of the root, of argument groups, and of sub-commands get the %(prog)s substitution 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.

@gaborbernat gaborbernat added the bug Something isn't working label Aug 27, 2026
@gaborbernat
gaborbernat force-pushed the help-format-specifiers branch 9 times, most recently from 7fd0a31 to 75b403c Compare August 27, 2026 16:35
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
gaborbernat force-pushed the help-format-specifiers branch from 75b403c to 2acd115 Compare August 27, 2026 16:37
@gaborbernat
gaborbernat merged commit 84f9966 into tox-dev:main Aug 27, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant