Exemple #1
0
def DEFINE_multi_enum(name,
                      default,
                      enum_values,
                      help=None,
                      flag_values=_flagvalues.FLAGS,
                      case_sensitive=True,
                      **args):
    """Registers a flag whose value can be a list strings from enum_values.

    Use the flag on the command line multiple times to place multiple
    enum values into the list.  The 'default' may be a single string
    (which will be converted into a single-element list) or a list of
    strings.

    Args:
      name: str, the flag name.
      default: Union[Iterable[Text], Text, None], the default value of the flag;
          see `DEFINE_multi`.
      enum_values: [str], a non-empty list of strings with 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.
      case_sensitive: Whether or not the enum is to be case-sensitive.
      **args: Dictionary with extra keyword args that are passed to the
          Flag __init__.
    """
    parser = _argument_parser.EnumParser(enum_values, case_sensitive)
    serializer = _argument_parser.ArgumentSerializer()
    DEFINE_multi(parser, serializer, name, default, help, flag_values, **args)
Exemple #2
0
 def __init__(self, name, default, help, enum_values,  # pylint: disable=redefined-builtin
              short_name=None, case_sensitive=True, **args):
   p = _argument_parser.EnumParser(enum_values, case_sensitive)
   g = _argument_parser.ArgumentSerializer()
   super(EnumFlag, self).__init__(
       p, g, name, default, help, short_name, **args)
   self.help = '<%s>: %s' % ('|'.join(enum_values), self.help)
Exemple #3
0
 def test_parse_not_found(self):
     parser = _argument_parser.EnumParser(['apple', 'banana'])
     with self.assertRaises(ValueError):
         parser.parse('orange')
Exemple #4
0
 def test_parse(self):
     parser = _argument_parser.EnumParser(['apple', 'banana'])
     self.assertEqual('apple', parser.parse('apple'))
Exemple #5
0
 def test_empty_values(self):
     with self.assertRaises(ValueError):
         _argument_parser.EnumParser([])