Repository: malte-soe/dataclass-cli

Issue:
Custom name also needs to be lower case

Discussion:

Patch:
diff --git a/dataclass_cli/parser.py b/dataclass_cli/parser.py
index 14db656..99715be 100644
--- a/dataclass_cli/parser.py
+++ b/dataclass_cli/parser.py
@@ -2,13 +2,13 @@ import argparse
 import dataclasses
 import enum
 import logging
+import typing
 from functools import partial
-from typing import Dict, List, Union
 
 
 class Options(str, enum.Enum):
-    POSSIBLE_VALUES = enum.auto()
-    HELP_TEXT = enum.auto()
+    POSSIBLE_VALUES = "possible_values"
+    HELP_TEXT = "help_text"
 
 
 def add(cls=None, *, name=None, **kwargs):
@@ -17,29 +17,70 @@ def add(cls=None, *, name=None, **kwargs):
     return _add(cls, name=name, **kwargs)
 
 
+def _add_type_basic(
+    group: argparse._ArgumentGroup, field: dataclasses.Field, name: str
+):
+    group.add_argument(
+        f"--{name}_{field.name}",
+        type=field.type,
+        default=None if field.default is dataclasses.MISSING else field.default,
+        required=field.default is dataclasses.MISSING,
+        choices=field.metadata.get(Options.POSSIBLE_VALUES, None),
+        help=field.metadata.get(Options.HELP_TEXT, None),
+    )
+
+
+def _add_type_list(group: argparse._ArgumentGroup, field: dataclasses.Field, name: str):
+    group.add_argument(
+        f"--{name}_{field.name}",
+        type=typing.get_args(field.type)[0],
+        default=None
+        if field.default_factory is dataclasses.MISSING  # type: ignore
+        else field.default_factory(),  # type: ignore
+        required=field.default_factory is dataclasses.MISSING,  # type: ignore
+        choices=field.metadata.get(Options.POSSIBLE_VALUES, None),
+        help=field.metadata.get(Options.HELP_TEXT, None),
+        nargs="*",
+    )
+
+
+_add_argument_: typing.Dict[
+    type, typing.Callable[[argparse._ArgumentGroup, dataclasses.Field, str], None]
+] = {
+    int: _add_type_basic,
+    float: _add_type_basic,
+    str: _add_type_basic,
+    typing.List[int]: _add_type_list,
+    typing.List[float]: _add_type_list,
+    typing.List[str]: _add_type_list,
+}
+
+
 def _add(
     cls,
     *,
     name: str = "",
-    _classes: Dict[str, List[str]] = {},
+    _classes: typing.Dict[str, typing.List[str]] = {},
     _parser=argparse.ArgumentParser(),
 ):
     assert dataclasses.is_dataclass(cls)
 
-    name = name or cls.__name__.lower()
+    name = name or cls.__name__
+    name = name.lower()
     assert name not in _classes
     _classes[name] = [arg.name for arg in dataclasses.fields(cls)]
 
     group = _parser.add_argument_group(name)
     for field in dataclasses.fields(cls):
-        group.add_argument(
-            f"--{name}_{field.name}",
-            type=field.type,
-            default=None if field.default is dataclasses.MISSING else field.default,
-            required=field.default is dataclasses.MISSING,
-            choices=field.metadata.get(Options.POSSIBLE_VALUES, None),
-            help=field.metadata.get(Options.HELP_TEXT, None),
-        )
+        _add_argument_[field.type](group, field, name)
+        # group.add_argument(
+        #     f"--{name}_{field.name}",
+        #     type=field.type,
+        #     default=None if field.default is dataclasses.MISSING else field.default,
+        #     required=field.default is dataclasses.MISSING,
+        #     choices=field.metadata.get(Options.POSSIBLE_VALUES, None),
+        #     help=field.metadata.get(Options.HELP_TEXT, None),
+        # )
 
     original_init = cls.__init__
 


Test patch:
diff --git a/tests/test_dc_cli.py b/tests/test_dc_cli.py
index 1695157..19e2552 100644
--- a/tests/test_dc_cli.py
+++ b/tests/test_dc_cli.py
@@ -1,4 +1,5 @@
 import argparse
+import typing
 import unittest
 from dataclasses import dataclass, field
 from functools import partial
@@ -159,3 +160,83 @@ class TestDcCli(unittest.TestCase):
         with mock.patch("sys.argv", testargs):
             dc = Dataclass(number=number)
         self.assertEqual(number, dc.number)
+
+    def test_default_list_arg(self):
+        testing_lists = [[], [1], [1, 2], ["a"], ["a", "b"], [0.5], [0.5, 0.2]]
+        for idx, test_list in enumerate(testing_lists):
+
+            element_type = int if (len(test_list) == 0) else type(test_list[0])
+
+            @self.add(name=f"dataclass_{idx}")
+            @dataclass
+            class Dataclass:
+                args: typing.List[element_type] = field(
+                    default_factory=lambda: test_list
+                )
+
+            testargs = [f"test.py"]
+            with mock.patch("sys.argv", testargs):
+                dc = Dataclass()
+            self.assertEqual(test_list, dc.args)
+
+    def test_list_arg(self):
+        testing_lists = [[1], [1, 2], ["a"], ["a", "b"], [0.5], [0.5, 0.2]]
+        for idx, test_list in enumerate(testing_lists):
+
+            element_type = int if (len(test_list) == 0) else type(test_list[0])
+            name = f"dataclass_{idx}"
+
+            @self.add(name=name)
+            @dataclass
+            class Dataclass:
+                args: typing.List[element_type] = field(default_factory=lambda: [])
+
+            testargs = [f"test.py", f"--{name}_args"]
+            testargs.extend(map(str, test_list))
+            with mock.patch("sys.argv", testargs):
+                dc = Dataclass()
+            self.assertEqual(test_list, dc.args)
+
+    def test_possible_values_list_arg(self):
+        testing_lists = [[1], [1, 2], ["a"], ["a", "b"], [0.5], [0.5, 0.2]]
+        for idx, test_list in enumerate(testing_lists):
+
+            element_type = int if (len(test_list) == 0) else type(test_list[0])
+            name = f"dataclass_{idx}"
+
+            @self.add(name=name)
+            @dataclass
+            class Dataclass:
+                args: typing.List[element_type] = field(
+                    default_factory=lambda: [],
+                    metadata={dataclass_cli.Options.POSSIBLE_VALUES: test_list},
+                )
+
+            testargs = [f"test.py", f"--{name}_args"]
+            testargs.extend(map(str, test_list))
+            with mock.patch("sys.argv", testargs):
+                dc = Dataclass()
+            self.assertEqual(test_list, dc.args)
+
+    def test_no_possible_values_list_arg(self):
+        testing_lists = [[1], [1, 2], ["a"], ["a", "b"], [0.5], [0.5, 0.2]]
+        for idx, test_list in enumerate(testing_lists):
+
+            element_type = int if (len(test_list) == 0) else type(test_list[0])
+            name = f"dataclass_{idx}"
+
+            @self.add(name=name)
+            @dataclass
+            class Dataclass:
+                args: typing.List[element_type] = field(
+                    default_factory=lambda: [],
+                    metadata={dataclass_cli.Options.POSSIBLE_VALUES: test_list},
+                )
+
+            testargs = [f"test.py", f"--{name}_args"]
+            testargs.extend(map(str, test_list + ["10"]))
+            with mock.patch("sys.argv", testargs), self.assertRaises(
+                SystemExit
+            ), mock.patch("sys.stderr", new=StringIO()) as fake_out:
+                _ = Dataclass()
+            self.assertIn("invalid choice", fake_out.getvalue())