Skip to content
You're not viewing the latest version. Click here to go to latest.

# CLI

Typer-based command-line interface for managing your FastAPI application, with built-in fixture commands integration.

## Installation

uv pip

    uv add "fastapi-toolsets[cli]"

    pip install "fastapi-toolsets[cli]"

## Overview

The `cli` module provides a `manager` entry point built with Typer. It allow custom commands to be added in addition of the fixture commands when a `FixtureRegistry` and a database context are configured.

## Configuration

Configure the CLI in your `pyproject.toml`:

    [tool.fastapi-toolsets]
    cli = "myapp.cli:cli"                   # Custom Typer app
    fixtures = "myapp.fixtures:registry"    # FixtureRegistry instance
    db_context = "myapp.db:db_context"      # Async context manager for sessions

All fields are optional. Without configuration, the `manager` command still works but no command are available.

## Usage

    # Manager commands
    manager --help

     Usage: manager [OPTIONS] COMMAND [ARGS]...

     FastAPI utilities CLI.

    ╭─ Options ────────────────────────────────────────────────────────────────────────╮
    │ --install-completion          Install completion for the current shell.          │
    │ --show-completion             Show completion for the current shell, to copy it  │
    │                               or customize the installation.                     │
    │ --help                        Show this message and exit.                        │
    ╰──────────────────────────────────────────────────────────────────────────────────╯
    ╭─ Commands ───────────────────────────────────────────────────────────────────────╮
    │ check-db                                                                         │
    │ fixtures  Manage database fixtures.                                              │
    ╰──────────────────────────────────────────────────────────────────────────────────╯

    # Fixtures commands
    manager fixtures --help

     Usage: manager fixtures [OPTIONS] COMMAND [ARGS]...

     Manage database fixtures.

    ╭─ Options ────────────────────────────────────────────────────────────────────────╮
    │ --help          Show this message and exit.                                      │
    ╰──────────────────────────────────────────────────────────────────────────────────╯
    ╭─ Commands ───────────────────────────────────────────────────────────────────────╮
    │ list  List all registered fixtures.                                              │
    │ load  Load fixtures into the database.                                           │
    ╰──────────────────────────────────────────────────────────────────────────────────╯

## Custom CLI

You can extend the CLI by providing your own Typer app. The `manager` entry point will merge your app's commands with the built-in ones:

    # myapp/cli.py
    import typer

    cli = typer.Typer()

    @cli.command()
    def hello():
        print("Hello from my app!")

    [tool.fastapi-toolsets]
    cli = "myapp.cli:cli"

API Reference