Skip to content

Commit

Permalink
fix: detect multi-word options sections in usage
Browse files Browse the repository at this point in the history
The regex used to detect options: sections in the usage: section was
different to the one acutally used to parse sections, and it failed to
find options sections with extra words, like "Advanced Options:".

This also adds a test for the "options (case-insensitive) was found in
usage." message, which we need because currently the only test that
triggers it is test_issue_71... which does so by mistake.
  • Loading branch information
h4l committed Sep 8, 2022
1 parent f4fa27b commit 57dd340
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
7 changes: 3 additions & 4 deletions docopt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -887,10 +887,9 @@ def docopt(
)
if len(usage_sections) > 1:
raise DocoptLanguageError('More than one "usage:" (case-insensitive).')
options_pattern = re.compile(r"\n\s*?options:", re.IGNORECASE)
if options_pattern.search(usage_sections[0]):
raise DocoptExit(
"Warning: options (case-insensitive) was found in usage."
if parse_section("options:", usage_sections[0]):
raise DocoptLanguageError(
"Warning: options (case-insensitive) was found in usage. "
"Use a blank line between each section.."
)
DocoptExit.usage = usage_sections[0]
Expand Down
16 changes: 16 additions & 0 deletions tests/test_docopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,22 @@ def test_language_errors():
docopt("usage: here \n\n and again usage: here")


@pytest.mark.parametrize(
"doc",
[
pytest.param("Usage: prog\n Options: --foo\n"),
pytest.param("Usage: prog\n Advanced Options: --foo\n"),
],
)
def test_docopt_reports_options_in_usage_section(doc: str):
with raises(
DocoptLanguageError,
match=r"Warning: options \(case-insensitive\) was found in usage\. "
r"Use a blank line between each section\.\.",
):
docopt(doc)


def test_issue_40(capsys: pytest.CaptureFixture):
with raises(SystemExit): # i.e. shows help
docopt("usage: prog --help-commands | --help", "--help")
Expand Down

0 comments on commit 57dd340

Please sign in to comment.