Skip to content

Commit 29e6ada

Browse files
marxinAA-Turner
authored andcommitted
extend option directive syntax
One can cross-reference an option value: :option:`--module=foobar`.
1 parent 05683f7 commit 29e6ada

File tree

5 files changed

+36
-1
lines changed

5 files changed

+36
-1
lines changed

CHANGES

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Deprecated
1313
Features added
1414
--------------
1515

16+
* #10840: One can cross-reference including an option value: ``:option:`--module=foobar```.
17+
Patch by Martin Liska.
18+
1619
Bugs fixed
1720
----------
1821

doc/usage/restructuredtext/domains.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,6 +1775,10 @@ There is a set of directives allowing documenting command-line programs:
17751775
referenceable by :rst:role:`option` (in the example case, you'd use something
17761776
like ``:option:`dest_dir```, ``:option:`-m```, or ``:option:`--module```).
17771777

1778+
.. versionchanged:: 5.3
1779+
1780+
One can cross-reference including an option value: ``:option:`--module=foobar```.
1781+
17781782
Use :confval:`option_emphasise_placeholders` for parsing of
17791783
"variable part" of a literal text (similarly to the :rst:role:`samp` role).
17801784

sphinx/domains/std.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,9 @@ def process_doc(self, env: "BuildEnvironment", docname: str, document: nodes.doc
780780
self.labels[name] = docname, labelid, sectname
781781

782782
def add_program_option(self, program: str, name: str, docname: str, labelid: str) -> None:
783-
self.progoptions[program, name] = (docname, labelid)
783+
# prefer first command option entry
784+
if (program, name) not in self.progoptions:
785+
self.progoptions[program, name] = (docname, labelid)
784786

785787
def build_reference_node(self, fromdocname: str, builder: "Builder", docname: str,
786788
labelid: str, sectname: str, rolename: str, **options: Any
@@ -941,6 +943,10 @@ def _resolve_option_xref(self, env: "BuildEnvironment", fromdocname: str,
941943
progname = node.get('std:program')
942944
target = target.strip()
943945
docname, labelid = self.progoptions.get((progname, target), ('', ''))
946+
# for :option:`-foo=bar` search for -foo option directive
947+
if not docname and '=' in target:
948+
target2 = target[:target.find('=')]
949+
docname, labelid = self.progoptions.get((progname, target2), ('', ''))
944950
if not docname:
945951
commands = []
946952
while ws_re.search(target):

tests/roots/test-root/objects.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,19 @@ Link to :option:`hg commit` and :option:`git commit -p`.
204204

205205
Foo bar.
206206

207+
Test repeated option directive.
208+
209+
.. option:: -mapi
210+
211+
My API.
212+
213+
.. option:: -mapi=secret
214+
215+
My secret API.
216+
217+
Reference the first option :option:`-mapi=secret`.
218+
219+
207220
User markup
208221
===========
209222

tests/test_build_html.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,6 +1764,15 @@ def test_option_emphasise_placeholders_default(app, status, warning):
17641764
'<a class="headerlink" href="#cmdoption-perl-plugin.option" title="Permalink to this definition">¶</a></dt>') in content
17651765

17661766

1767+
@pytest.mark.sphinx('html', testroot='root')
1768+
def test_option_reference_with_value(app, status, warning):
1769+
app.build()
1770+
content = (app.outdir / 'objects.html').read_text()
1771+
assert ('<span class="pre">-mapi</span></span><span class="sig-prename descclassname">'
1772+
'</span><a class="headerlink" href="#cmdoption-git-commit-mapi"') in content
1773+
assert 'first option <a class="reference internal" href="#cmdoption-git-commit-mapi">' in content
1774+
1775+
17671776
@pytest.mark.sphinx('html', testroot='theming')
17681777
def test_theme_options(app, status, warning):
17691778
app.build()

0 commit comments

Comments
 (0)