Skip to content

Commit

Permalink
option ref: support optional part with '[=value]'
Browse files Browse the repository at this point in the history
This supports a commonly used format when it comes to optional argument:
-fauto-profile[=path], where `path` is optional part.

Extends: sphinx-doc#10840
  • Loading branch information
marxin committed Sep 29, 2022
1 parent 5ed06f5 commit 0f49218
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
3 changes: 2 additions & 1 deletion doc/usage/restructuredtext/domains.rst
Expand Up @@ -1777,7 +1777,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```,
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
17 changes: 13 additions & 4 deletions sphinx/domains/std.py
Expand Up @@ -942,11 +942,20 @@ def _resolve_option_xref(self, env: "BuildEnvironment", fromdocname: str,
node: pending_xref, contnode: Element) -> Optional[Element]:
progname = node.get('std:program')
target = target.strip()
docname, labelid = self.progoptions.get((progname, target), ('', ''))
needles = [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), ('', ''))
idx = target.find('=')
if idx != -1:
needles.append(target[:idx])
# for :option:`-foo[=bar]` search for -foo option directive
idx = target.find('[=')
if idx != -1:
needles.append(target[:idx])
for needle in needles:
docname, labelid = self.progoptions.get((progname, needle), ('', ''))
if docname:
break

if not docname:
commands = []
while ws_re.search(target):
Expand Down
2 changes: 1 addition & 1 deletion tests/roots/test-root/objects.txt
Expand Up @@ -214,7 +214,7 @@ Test repeated option directive.

My secret API.

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


User markup
Expand Down
2 changes: 2 additions & 0 deletions tests/test_build_html.py
Expand Up @@ -1771,6 +1771,8 @@ 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


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

0 comments on commit 0f49218

Please sign in to comment.