Skip to content

Conversation

@svlandeg
Copy link
Member

@svlandeg svlandeg commented Nov 17, 2025

As stated in the docs, currently when an argument has a default and is thus not required (e.g. tutorial003.py), the help string shows the argument name between brackets when printing the help string:

python tutorial003.py --help
Usage: main.py [OPTIONS] [NAME]
...
Arguments:
[NAME] Who to greet [default: World]

However, when we use metavar to set a synonym for this argument, then suddenly it doesn't appear between brackets anymore, e.g. tutorial006.py

python tutorial006.py --help
Usage: main.py [OPTIONS] ✨username✨

Arguments:
✨username✨ [default: World]

This PR makes that consistent:

python tutorial006.py --help
Usage: main.py [OPTIONS] [✨username✨]

Arguments:
[✨username✨] [default: World]

Follow-up work

I'd love feedback on this, because I'm looking into a few more cases where metavar processing seems to be inconsistent, especially when combined with Rich formatting.

I also have questions around adding the type_var stuff at L394-403 in core.py, and whether that's actually used/documented anywhere in Typer. Those are probably follow-up discussions though, and this PR tries to single out a first fix before moving on.

[Update]: now also created #1410, which I would review after this one.

@svlandeg svlandeg added the bug Something isn't working label Nov 17, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Nov 17, 2025

assert "[OPTIONS] ✨username✨" in result.output
assert "Usage: main [OPTIONS] [✨username✨]" in result.output
assert "Arguments" in result.output
assert "✨username✨" in result.output
Copy link
Member Author

Choose a reason for hiding this comment

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

Note that this second assert was always redundant, as L18 would already fail if this wasn't the case.

Copy link
Member

@tiangolo tiangolo left a comment

Choose a reason for hiding this comment

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

Thank you! 🚀

This will be available in Typer 0.20.1, released in the next few hours. 🎉

@tiangolo tiangolo merged commit 2cfdb99 into fastapi:master Dec 19, 2025
25 checks passed
@svlandeg svlandeg deleted the fix/metavar_usage branch December 19, 2025 16:55
svlandeg added a commit to svlandeg/typer that referenced this pull request Jan 7, 2026
svlandeg added a commit to svlandeg/typer that referenced this pull request Jan 7, 2026
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.

2 participants