diff --git a/codespell_lib/_codespell.py b/codespell_lib/_codespell.py index f5070dc2d9..e72b25d81c 100644 --- a/codespell_lib/_codespell.py +++ b/codespell_lib/_codespell.py @@ -465,6 +465,13 @@ def parse_options( default=False, help="print summary of fixes", ) + parser.add_argument( + "-m", + "--machine-readable", + action="store_true", + default=False, + help="Use alternate output format intended for parsing", + ) parser.add_argument( "--count", @@ -1018,6 +1025,15 @@ def parse_file( if (not context_shown) and (context is not None): print_context(lines, i, context) + + if options.machine_readable: + mrreason = f" ({reason})" if reason else "" + print( + f"@{filename}: line {i + 1}, col {match.start() + 1}, " + f"{word} ==> {fixword}{mrreason}" + ) + continue + if filename != "-": print( f"{cfilename}:{cline}: {cwrongword} " diff --git a/codespell_lib/tests/test_basic.py b/codespell_lib/tests/test_basic.py index 98e5dd41f0..216663a275 100644 --- a/codespell_lib/tests/test_basic.py +++ b/codespell_lib/tests/test_basic.py @@ -5,6 +5,7 @@ import re import subprocess import sys +import textwrap from io import StringIO from pathlib import Path from shutil import copyfile @@ -325,6 +326,29 @@ def test_summary( assert "abandonned" in stdout.split()[-2] +def test_machine_readable( + tmp_path: Path, + capsys: pytest.CaptureFixture[str], +) -> None: + """Test machine-readable output format.""" + fname = tmp_path / "mrfile.txt" + fname.write_text( + textwrap.dedent("""\ + abandonned + the word is abandonned + the word abandonned is wrong + """) + ) + result = cs.main(fname, "--machine-readable", std=True) + assert isinstance(result, tuple) + code, stdout, stderr = result + output_lines = [line for line in stdout.split("\n") if line] + assert all([outline.startswith("@") for outline in output_lines]) + assert "line 1, col 1, abandonned ==> abandoned" in output_lines[0] + assert "line 2, col 13" in output_lines[1] + assert "line 3, col 10" in output_lines[2] + + def test_ignore_dictionary( tmp_path: Path, capsys: pytest.CaptureFixture[str], diff --git a/pyproject.toml b/pyproject.toml index 87fa3c4b2e..c2827981e6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -169,6 +169,6 @@ max-complexity = 45 [tool.ruff.lint.pylint] allow-magic-value-types = ["bytes", "int", "str",] max-args = 13 -max-branches = 46 +max-branches = 48 max-returns = 11 -max-statements = 119 +max-statements = 125