Repository navigation
Replies: 1 comment
|
You can implement the “installed packages extend the CLI” part using Python entry points plus Here is a minimal registration pattern for Python 3.10+ (including your 3.12 environment). The snippets assume the host and plugin are ordinary installable packages. In the plugin package, import typer
app = typer.Typer()
@app.command()
def hello(name: str):
typer.echo(f"Hello, {name}")Add this entry to that plugin package's existing [project.entry-points."example_cli.plugins"]
greetings = "example_plugin.cli:app"In the host, from importlib.metadata import entry_points
import typer
app = typer.Typer(no_args_is_help=True)
@app.callback()
def main():
"""CLI with optional command groups."""
def load_plugins():
names = set()
for ep in entry_points(group="example_cli.plugins"):
if ep.name in names:
raise RuntimeError(f"Duplicate plugin group: {ep.name}")
names.add(ep.name)
plugin = ep.load()
if not isinstance(plugin, typer.Typer):
raise TypeError(f"{ep.name} must export a Typer app")
app.add_typer(plugin, name=ep.name)
def run():
load_plugins()
app()
if __name__ == "__main__":
run()Point the host's console script at that wrapper: [project.scripts]
example-cli = "example_cli.main:run"After both packages are installed into the same environment, the invocation is This loads plugins at startup, including for help/completion; it is not lazy loading. Installed plugins must have their own dependencies available. In a larger host, reserve built-in command names too. A version detail for readers upgrading from 0.15.1: since Typer 0.26.0, extracting and modifying its internal Click app is no longer supported. The pattern above stays at the Typer API level. References: Typer app composition, PyPA plugin discovery, and Python entry-point loading. |
Uh oh!
There was an error while loading. Please reload this page.
First Check
Commit to Help
Example Code
-Description
I'm searching for documentation or a full example on how to use something like https://github.com/click-contrib/click-plugins:
I did see these
But I am missing a documentation page, that allows me to quickly get to the result — instead of having to go through multiple threads/pages.
Operating System
macOS
Operating System Details
No response
Typer Version
0.15.1
Python Version
3.12.8
Additional Context
No response
All reactions