File tree Expand file tree Collapse file tree 6 files changed +38
-4
lines changed Expand file tree Collapse file tree 6 files changed +38
-4
lines changed Original file line number Diff line number Diff line change @@ -36,9 +36,11 @@ Within the reStructuredText files use the `sphinx_argparse_cli` directive that t
36
36
- the module path to where the parser is defined,
37
37
- a no argument function within that module that once called returns the constructed
38
38
[ argparse] ( https://docs.python.org/3/library/argparse.html ) parser
39
+ - (optional) a program name that overwrites the autodiscovered running argument parser
39
40
40
41
``` rst
41
42
.. sphinx_argparse_cli::
42
43
:module: a_project.cli
43
44
:func: build_parser
45
+ :prog: my-cli-program
44
46
```
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
1
+ .. sphinx_argparse_cli ::
2
+ :module: parser
3
+ :func: make
4
+ :prog: magic
Original file line number Diff line number Diff line change
1
+ from __future__ import annotations
2
+
3
+ from argparse import ArgumentParser
4
+
5
+
6
+ def make () -> ArgumentParser :
7
+ return ArgumentParser (add_help = False )
Original file line number Diff line number Diff line change 26
26
section ,
27
27
title ,
28
28
)
29
- from docutils .parsers .rst .directives import unchanged_required
29
+ from docutils .parsers .rst .directives import unchanged , unchanged_required
30
30
from docutils .parsers .rst .states import RSTState , RSTStateMachine
31
31
from docutils .statemachine import StringList
32
32
from sphinx .util .docutils import SphinxDirective
@@ -41,7 +41,11 @@ def make_id(key: str) -> str:
41
41
class SphinxArgparseCli (SphinxDirective ):
42
42
name = "sphinx_argparse_cli"
43
43
has_content = False
44
- option_spec = {"module" : unchanged_required , "func" : unchanged_required }
44
+ option_spec = {
45
+ "module" : unchanged_required ,
46
+ "func" : unchanged_required ,
47
+ "prog" : unchanged ,
48
+ }
45
49
46
50
def __init__ (
47
51
self ,
@@ -64,6 +68,8 @@ def parser(self) -> ArgumentParser:
64
68
module_name , attr_name = self .options ["module" ], self .options ["func" ]
65
69
parser_creator = getattr (__import__ (module_name , fromlist = [attr_name ]), attr_name )
66
70
self ._parser = parser_creator ()
71
+ if "prog" in self .options :
72
+ self ._parser .prog = self .options ["prog" ]
67
73
del sys .modules [module_name ] # no longer needed cleanup
68
74
return self ._parser
69
75
Original file line number Diff line number Diff line change 7
7
8
8
9
9
@pytest .mark .sphinx ("html" , testroot = "basic" )
10
- def test_basic_as_text (app : SphinxTestApp ) -> None :
10
+ def test_basic_as_html (app : SphinxTestApp ) -> None :
11
11
app .build ()
12
12
outcome = (Path (app .outdir ) / "index.html" ).read_text ()
13
13
assert outcome
14
14
15
15
16
16
@pytest .mark .sphinx ("html" , testroot = "complex" )
17
- def test_complex_as_text (app : SphinxTestApp ) -> None :
17
+ def test_complex_as_html (app : SphinxTestApp ) -> None :
18
18
app .build ()
19
19
outcome = (Path (app .outdir ) / "index.html" ).read_text ()
20
20
assert outcome
21
+
22
+
23
+ @pytest .mark .sphinx ("text" , testroot = "prog" )
24
+ def test_prog_as_text (app : SphinxTestApp ) -> None :
25
+ app .build ()
26
+ outcome = (Path (app .outdir ) / "index.txt" ).read_text ()
27
+ assert outcome == "magic - CLI interface\n *********************\n \n magic\n "
You can’t perform that action at this time.
0 commit comments