馃悰 fix: render option metavars, nargs and choices like usage - #353
Merged
Conversation
gaborbernat
force-pushed
the
metavar-nargs
branch
8 times, most recently
from
August 27, 2026 16:33
95ef9d7 to
229e642
Compare
The option line assembled its own metavar text from dest and metavar: a user metavar was upper-cased, nargs and choices were ignored, and a positional with a tuple metavar showed only the first element. Usage on the same page showed the correct spec, so the two disagreed. Ask argparse's HelpFormatter._format_args for the text instead; it is the same call that builds the usage line. Positionals join a tuple metavar with spaces, as the name also serves as the reference anchor.
gaborbernat
force-pushed
the
metavar-nargs
branch
from
August 27, 2026 16:34
229e642 to
a31f3b5
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.
The option line built its own argument spec from
destandmetavar, and that spec disagreed with the usage line argparse prints two lines above it. 馃悰 A user metavar such as<file>orpath/to/dircame out upper-cased.nargs=2showed oneTWOwhere usage showsTWO TWO;nargs="?","*"andREMAINDERcollapsed to a bare name;choiceswere missing; a positional withmetavar=("SRC", "DST")showed onlySRC.Options now hand the action to argparse's own
HelpFormatter._format_args, the same call that produces the usage line. The literal after the option name therefore reads[OPT],[MANY ...],TWO TWO,...,{json,xml}or the metavar as the author typed it. Positionals join a tuple metavar with spaces for both the displayed name and the anchor; otherwise they keep the metavar ordest, since that name doubles as the reference target.Rendered output changes for parsers that pass a lower-case metavar.
--outdir out_dirused to render asOUT_DIRand now renders asout_dir, matching usage. Option anchors stay the same; a positional with a tuple metavar moves from#tool-SRCto#tool-SRC-DST.