From 9c218b59c1980cbaf430258dd7e7c4c4be119ef7 Mon Sep 17 00:00:00 2001 From: Shashank Yadav <65058495+shashank11yadav@users.noreply.github.com> Date: Sat, 28 Nov 2026 23:43:26 -0700 Subject: [PATCH] fix: stop the help check at the -- end-of-options marker The -h / --help pre-check in _main scanned every command-line token, including the ones after "--". A path named -h passed after the marker printed the usage text and exited 0 without formatting or checking anything, so "docformatter --check --recursive -- -h" passed silently. Cut the token list at the first "--" before looking for the help flags. --- src/docformatter/__main__.py | 7 ++++++- tests/test_docformatter.py | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/docformatter/__main__.py b/src/docformatter/__main__.py index 69f71a6..83e5987 100755 --- a/src/docformatter/__main__.py +++ b/src/docformatter/__main__.py @@ -113,7 +113,12 @@ def _main(argv, standard_out, standard_error, standard_in): """Run internal main entry point.""" configurator = _configuration.Configurater(argv) - if "--help" in configurator.args_lst or "-h" in configurator.args_lst: + # Tokens after the "--" end-of-options marker are file names, not options. + _options = configurator.args_lst + if "--" in _options: + _options = _options[: _options.index("--")] + + if "--help" in _options or "-h" in _options: _help() return 0 else: diff --git a/tests/test_docformatter.py b/tests/test_docformatter.py index 89f8d3f..0465b05 100644 --- a/tests/test_docformatter.py +++ b/tests/test_docformatter.py @@ -331,6 +331,29 @@ def test_help_output(self): ) assert ret_code == 0 + @pytest.mark.system + def test_help_flag_after_end_of_options_is_a_path( + self, tmp_path, monkeypatch, capsys + ): + """Treat -h after the -- marker as a path, not as a help request.""" + monkeypatch.chdir(tmp_path) + (tmp_path / "-h").mkdir() + (tmp_path / "-h" / "bad.py").write_text( + 'def f():\n """ Needs fixing. """\n' + ) + stderr = io.StringIO() + + ret_code = main._main( + argv=["my_fake_program", "--check", "--recursive", "--", "-h"], + standard_out=io.StringIO(), + standard_error=stderr, + standard_in=None, + ) + + assert ret_code == 3 # FormatResult.format_required + assert "usage:" not in capsys.readouterr().out + assert stderr.getvalue().strip() == os.path.join("-h", "bad.py") + class TestEndToEnd: """Class to test docformatter by executing it from the command line."""