Skip to content

Commit 50a092f

Browse files
committed
Implement ability to use ref to refer to content generated
Signed-off-by: Bernát Gábor <[email protected]>
1 parent 17c6a96 commit 50a092f

File tree

18 files changed

+201
-24
lines changed

18 files changed

+201
-24
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## 1.6.0 (2021-04-15)
6+
7+
- Support for using the `ref` sphinx role to refer to all anchor-able objects generated by the tool
8+
- Flags now have their reference title set (for the HTML builder this is shown when hover over the reference)
9+
- Anchors generated no longer collapse multiple subsequent `-` characters (to avoid clash when there's a flag and a
10+
positional argument with the same name)
11+
- Added a sphinx flag `sphinx_argparse_cli_prefix_document` (by default `False`) to avoid reference clashes when
12+
multiple documents generate the same reference labels
13+
- The root `prog` name now is prefixed for the root level optional/positional arguments' header (to avoid multiple
14+
anchors with the same id when multiple commands are documented in the same document)
15+
516
## 1.5.1 (2021-04-15)
617

718
- For sub-commands use the parser description first as description and only then fallback to the help message

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,25 @@ For example:
4747
:func: build_parser
4848
:prog: my-cli-program
4949
```
50+
51+
### Refer to generated content
52+
53+
The tool will register reference links to all anchors. This means that you can use the sphinx `ref` role to refer to
54+
both the (sub)command title/groups and every flag/argument. The tool offers a configuration flag
55+
`sphinx_argparse_cli_prefix_document` (change by setting this variable in `conf.py` - by default `False`). This option
56+
influences the reference ids generated. If it's false the reference will be the anchor id (the text appearing after the
57+
`'#` in the URI once you click on it). If it's true the anchor id will be prefixed by the document name (this is useful
58+
to avoid reference label clash when the same anchors are generated in multiple documents).
59+
60+
For example in case of a `tox` command, and `sphinx_argparse_cli_prefix_document=False` (default):
61+
62+
- to refer to the optional arguments group use `` :ref:`tox-optional-arguments` ``,
63+
- to refer to the run subcommand use `` :ref:`tox-run` ``,
64+
- to refer to flag `--magic` of the `run` sub-command use `` :ref:`tox-run---magic` ``.
65+
66+
For example in case of a `tox` command, and `sphinx_argparse_cli_prefix_document=True`, and the current document name
67+
being `cli`:
68+
69+
- to refer to the optional arguments group use `` :ref:`cli:tox-optional-arguments` ``,
70+
- to refer to the run subcommand use `` :ref:`cli:tox-run` ``,
71+
- to refer to flag `--magic` of the `run` sub-command use `` :ref:`cli:tox-run---magic` ``.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.. sphinx_argparse_cli::
2+
:module: parser
3+
:func: make
4+
5+
.. sphinx_argparse_cli::
6+
:module: parser
7+
:func: make
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from __future__ import annotations
2+
3+
import sys
4+
from pathlib import Path
5+
6+
sys.path.insert(0, str(Path(__file__).parent))
7+
extensions = ["sphinx_argparse_cli"]
8+
nitpicky = True
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.. sphinx_argparse_cli::
2+
:module: parser
3+
:func: make
4+
5+
.. sphinx_argparse_cli::
6+
:module: parser
7+
:func: make
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from __future__ import annotations
2+
3+
from argparse import ArgumentParser
4+
5+
6+
def make() -> ArgumentParser:
7+
parser = ArgumentParser(description="argparse tester", prog="prog")
8+
parser.add_argument("root")
9+
parser.add_argument("--root", action="store_true", help="root flag")
10+
return parser

roots/test-ref-prefix-doc/conf.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from __future__ import annotations
2+
3+
import sys
4+
from pathlib import Path
5+
6+
sys.path.insert(0, str(Path(__file__).parent))
7+
extensions = ["sphinx_argparse_cli"]
8+
nitpicky = True
9+
sphinx_argparse_cli_prefix_document = True

roots/test-ref-prefix-doc/index.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.. sphinx_argparse_cli::
2+
:module: parser
3+
:func: make
4+
5+
Reference test
6+
--------------
7+
Flag :ref:`index:prog---root` and positional :ref:`index:prog-root`.

roots/test-ref-prefix-doc/parser.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from __future__ import annotations
2+
3+
from argparse import ArgumentParser
4+
5+
6+
def make() -> ArgumentParser:
7+
parser = ArgumentParser(description="argparse tester", prog="prog")
8+
parser.add_argument("root")
9+
parser.add_argument("--root", action="store_true", help="root flag")
10+
return parser

roots/test-ref/conf.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from __future__ import annotations
2+
3+
import sys
4+
from pathlib import Path
5+
6+
sys.path.insert(0, str(Path(__file__).parent))
7+
extensions = ["sphinx_argparse_cli"]
8+
nitpicky = True

0 commit comments

Comments
 (0)