Repository: malte-soe/dataclass-cli

Issue:
Customize group name
Currently the decorator uses the class name as group name. Add ability to set a custom group name

Discussion:

Patch:
diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml
index 35e558f..3cc4d10 100644
--- a/.github/workflows/coverage.yml
+++ b/.github/workflows/coverage.yml
@@ -1,5 +1,8 @@
 name: Codecoverage
-on: [push]
+on:
+  pull_request:
+  push:
+    branches: [master]
 jobs:
   run:
     runs-on: ubuntu-latest
diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml
index 73b2095..c147950 100644
--- a/.github/workflows/pre-commit.yml
+++ b/.github/workflows/pre-commit.yml
@@ -1,10 +1,8 @@
 name: pre-commit
-
 on:
   pull_request:
   push:
     branches: [master]
-
 jobs:
   pre-commit:
     runs-on: ubuntu-latest
diff --git a/dataclass_cli/parser.py b/dataclass_cli/parser.py
index 4caf33b..38be763 100644
--- a/dataclass_cli/parser.py
+++ b/dataclass_cli/parser.py
@@ -1,6 +1,7 @@
 import argparse
 import dataclasses
 import enum
+from functools import partial
 from typing import Dict, List, Union
 
 
@@ -9,16 +10,23 @@ class Options(str, enum.Enum):
     HELP_TEXT = enum.auto()
 
 
-def add(
+def add(cls=None, *, name=None, **kwargs):
+    if cls is None:
+        return partial(_add, name=name, **kwargs)
+    return _add(cls, name=name, **kwargs)
+
+
+def _add(
     cls,
     *,
+    name: str = "",
     _classes: Dict[str, List[str]] = {},
     _parsed_args: Dict[str, Union[int, str]] = {},
     _parser=argparse.ArgumentParser(),
 ):
     assert dataclasses.is_dataclass(cls)
 
-    name = cls.__name__.lower()
+    name = name or cls.__name__.lower()
     assert name not in _classes
     _classes[name] = [arg.name for arg in dataclasses.fields(cls)]
 


Test patch:
diff --git a/tests/test_dc_cli.py b/tests/test_dc_cli.py
index 02e0e8a..f4f7319 100644
--- a/tests/test_dc_cli.py
+++ b/tests/test_dc_cli.py
@@ -133,3 +133,17 @@ class TestDcCli(unittest.TestCase):
             _ = DataclassWithNoDefault()
 
         self.assertIn("the following arguments are required", fake_out.getvalue())
+
+    def test_custom_name(self):
+        name = "custom42"
+        number = 42
+
+        @self.add(name=name)
+        @dataclass
+        class Dataclass:
+            number: int
+
+        testargs = f"test.py --{name}_number {number}".split()
+        with mock.patch("sys.argv", testargs):
+            dc = Dataclass()
+        self.assertEqual(number, dc.number)