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
3636- the module path to where the parser is defined,
3737- a no argument function within that module that once called returns the constructed
3838 [ argparse] ( https://docs.python.org/3/library/argparse.html ) parser
39+ - (optional) a program name that overwrites the autodiscovered running argument parser
3940
4041``` rst
4142.. sphinx_argparse_cli::
4243 :module: a_project.cli
4344 :func: build_parser
45+ :prog: my-cli-program
4446```
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 2626 section ,
2727 title ,
2828)
29- from docutils .parsers .rst .directives import unchanged_required
29+ from docutils .parsers .rst .directives import unchanged , unchanged_required
3030from docutils .parsers .rst .states import RSTState , RSTStateMachine
3131from docutils .statemachine import StringList
3232from sphinx .util .docutils import SphinxDirective
@@ -41,7 +41,11 @@ def make_id(key: str) -> str:
4141class SphinxArgparseCli (SphinxDirective ):
4242 name = "sphinx_argparse_cli"
4343 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+ }
4549
4650 def __init__ (
4751 self ,
@@ -64,6 +68,8 @@ def parser(self) -> ArgumentParser:
6468 module_name , attr_name = self .options ["module" ], self .options ["func" ]
6569 parser_creator = getattr (__import__ (module_name , fromlist = [attr_name ]), attr_name )
6670 self ._parser = parser_creator ()
71+ if "prog" in self .options :
72+ self ._parser .prog = self .options ["prog" ]
6773 del sys .modules [module_name ] # no longer needed cleanup
6874 return self ._parser
6975
Original file line number Diff line number Diff line change 77
88
99@pytest .mark .sphinx ("html" , testroot = "basic" )
10- def test_basic_as_text (app : SphinxTestApp ) -> None :
10+ def test_basic_as_html (app : SphinxTestApp ) -> None :
1111 app .build ()
1212 outcome = (Path (app .outdir ) / "index.html" ).read_text ()
1313 assert outcome
1414
1515
1616@pytest .mark .sphinx ("html" , testroot = "complex" )
17- def test_complex_as_text (app : SphinxTestApp ) -> None :
17+ def test_complex_as_html (app : SphinxTestApp ) -> None :
1818 app .build ()
1919 outcome = (Path (app .outdir ) / "index.html" ).read_text ()
2020 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