Repository: asottile/add-trailing-comma

Issue:
Even if successful, add-trailing-comma reading from stdin returns 1 as a status code in the shell. Is this the intended behavior?
Create a `test.py`:

```python
# -*- coding: utf-8 -*-

test = [
    1,
    2
]
```

`add-trailing-comma - < test.py` outputs correctly:

```python
# -*- coding: utf-8 -*-

test = [
    1,
    2,
]
```

But the return statuscode is `1`:

```bash
❯ echo $?                                                                                                                                                   [11:30:11]
1
```

This is a different behavior from other formatters, like black:

``` bash
black - < test.py                                                                                                                                         [11:30:46]
# -*- coding: utf-8 -*-

test = [1, 2]
reformatted -
All done! ✨ 🍰 ✨
1 file reformatted.
 ❯ echo $?                                                                                                                                                   [11:30:54]
0
```

Some formatters in editors (like [vim-autoformat](https://github.com/Chiel92/vim-autoformat)) [expect a `0` statuscode](https://github.com/Chiel92/vim-autoformat/blob/c203080645936e73b2124040bc963386eeb44f5e/plugin/autoformat.vim#L216) to make sure the formatter call was ok, and this is inconsistent with [exit codes with special meanings](https://www.tldp.org/LDP/abs/html/exitcodes.html).

Since the `stdin` feature comes from a month old PR (https://github.com/asottile/add-trailing-comma/pull/75), I think it's nice to consider changing this to be standard like other tools.

Discussion:

Patch:
diff --git a/add_trailing_comma.py b/add_trailing_comma.py
index 7052979..ea579a6 100644
--- a/add_trailing_comma.py
+++ b/add_trailing_comma.py
@@ -517,13 +517,17 @@ def fix_file(filename, args):
         with io.open(filename, 'w', newline='', encoding='UTF-8') as f:
             f.write(contents_text)
 
-    return contents_text != contents_text_orig
+    if args.exit_zero_even_if_changed:
+        return 0
+    else:
+        return contents_text != contents_text_orig
 
 
 def main(argv=None):
     # type: (Optional[Sequence[str]]) -> int
     parser = argparse.ArgumentParser()
     parser.add_argument('filenames', nargs='*')
+    parser.add_argument('--exit-zero-even-if-changed', action='store_true')
     parser.add_argument('--py35-plus', action='store_true')
     parser.add_argument('--py36-plus', action='store_true')
     args = parser.parse_args(argv)


Test patch:
diff --git a/tests/add_trailing_comma_test.py b/tests/add_trailing_comma_test.py
index 55e9bf6..e0c891a 100644
--- a/tests/add_trailing_comma_test.py
+++ b/tests/add_trailing_comma_test.py
@@ -958,3 +958,11 @@ def test_main_stdin_with_changes(capsys):
         assert main(('-',)) == 1
     out, err = capsys.readouterr()
     assert out == 'x(\n    1,\n)\n'
+
+
+def test_main_exit_zero_even_if_changed(tmpdir):
+    f = tmpdir.join('t.py')
+    f.write('x(\n    1\n)')
+    assert not main((str(f), '--exit-zero-even-if-changed'))
+    assert f.read() == 'x(\n    1,\n)'
+    assert not main((str(f), '--exit-zero-even-if-changed'))