Last active
August 29, 2015 13:57
-
-
Save mjdorma/9756431 to your computer and use it in GitHub Desktop.
begin.utils.choices
This file contains 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
"""Assert a choice for the value of an argument. | |
Example:: | |
@begin.start | |
@choices(foo=['meh', 'one']) | |
def main(foo): | |
print(foo) | |
""" | |
import begin | |
def choices(**kwargs): | |
"Validate arguments values are in a set" | |
def assert_choice(name, options): | |
def assert_wrapper(value): | |
if value not in options: | |
raise TypeError("'%s' choices are: '%s'" % (name, "', '".join(options))) | |
return value | |
new_kwargs = {} | |
for name, options in kwargs.items(): | |
new_kwargs[name] = assert_choice(name, options) | |
return begin.convert(**new_kwargs) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment