Skip to content

馃悰 fix: render option metavars, nargs and choices like usage - #353

Merged
gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:metavar-nargs
Aug 27, 2026
Merged

馃悰 fix: render option metavars, nargs and choices like usage#353
gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:metavar-nargs

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Aug 27, 2026

Copy link
Copy Markdown
Member

The option line built its own argument spec from dest and metavar, and that spec disagreed with the usage line argparse prints two lines above it. 馃悰 A user metavar such as <file> or path/to/dir came out upper-cased. nargs=2 showed one TWO where usage shows TWO TWO; nargs="?", "*" and REMAINDER collapsed to a bare name; choices were missing; a positional with metavar=("SRC", "DST") showed only SRC.

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 or dest, since that name doubles as the reference target.

Rendered output changes for parsers that pass a lower-case metavar. --outdir out_dir used to render as OUT_DIR and now renders as out_dir, matching usage. Option anchors stay the same; a positional with a tuple metavar moves from #tool-SRC to #tool-SRC-DST.

@gaborbernat gaborbernat added the bug Something isn't working label Aug 27, 2026
@gaborbernat
gaborbernat force-pushed the metavar-nargs branch 8 times, most recently from 95ef9d7 to 229e642 Compare August 27, 2026 16:33
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
gaborbernat merged commit fc42259 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