abseil/abseil-py

DEFINE_enum supports Enum?

TRSasasusu opened this issue · 3 comments

Are there any plans to support Enum in DEFINE_enum?

For example, main.py:

from enum import Enum, auto
from absl import app, flags

class Color(Enum):
    RED = auto()
    GREEN = auto()

flags.DEFINE_enum('color', Color.RED, Color, 'set color', use_lower_case=True) # supporting Enum
FLAGS = flags.FLAGS

def main(argv_):
    if FLAGS.color == Color.RED:
        print('color is red')

if __name__ == '__main__':
    app.run(main)

and,

$ python main.py --color red
color is red
yilei commented

Enum classes are supported by a separate function flags.DEFINE_enum_class. Does this work for you?

Thank you, @yilei !!
I'm surprised that there is flags.DEFINE_enum_class, which is not written in the document... (but exists in _defines.py)

Now, main.py:

from enum import Enum, auto
from absl import app, flags

class Color(Enum):
    RED = auto()
    GREEN = auto()

flags.DEFINE_enum_class('color', Color.RED, Color, 'set color')
FLAGS = flags.FLAGS

def main(argv_):
    if FLAGS.color == Color.RED:
        print('color is red')

if __name__ == '__main__':
    app.run(main)

and,

$ python main.py --help

       USAGE: main.py [flags]
flags:

main.py:
  --color: <red|green>: set color
    (default: 'red')

Try --helpfull to get a list of all flags.
$ python main.py --color red
color is red

It works well. Thanks!