-
-
Notifications
You must be signed in to change notification settings - Fork 672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
✨ Add support for optional values of CLI Options #1063
base: master
Are you sure you want to change the base?
Conversation
📝 Docs preview for commit e8b83d6 at: https://791524f2.typertiangolo.pages.dev Modified Pages |
📝 Docs preview for commit 8bb363d at: https://d878ac4e.typertiangolo.pages.dev Modified Pages |
📝 Docs preview for commit b9fbaf9 at: https://1f8f8cb4.typertiangolo.pages.dev Modified Pages |
b9fbaf9
to
9e7afc3
Compare
📝 Docs preview for commit 9e7afc3 at: https://85fa57a2.typertiangolo.pages.dev Modified Pages |
📝 Docs preview for commit 9116d7f at: https://c57e1705.typertiangolo.pages.dev Modified Pages |
9116d7f
to
37b74b4
Compare
📝 Docs preview for commit 37b74b4 at: https://24fe9493.typertiangolo.pages.dev Modified Pages |
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
--doc
option 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--doc
with no value, the doc parameter gets the default value"en"
The
help
message for the Options is rendered as:note : This PR does not implement type unions for Options, only
bool | T
is parsed