Metadata-Version: 2.4
Name: typer-injector
Version: 0.1.0
Summary: Dependency injection for typer
Project-URL: Documentation, https://github.com/Benjy/typer-injector#readme
Project-URL: Issues, https://github.com/Benjy/typer-injector/issues
Project-URL: Source, https://github.com/Benjy/typer-injector
Author-email: Benjy Wiener <benjywiener@gmail.com>
License-Expression: MIT
License-File: LICENSE.txt
Classifier: Development Status :: 4 - Beta
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: Implementation :: PyPy
Requires-Python: >=3.10
Requires-Dist: typer>=0.20.0
Provides-Extra: dev
Requires-Dist: pytest; extra == 'dev'
Description-Content-Type: text/markdown

# typer-injector

[![PyPI - Version](https://img.shields.io/pypi/v/typer-injector.svg)](https://pypi.org/project/typer-injector)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/typer-injector.svg)](https://pypi.org/project/typer-injector)

-----

`typer-injector` adds [FastAPI-style dependency injection](https://fastapi.tiangolo.com/reference/dependencies/) to [Typer](https://typer.tiangolo.com/).

## Table of Contents

- [Installation](#installation)
- [Usage](#usage)
- [License](#license)

## Installation

```console
pip install typer-injector
```

## Usage

Example:

```python
import socket
from typing import Annotated, TypeAlias

import typer
from typer_injector import Depends, InjectingTyper


app = InjectingTyper()


def address_dependency(
    host: Annotated[str, typer.Option()],
    port: Annotated[int, typer.Option()],
) -> tuple[str, int]:
    return host, port


Address: TypeAlias = Annotated[tuple[str, int], Depends(address_dependency)]


@app.command()
def send_message(message: str, address: Address) -> None:
    """Send a message to the specified address."""
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
        sock.connect(address)
        sock.send(message.encode())


@app.command()
def receive_message(address: Address) -> None:
    """Listen for a message at the specified address."""
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
        sock.bind(address)
        sock.listen(1)
        conn, _ = sock.accept()
        with conn:
            data = conn.recv(1024)
            print(data.decode())


if __name__ == '__main__':
    app()
```


## License

`typer-injector` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.
