SimpleParsing icon indicating copy to clipboard operation
SimpleParsing copied to clipboard

Optional Enum's are not parsed properly

Open rggjan opened this issue 3 years ago • 3 comments

Optional enums need to be passed to command line by value instead of name:

import enum
from dataclasses import dataclass
from typing import Optional

from simple_parsing import ArgumentParser

parser = ArgumentParser()


class Color(enum.Enum):
    RED = "red"
    ORANGE = "orange"
    BLUE = "blue"


@dataclass
class MyPreferences:
    """You can use Enums"""

    color_one: Optional[Color] = Color.BLUE
    color_two: Color = Color.BLUE


parser.add_arguments(MyPreferences, "my_preferences")
parser.parse_args(["--color_one", "red", "--color_two", "RED"])  # This works
parser.parse_args(["--color_one", "RED", "--color_two", "RED"])  # This fails

rggjan avatar Jan 25 '22 12:01 rggjan

error: argument --color_one: invalid Color value: 'RED'

rggjan avatar Jan 25 '22 12:01 rggjan

Hey @rggjan , thanks for posting!

Indeed, this was mentioned in #62 , and I've been working on a fix in #99 . I need to fix the failing tests for other python versions, and this should be good to go.

lebrice avatar Jan 26 '22 16:01 lebrice

Ah, I missed that, thanks a lot!

rggjan avatar Jan 28 '22 09:01 rggjan