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."""