lebrice / SimpleParsing

Simple, Elegant, Typed Argument Parsing with argparse
MIT License
427 stars 52 forks source link

Optional Enum's are not parsed properly #106

Open rggjan opened 2 years ago

rggjan commented 2 years ago

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 commented 2 years ago

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

lebrice commented 2 years ago

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.

rggjan commented 2 years ago

Ah, I missed that, thanks a lot!