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 option directive syntax #10840

Merged
merged 1 commit into from Sep 27, 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: 3 additions & 0 deletions CHANGES
Expand Up @@ -13,6 +13,9 @@ Deprecated
Features added
--------------

* #10840: One can cross-reference including an option value: ``:option:`--module=foobar```.
Patch by Martin Liska.

Bugs fixed
----------

Expand Down
4 changes: 4 additions & 0 deletions doc/usage/restructuredtext/domains.rst
Expand Up @@ -1775,6 +1775,10 @@ There is a set of directives allowing documenting command-line programs:
referenceable by :rst:role:`option` (in the example case, you'd use something
like ``:option:`dest_dir```, ``:option:`-m```, or ``:option:`--module```).

.. versionchanged:: 5.3

One can cross-reference including an option value: ``: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
8 changes: 7 additions & 1 deletion sphinx/domains/std.py
Expand Up @@ -780,7 +780,9 @@ def process_doc(self, env: "BuildEnvironment", docname: str, document: nodes.doc
self.labels[name] = docname, labelid, sectname

def add_program_option(self, program: str, name: str, docname: str, labelid: str) -> None:
self.progoptions[program, name] = (docname, labelid)
# prefer first command option entry
if (program, name) not in self.progoptions:
self.progoptions[program, name] = (docname, labelid)
marxin marked this conversation as resolved.
Show resolved Hide resolved

def build_reference_node(self, fromdocname: str, builder: "Builder", docname: str,
labelid: str, sectname: str, rolename: str, **options: Any
Expand Down Expand Up @@ -941,6 +943,10 @@ 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:
commands = []
while ws_re.search(target):
Expand Down
13 changes: 13 additions & 0 deletions tests/roots/test-root/objects.txt
Expand Up @@ -204,6 +204,19 @@ Link to :option:`hg commit` and :option:`git commit -p`.

Foo bar.

Test repeated option directive.

.. option:: -mapi

My API.

.. option:: -mapi=secret

My secret API.

Reference the first option :option:`-mapi=secret`.


User markup
===========

Expand Down
9 changes: 9 additions & 0 deletions tests/test_build_html.py
Expand Up @@ -1764,6 +1764,15 @@ def test_option_emphasise_placeholders_default(app, status, warning):
'<a class="headerlink" href="#cmdoption-perl-plugin.option" title="Permalink to this definition">¶</a></dt>') in content


@pytest.mark.sphinx('html', testroot='root')
def test_option_reference_with_value(app, status, warning):
app.build()
content = (app.outdir / 'objects.html').read_text()
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


@pytest.mark.sphinx('html', testroot='theming')
def test_theme_options(app, status, warning):
app.build()
Expand Down