| """Utilities for creating multiple choice flags.""" |
| |
| # buildifier: disable=unnamed-macro |
| def declare_flag_choices(*, flag, choices): |
| """Declares a `config_setting` for a series of choices for a flag. |
| |
| The name of each config setting uses the name of the `config_setting` is: |
| [flag label name]_[choice] |
| |
| This can be used with select_choice() to map `config_setting`s to values. |
| |
| Args: |
| flag: The flag that guides the declared `config_setting`s. |
| choices: An array giving the choices for the flag. |
| """ |
| flag_name = flag.split(":")[1] |
| for choice in choices: |
| native.config_setting( |
| name = "{}_{}".format(flag_name, choice), |
| flag_values = {flag: choice}, |
| ) |
| |
| def flag_choice(flag, pkg, choice_map): |
| """Creates a `select()` based on choices declared by `declare_choices()`. |
| |
| Args: |
| flag: The flag that guides the select. |
| pkg: The package that `declare_flag_choices()` was called in. |
| choice_map: A mapping of distinct choices to the final intended value. |
| """ |
| return { |
| "{}:{}_{}".format( |
| pkg.split(":")[0], |
| flag.split(":")[1], |
| choice, |
| ): val |
| for choice, val in choice_map.items() |
| } |