Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend cross referencing options with values #10883

Merged
merged 1 commit into from Oct 2, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/usage/restructuredtext/domains.rst
Expand Up @@ -1803,7 +1803,8 @@ There is a set of directives allowing documenting command-line programs:

.. versionchanged:: 5.3

One can cross-reference including an option value: ``:option:`--module=foobar```.
One can cross-reference including an option value: ``:option:`--module=foobar```,
,``:option:`--module[=foobar]``` or ``:option:`--module foobar```.

Use :confval:`option_emphasise_placeholders` for parsing of
"variable part" of a literal text (similarly to the :rst:role:`samp` role).
Expand Down
15 changes: 11 additions & 4 deletions sphinx/domains/std.py
Expand Up @@ -943,10 +943,17 @@ def _resolve_option_xref(self, env: "BuildEnvironment", fromdocname: str,
progname = node.get('std:program')
target = target.strip()
docname, labelid = self.progoptions.get((progname, target), ('', ''))
# for :option:`-foo=bar` search for -foo option directive
if not docname and '=' in target:
target2 = target[:target.find('=')]
docname, labelid = self.progoptions.get((progname, target2), ('', ''))
if not docname:
# Support also reference that contain an option value:
# * :option:`-foo=bar`
# * :option:`-foo[=bar]`
# * :option:`-foo bar`
for needle in {'=', '[=', ' '}:
if needle in target:
stem, _, _ = target.partition(needle)
docname, labelid = self.progoptions.get((progname, stem), ('', ''))
if docname:
break
if not docname:
commands = []
while ws_re.search(target):
Expand Down
3 changes: 2 additions & 1 deletion tests/roots/test-root/objects.txt
Expand Up @@ -214,7 +214,8 @@ Test repeated option directive.

My secret API.

Reference the first option :option:`-mapi=secret`.
Reference the first option :option:`-mapi=secret`, :option:`-mapi[=xxx]`
or :option:`-mapi with_space`.


User markup
Expand Down
3 changes: 3 additions & 0 deletions tests/test_build_html.py
Expand Up @@ -1771,6 +1771,9 @@ def test_option_reference_with_value(app, status, warning):
assert ('<span class="pre">-mapi</span></span><span class="sig-prename descclassname">'
'</span><a class="headerlink" href="#cmdoption-git-commit-mapi"') in content
assert 'first option <a class="reference internal" href="#cmdoption-git-commit-mapi">' in content
assert ('<a class="reference internal" href="#cmdoption-git-commit-mapi">'
'<code class="xref std std-option docutils literal notranslate"><span class="pre">-mapi[=xxx]</span></code></a>') in content
assert '<span class="pre">-mapi</span> <span class="pre">with_space</span>' in content


@pytest.mark.sphinx('html', testroot='theming')
Expand Down