Skip to content

Commit a763e13

Browse files
authored
Show origin of subcommands (#19)
1 parent 26099f1 commit a763e13

7 files changed

Lines changed: 66 additions & 6 deletions

File tree

.github/workflows/main.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ jobs:
2020
- name: Install
2121
shell: bash -l {0}
2222
run: |
23-
python -m pip install -e .
24-
python -m pip install pytest
23+
python -m pip install -e ".[test]"
2524
- name: Test
2625
shell: bash -l {0}
2726
run: |

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ 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.2] - unreleased
8+
### Changed
9+
- `pyodide --help` will now group subcommands by their package name.
10+
([#19](https://github.com/pyodide/pyodide-cli/pull/19))
11+
712
## [0.2.1] - 2022-12-20
813
### Added
914
- Define `pyodide_cli.app.typer_click_object` when `pyodide_cli` is imported from within sphinx,

pyodide_cli/app.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import sys
2+
from importlib.metadata import EntryPoint
3+
from importlib.metadata import distribution as importlib_distribution
24
from importlib.metadata import entry_points
35

46
import typer # type: ignore[import]
@@ -29,16 +31,29 @@ def callback(
2931
pass
3032

3133

34+
def entrypoint_to_pkgname(entrypoint: EntryPoint) -> str:
35+
"""Find package name from entrypoint"""
36+
37+
top_level = entrypoint.value.split(".")[0]
38+
dist = importlib_distribution(top_level)
39+
return dist.metadata["name"]
40+
41+
3242
def register_plugins():
3343
"""Register subcommands via the ``pyodide.cli`` entry-point"""
3444
eps = entry_points(group="pyodide.cli")
35-
plugins = {ep.name: ep.load() for ep in eps}
36-
for plugin_name, module in plugins.items():
45+
plugins = {ep.name: (ep.load(), ep) for ep in eps}
46+
for plugin_name, (module, ep) in plugins.items():
47+
pkgname = entrypoint_to_pkgname(ep)
3748
if isinstance(module, typer.Typer):
38-
app.add_typer(module, name=plugin_name)
49+
app.add_typer(
50+
module, name=plugin_name, rich_help_panel=f"Registered by: {pkgname}"
51+
)
3952
elif callable(module):
4053
typer_kwargs = getattr(module, "typer_kwargs", {})
41-
app.command(plugin_name, **typer_kwargs)(module)
54+
app.command(
55+
plugin_name, rich_help_panel=f"Registered by: {pkgname}", **typer_kwargs
56+
)(module)
4257
else:
4358
raise RuntimeError(f"Invalid plugin: {plugin_name}")
4459

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def main():
2+
pass
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[build-system]
2+
requires = ["setuptools"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[project]
6+
name = "plugin-test"
7+
version = "1.0.0"
8+
authors = []
9+
10+
[project.entry-points."pyodide.cli"]
11+
plugin_test = "plugin_test.main:main"

pyodide_cli/tests/test_cli.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
1+
import pathlib
2+
import subprocess
13
from multiprocessing import Process, Queue
24
from subprocess import check_output
35

6+
import pytest
7+
8+
9+
@pytest.fixture(scope="module")
10+
def plugins():
11+
12+
test_plugin = pathlib.Path(__file__).parent / "plugin-test"
13+
14+
subprocess.run(["pip", "install", str(test_plugin)])
15+
16+
yield
17+
18+
subprocess.run(["pip", "uninstall", "-y", test_plugin.name])
19+
420

521
def test_cli_help():
622
output = check_output(["pyodide", "--help"]).decode("utf-8")
@@ -33,3 +49,11 @@ def func(q: Queue, with_sphinx=False):
3349
p.join()
3450
app_dir = q.get()
3551
assert "typer_click_object" in app_dir
52+
53+
54+
def test_plugin_origin(plugins):
55+
56+
output = check_output(["pyodide", "--help"]).decode("utf-8")
57+
msg = "Registered by: plugin-test"
58+
59+
assert msg in output

setup.cfg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ install_requires =
2424
typer[all]
2525
rich
2626

27+
[options.extras_require]
28+
test =
29+
pytest
30+
2731
[options.entry_points]
2832
console_scripts =
2933
pyodide = pyodide_cli.__main__:main

0 commit comments

Comments
 (0)