Skip to content
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

consider a choices parser #253

Closed
willkg opened this issue Jul 24, 2024 · 1 comment · Fixed by #265
Closed

consider a choices parser #253

willkg opened this issue Jul 24, 2024 · 1 comment · Fixed by #265

Comments

@willkg
Copy link
Owner

willkg commented Jul 24, 2024

I've had several situations where I want a parser that requires the value to be one of a list of possible values. I'd like to do something like:

from everett.manager import ConfigManager

config = ConfigManager.basic_config()

CLOUD_PROVIDER = config(
    "CLOUD_PROVIDER",
    default="AWS",
    parser=OneOf(["AWS", "GCP"]),
)

and then have that work for "AWS" and "GCP" and raise aValueError specifying one of the valid values for everything else.

@willkg
Copy link
Owner Author

willkg commented Oct 29, 2024

I rethought the API. I want it to support different sub-parsers. Given that, I thought it should mirror ListOf. Examples:

>>> from everett.manager import ChoiceOf, parse_data_size
>>> ChoiceOf(str, choices=["aws", "gcp"])("aws")
"aws"
 >>> ChoiceOf(int, choices=["1", "2", "3"])("1")
1
 >>> ChoiceOf(parse_data_size, choices=["1kb", "1mb", "1gb"])("1mb")
1000000

That gives a lot of flexibility. It forces values to be consistent in the places they show up that the user sees them (documentation, error messages, etc). It also allows the parsed value to be convenient for programming.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant