def DEFINE_multi_enum_class(name, default, enum_class, help=None, flag_values=_flagvalues.FLAGS, module_name=None, **args): """Registers a flag whose value can be a list of enum members. Use the flag on the command line multiple times to place multiple enum values into the list. Args: name: str, the flag name. default: Union[Iterable[Enum], Iterable[Text], Enum, Text, None], the default value of the flag; see `DEFINE_multi`; only differences are documented here. If the value is a single Enum, it is treated as a single-item list of that Enum value. If it is an iterable, text values within the iterable will be converted to the equivalent Enum objects. enum_class: class, the Enum class with all the possible values for the flag. help: str, the help message. flag_values: FlagValues, the FlagValues instance with which the flag will be registered. This should almost never need to be overridden. module_name: A string, the name of the Python module declaring this flag. If not provided, it will be computed using the stack trace of this call. **args: Dictionary with extra keyword args that are passed to the Flag __init__. """ DEFINE_flag(_flag.MultiEnumClassFlag(name, default, help, enum_class), flag_values, module_name, **args)
def test_rejects_wrong_case_when_case_sensitive(self): with self.assertRaisesRegex(_exceptions.IllegalFlagValueError, '<APPLE|ORANGE>'): _flag.MultiEnumClassFlag('fruit', ['APPLE', 'Orange'], 'A sample enum flag.', Fruit, case_sensitive=True)
def test_help_text(self, helptext_input, helptext_output, case_sensitive): f = _flag.MultiEnumClassFlag('fruit', None, helptext_input, Fruit, case_sensitive=case_sensitive) self.assertEqual(helptext_output, f.help)
def DEFINE_multi_enum_class( # pylint: disable=invalid-name,redefined-builtin name, default, enum_class, help, flag_values=_flagvalues.FLAGS, module_name=None, case_sensitive=False, required=False, **args): """Registers a flag whose value can be a list of enum members. Use the flag on the command line multiple times to place multiple enum values into the list. Args: name: str, the flag name. default: Union[Iterable[Enum], Iterable[Text], Enum, Text, None], the default value of the flag; see `DEFINE_multi`; only differences are documented here. If the value is a single Enum, it is treated as a single-item list of that Enum value. If it is an iterable, text values within the iterable will be converted to the equivalent Enum objects. enum_class: class, the Enum class with all the possible values for the flag. help: str, the help message. flag_values: FlagValues, the FlagValues instance with which the flag will be registered. This should almost never need to be overridden. module_name: A string, the name of the Python module declaring this flag. If not provided, it will be computed using the stack trace of this call. case_sensitive: bool, whether to map strings to members of the enum_class without considering case. required: bool, is this a required flag. This must be used as a keyword argument. **args: Dictionary with extra keyword args that are passed to the Flag __init__. Returns: a handle to defined flag. """ return DEFINE_flag(_flag.MultiEnumClassFlag(name, default, help, enum_class, case_sensitive=case_sensitive), flag_values, module_name, required=required, **args)
def test_default_value_does_not_exist(self): with self.assertRaises(_exceptions.IllegalFlagValueError): _flag.MultiEnumClassFlag('fruit', 'BANANA', 'help', Fruit)
def test_accepts_list_of_string_default(self): f = _flag.MultiEnumClassFlag('fruit', ['ORANGE', 'APPLE'], 'A sample enum flag.', Fruit) self.assertListEqual([Fruit.ORANGE, Fruit.APPLE], f.value)
def test_accepts_literal_default(self): f = _flag.MultiEnumClassFlag('fruit', Fruit.APPLE, 'A sample enum flag.', Fruit) self.assertListEqual([Fruit.APPLE], f.value)
def test_requires_non_empty_enum_class(self): with self.assertRaises(ValueError): _flag.MultiEnumClassFlag('empty', None, 'help', EmptyEnum)
def test_requires_enum(self): with self.assertRaises(TypeError): _flag.MultiEnumClassFlag('fruit', None, 'help', ['apple', 'orange'])
def test_accepts_case_insensitive(self): f = _flag.MultiEnumClassFlag('fruit', ['apple', 'APPLE'], 'A sample enum flag.', Fruit) self.assertListEqual([Fruit.APPLE, Fruit.APPLE], f.value)