Skip to content

Commit 31c3abc

Browse files
authored
Create the typer-click object when run from sphinx (#17)
This is necessary so we can auto-generate CLI documentation using sphinx-click plugin. That's the easiest way currently do document typer CLI fastapi/typer#200 (comment)
1 parent 26aef2c commit 31c3abc

3 files changed

Lines changed: 44 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,22 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [0.2.1] - 2022-12-18
8+
### Added
9+
- Define `pyodide_cli.app.typer_click_object` when `pyodide_cli` is imported from within sphinx,
10+
to allow auto-generate CLI documentation with [sphinx-click](https://sphinx-click.readthedocs.io/en/latest/)
11+
([#17](https://github.com/pyodide/pyodide-cli/pull/17))
12+
13+
714
## [0.2.0] - 2022-09-04
815
### Added
916
- When registering commands, you can pass extra arguments to the typer's `app.command` method, by setting
1017
CLI entry point function attribute `typer_kwargs` to the corresponding kwargs dict.
11-
([#4](https://github.com/pyodide/pyodide-cli/pull/2))
18+
([#2](https://github.com/pyodide/pyodide-cli/pull/2))
1219

1320
### Changed
1421

15-
- Fix entry point registration for callable functions ([#4](https://github.com/pyodide/pyodide-cli/pull/2))
22+
- Fix entry point registration for callable functions ([#4](https://github.com/pyodide/pyodide-cli/pull/4))
1623

1724
## [0.1.0] - 2022-09-02
1825

pyodide_cli/app.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
from importlib.metadata import entry_points
23

34
import typer # type: ignore[import]
@@ -29,6 +30,7 @@ def callback(
2930

3031

3132
def register_plugins():
33+
"""Register subcommands via the ``pyodide.cli`` entry-point"""
3234
eps = entry_points(group="pyodide.cli")
3335
plugins = {ep.name: ep.load() for ep in eps}
3436
for plugin_name, module in plugins.items():
@@ -46,5 +48,10 @@ def main():
4648
app()
4749

4850

51+
if "sphinx" in sys.modules and __name__ != "__main__":
52+
# Create the typer click object to generate docs with sphinx-click
53+
register_plugins()
54+
typer_click_object = typer.main.get_command(app)
55+
4956
if __name__ == "__main__":
5057
main()

pyodide_cli/tests/test_cli.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,35 @@
1+
from multiprocessing import Process, Queue
12
from subprocess import check_output
23

34

45
def test_cli_help():
56
output = check_output(["pyodide", "--help"]).decode("utf-8")
67
msg = "A command line interface for Pyodide."
78
assert msg in output
9+
10+
11+
def test_click_click_object_defintion():
12+
# By default the typer-click-object is not defined
13+
# Run in a separate process to make s
14+
q = Queue()
15+
16+
def func(q: Queue, with_sphinx=False):
17+
import sys
18+
19+
if with_sphinx:
20+
sys.modules["sphinx"] = None # type: ignore
21+
import pyodide_cli.app
22+
23+
q.put(dir(pyodide_cli.app))
24+
25+
p = Process(target=func, args=(q,))
26+
p.start()
27+
p.join()
28+
app_dir = q.get()
29+
assert "typer_click_object" not in app_dir
30+
31+
p = Process(target=func, args=(q,), kwargs={"with_sphinx": True})
32+
p.start()
33+
p.join()
34+
app_dir = q.get()
35+
assert "typer_click_object" in app_dir

0 commit comments

Comments
 (0)