Repository: akaihola/darker

Issue:
Option to choose formatter, Black as default
There are feature requests for
- using Ruff or Blue instead of Black as the formatter
  - #521
  - #251
- just running isort and/or flynt but no formatter at all
  - #304

For these, a command line and configuration option for choosing (or omitting) the formatter is needed.

In discussion for #304, a `--format=` or `--formatter=` option was proposed. The `--format=` variant was preferred since it corresponds to the name of the `--lint=` option (not `--linter=`). On the other hand, now that linting is moving to the separate Graylint package anyway, maybe we could also use the more descriptive variant `--formatter=`.

In the first phase, we would just add `--formatter=` with the default value of `--formatter=black` and no other supported options. This would pave the way for adding other formatters.

In `pyproject.toml`, a corresponding `formatter =` option would be added, with `formatter = "black"` as  the default.

See also:
- [many-formatters](/users/akaihola/projects/4/views/5) sub-project

Discussion:

Patch:
diff --git a/CHANGES.rst b/CHANGES.rst
index 2c4a937..e69f730 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -13,6 +13,8 @@ Added
 - The ``--preview`` configuration flag is now supported in the configuration files for
   Darker and Black
 - Prevent Pylint from updating beyond version 3.2.7 due to dropped Python 3.8 support.
+- The ``--formatter=black`` option (the default) has been added in preparation for
+  future formatters.
 
 Removed
 -------
diff --git a/README.rst b/README.rst
index 7acfc0d..e378e9d 100644
--- a/README.rst
+++ b/README.rst
@@ -370,6 +370,8 @@ The following `command line arguments`_ can also be used to modify the defaults:
        [py33\|py34\|py35\|py36\|py37\|py38\|py39\|py310\|py311\|py312\|py313] Python
        versions that should be supported by Black's output. [default: per-file auto-
        detection]
+--formatter FORMATTER
+       Formatter to use for reformatting code
 
 To change default values for these options for a given project,
 add a ``[tool.darker]`` section to ``pyproject.toml`` in the project's root directory,
diff --git a/src/darker/command_line.py b/src/darker/command_line.py
index fc72274..ab7a736 100644
--- a/src/darker/command_line.py
+++ b/src/darker/command_line.py
@@ -83,6 +83,13 @@ def make_argument_parser(require_src: bool) -> ArgumentParser:
         metavar="VERSION",
         choices=[v.name.lower() for v in TargetVersion],
     )
+    add_arg(
+        "Formatter to use for reformatting code",
+        "--formatter",
+        default="black",
+        choices=["black"],
+        metavar="FORMATTER",
+    )
     return parser
 
 
diff --git a/src/darker/config.py b/src/darker/config.py
index 125d34c..25f9c9e 100644
--- a/src/darker/config.py
+++ b/src/darker/config.py
@@ -44,6 +44,7 @@ class DarkerConfig(BaseConfig, total=False):
     skip_magic_trailing_comma: bool
     line_length: int
     target_version: str
+    formatter: str
 
 
 class OutputMode:


Test patch:
diff --git a/src/darker/tests/test_command_line.py b/src/darker/tests/test_command_line.py
index 49f9e3f..ac324b1 100644
--- a/src/darker/tests/test_command_line.py
+++ b/src/darker/tests/test_command_line.py
@@ -183,6 +183,30 @@ def get_darker_help_output(capsys):
         expect_config=("target_version", "py37"),
         expect_modified=("target_version", "py37"),
     ),
+    dict(
+        argv=["--formatter", "black", "."],
+        expect_value=("formatter", "black"),
+        expect_config=("formatter", "black"),
+        expect_modified=("formatter", ...),
+    ),
+    dict(
+        argv=["--formatter=black", "."],
+        expect_value=("formatter", "black"),
+        expect_config=("formatter", "black"),
+        expect_modified=("formatter", ...),
+    ),
+    dict(
+        argv=["--formatter", "rustfmt", "."],
+        expect_value=SystemExit,
+        expect_config=None,
+        expect_modified=None,
+    ),
+    dict(
+        argv=["--formatter=rustfmt", "."],
+        expect_value=SystemExit,
+        expect_config=None,
+        expect_modified=None,
+    ),
     dict(
         argv=["--target-version", "py39", "."],
         expect_value=("target_version", "py39"),