✨ Add support for optional values of CLI Options#1063
Open
MatteoBouvier wants to merge 10 commits intofastapi:masterfrom
Open
✨ Add support for optional values of CLI Options#1063MatteoBouvier wants to merge 10 commits intofastapi:masterfrom
MatteoBouvier wants to merge 10 commits intofastapi:masterfrom
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
b9fbaf9 to
9e7afc3
Compare
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
9116d7f to
37b74b4
Compare
Contributor
|
📝 Docs preview for commit 37b74b4 at: https://24fe9493.typertiangolo.pages.dev Modified Pages |
Contributor
|
This pull request has a merge conflict that needs to be resolved. |
YuriiMotov
reviewed
Sep 22, 2025
Member
YuriiMotov
left a comment
There was a problem hiding this comment.
@MatteoBouvier, thanks for your interest and efforts!
I think this feature looks unintuitive in such implementation:
BOOLEAN|TEXT [default: False (en)]in docs looks confusing..- behavior is different from how it works in Click: in Click the default value is used if option is not used and
flag_valueis used if option is used without value
Anyway, I would wait for Sebastian to approve the idea of feature request first
svlandeg
reviewed
Nov 25, 2025
Member
svlandeg
left a comment
There was a problem hiding this comment.
As a quick drive-by comment, just noting here that if we do decide to progress with this PR, we'll need to cover the new tutorial files in a specific test in tests/test_tutorial/test_options/test_optional_value/
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.
Click allows to make cli option values optional, using a default value when not provided (https://click.palletsprojects.com/en/stable/options/#optional-value).
This feature is not currently supported by Typer but there is interest from people (including myself) : cf #873, #608.
My use case was creating a
--docoption for displaying a detailed documentation of a command, in english by default but in another language if passing a language name such as--doc fr.To stick to the type annotation philosophy of Typer, this PR defines Options with optional values (with type T) by annotating them as
bool | T:--doc, the doc parameter gets a value ofFalse--doc <value>as usual, the doc parameter gets the value--docwith no value, the doc parameter gets the default value"en"The
helpmessage for the Options is rendered as:note : This PR does not implement type unions for Options, only
bool | Tis parsed