Skip to content

fix: -h/--help after -- prints help instead of being treated as a file name #423

Done

Description

@shashank11yadav

Describe the bug

The help check in _main looks at every argument, including those after the -- end-of-options marker. A file literally named -h (or --help) passed after -- prints the usage text and exits 0 instead of being checked or formatted. With --check a CI gate passes silently. With --in-place the file is skipped. This is rare, since the file must be named like an option.

To reproduce

import os
import subprocess
import sys
import tempfile

d = tempfile.mkdtemp()
with open(os.path.join(d, "-h"), "w") as f:
    f.write('def f():\n    """   Needs fixing.   """\n')

p = subprocess.run(
    [sys.executable, "-m", "docformatter", "--check", "--", "-h"],
    cwd=d, capture_output=True, text=True,
)
print("EXPECTED: file '-h' after '--' is checked (exit 3, no usage text)")
print("ACTUAL: exit %d, usage printed: %s" % (p.returncode, "usage:" in p.stdout.lower()))
print(p.stdout[:80].replace("\n", " | "), p.stderr[:200])

Output on master (847bdb7):

EXPECTED: file '-h' after '--' is checked (exit 3, no usage text)
ACTUAL: exit 0, usage printed: True
usage: docformatter [-h] [-i | -c] [-d] [-r] [-e [EXCLUDE ...]] |                  

The output is the same on the released 1.7.8.

Expected behavior

Tokens after -- are file names, as argparse treats them. The file -h should be checked, and --check should exit 3 because it needs changes.

Versions

  • docformatter 1.7.8 and master @ 847bdb7
  • Python 3.12, Linux

Additional context

src/docformatter/__main__.py:116 tests "--help" in configurator.args_lst or "-h" in configurator.args_lst over all tokens. The --config pre-scan in src/docformatter/configuration.py already stops at -- (line 104). Limiting the help check to the tokens before the first -- would fix it.

I have a fix with a regression test ready and will open a pull request for it right after filing this issue; it will link back here.

Activity

  1. added
    fresh
    on Nov 29, 2026
  2. shashank11yadav commented on Nov 30, 2026

    @shashank11yadav
    ContributorAuthor

    A correction to my report: a plain file named -h is not a Python file, so docformatter skips it even after the fix, and --check still exits 0 there. The case the bug really affects is a directory named -h given after -- with --recursive: the usage text is printed and the check exits 0 without reading the Python files in it. That is the case #424 tests. Sorry for the confusion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    fresh

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions