Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file.
- Register flags and positional arguments as Sphinx program options so the `:option:` role links to them.
- Leave apostrophes inside words (`don't`, `it's`) alone in help text instead of turning them into broken inline
literals.
- Make `:hook:` intercept `parse_intermixed_args()` as well as `parse_args()`.

## 1.13.1

Expand Down
8 changes: 8 additions & 0 deletions roots/test-hook-intermixed/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from __future__ import annotations

import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent))
extensions = ["sphinx_argparse_cli"]
nitpicky = True
4 changes: 4 additions & 0 deletions roots/test-hook-intermixed/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.. sphinx_argparse_cli::
:module: parser
:func: main
:hook:
10 changes: 10 additions & 0 deletions roots/test-hook-intermixed/parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from __future__ import annotations

from argparse import ArgumentParser


def main() -> None:
parser = ArgumentParser(prog="foo", add_help=False)
parser.add_argument("--flag", help="a flag")
args = parser.parse_intermixed_args()
print(args) # noqa: T201
17 changes: 9 additions & 8 deletions src/sphinx_argparse_cli/_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,15 @@ def parser(self) -> ArgumentParser:
raise self.error(msg) # noqa: B904
parser: ArgumentParser | None = None
if "hook" in self.options:
original_parse_known_args = ArgumentParser.parse_known_args
ArgumentParser.parse_known_args = _parse_known_args_hook # type: ignore[method-assign,assignment]
try:
parser_creator()
except HookError as hooked:
parser = hooked.parser
finally:
ArgumentParser.parse_known_args = original_parse_known_args
# parse_intermixed_args bypasses parse_known_args since Python 3.12, so both entry points need the hook
with (
patch.object(ArgumentParser, "parse_known_args", _parse_known_args_hook),
patch.object(ArgumentParser, "parse_known_intermixed_args", _parse_known_args_hook),
):
try:
parser_creator()
except HookError as hooked:
parser = hooked.parser
else:
parser = parser_creator()

Expand Down
18 changes: 18 additions & 0 deletions tests/test_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,24 @@ def test_hook(build_outcome: str) -> None:
assert build_outcome


@pytest.mark.sphinx(buildername="text", testroot="hook-intermixed")
def test_hook_intermixed(build_outcome: str) -> None:
assert (
build_outcome
== """foo - CLI interface
*******************

foo [--flag FLAG]


foo options
===========

* **"--flag"** "FLAG" - a flag
"""
)


@pytest.mark.sphinx(buildername="text", testroot="hook-fail")
def test_hook_fail(app: SphinxTestApp, warning: StringIO) -> None:
app.build()
Expand Down