Given a user parameter, parse the parameter into a chosen value from a list of choice objects, typically Enum values. The user argument can be a string name that matches the name of a symbol, or the symbol object itself, or any number of alternate choices such as True/False/ None et
(
arg: Any,
choices: Dict[_E, List[Any]],
name: str,
resolve_symbol_names: bool = False,
)
| 1782 | |
| 1783 | |
| 1784 | def parse_user_argument_for_enum( |
| 1785 | arg: Any, |
| 1786 | choices: Dict[_E, List[Any]], |
| 1787 | name: str, |
| 1788 | resolve_symbol_names: bool = False, |
| 1789 | ) -> Optional[_E]: |
| 1790 | """Given a user parameter, parse the parameter into a chosen value |
| 1791 | from a list of choice objects, typically Enum values. |
| 1792 | |
| 1793 | The user argument can be a string name that matches the name of a |
| 1794 | symbol, or the symbol object itself, or any number of alternate choices |
| 1795 | such as True/False/ None etc. |
| 1796 | |
| 1797 | :param arg: the user argument. |
| 1798 | :param choices: dictionary of enum values to lists of possible |
| 1799 | entries for each. |
| 1800 | :param name: name of the argument. Used in an :class:`.ArgumentError` |
| 1801 | that is raised if the parameter doesn't match any available argument. |
| 1802 | |
| 1803 | """ |
| 1804 | for enum_value, choice in choices.items(): |
| 1805 | if arg is enum_value: |
| 1806 | return enum_value |
| 1807 | elif resolve_symbol_names and arg == enum_value.name: |
| 1808 | return enum_value |
| 1809 | elif arg in choice: |
| 1810 | return enum_value |
| 1811 | |
| 1812 | if arg is None: |
| 1813 | return None |
| 1814 | |
| 1815 | raise exc.ArgumentError(f"Invalid value for '{name}': {arg!r}") |
| 1816 | |
| 1817 | |
| 1818 | _creation_order = 1 |