-
-
Notifications
You must be signed in to change notification settings - Fork 64
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
Feature request: better support for argumentless CLI boolean flags #361
Comments
Thanks @sersorrel for this feature request. @kschwab Do you think we can support this in |
@sersorrel thanks for the request. @hramezani, yes, we can support this. My thoughts are to add a class Cfg(BaseSettings, cli_parse_args=True, cli_implicit_flags=True):
req_flag: bool
"""--req_flag {True,False}"""
implicit_flag: bool = False
"""--implicit_flag, --no-implicit_flag"""
# Flags are implicit by default, so must override explicit flags with annotation
explicit_flag: CliExplicitFlag[bool] = False
"""--explicit_flag {True,False}"""
class Cfg(BaseSettings, cli_parse_args=True, cli_implicit_flags=False):
req_flag: bool
"""--req_flag {True,False}"""
# Flags are explicit by default, so must override implicit flags with annotation
implicit_flag: CliImplicitFlag[bool] = False
"""--implicit_flag, --no-implicit_flag"""
explicit_flag: bool = False
"""--explicit_flag {True,False}""" If the above seems reasonable, I'll move forward. The only question I have is what the default should be for |
Thanks @kschwab for the proposal.
Yes, LGTM. the default should be I assigned the issue to you @kschwab. but feel free to unassign yourself if you don't want to continue working on this issue. |
Final implementation is slightly modified from above proposal. CLI implicit flags will also convert required fields for class Cfg(BaseSettings, cli_parse_args=True, cli_implicit_flags=True):
req_implicit_flag: bool
"""--req_implicit_flag, --no-req_implicit_flag (required)"""
# Flags are implicit by default, so must override explicit flags with annotation
req_explicit_flag: CliExplicitFlag[bool]
"""--req_explicit_flag bool (required)"""
opt_implicit_flag: bool = False
"""--opt_implicit_flag, --no-opt_implicit_flag (default: False)"""
# Flags are implicit by default, so must override explicit flags with annotation
opt_explicit_flag: CliExplicitFlag[bool] = False
"""--opt_explicit_flag bool (default: False)"""
class Cfg(BaseSettings, cli_parse_args=True, cli_implicit_flags=False):
# Flags are explicit by default, so must override implicit flags with annotation
req_implicit_flag: CliImplicitFlag[bool]
"""--req_implicit_flag, --no-req_implicit_flag (required)"""
req_explicit_flag: bool
"""--req_explicit_flag bool (required)"""
# Flags are explicit by default, so must override implicit flags with annotation
opt_implicit_flag: CliImplicitFlag[bool] = False
"""--opt_implicit_flag, --no-opt_implicit_flag (default: False)"""
opt_explicit_flag: CliExplicitFlag[bool] = False
"""--opt_explicit_flag bool (default: False)""" For class Cfg(BaseSettings, cli_parse_args=True, cli_implicit_flags=True):
req_flag: bool
"""--req_flag bool (required)"""
implicit_flag: bool = False
"""--implicit_flag (default: False)"""
# Flags are implicit by default, so must override explicit flags with annotation
explicit_flag: CliExplicitFlag[bool] = False
"""--explicit_flag bool (default: False)"""
class Cfg(BaseSettings, cli_parse_args=True, cli_implicit_flags=False):
req_flag: bool
"""--req_flag bool (required)"""
# Flags are explicit by default, so must override implicit flags with annotation
implicit_flag: CliImplicitFlag[bool] = False
"""--implicit_flag (default: False)"""
explicit_flag: bool = False
"""--explicit_flag bool (default: False)""" |
Currently, you can declare a model like this:
and end up with an application you can run in dry-run mode by running
myapp --dry-run=true
. But this is not exactly what is usually expected of CLI applications; usually, boolean flags are argumentless (--dry-run
,--force
, etc.) and their presence implies the associated value is true. Sometimes they have an inverse like--no-dry-run
which implies the opposite.I don't think pydantic-settings currently supports arguments like this. It would be very useful if it did :)
The text was updated successfully, but these errors were encountered: