Skip to content

BrianPugh/cyclopts

Repository files navigation

Python compat PyPI ReadTheDocs codecov


Documentation: https://cyclopts.readthedocs.io

Source Code: https://github.com/BrianPugh/cyclopts


Cyclopts is a modern, easy-to-use command-line interface (CLI) framework. It offers a streamlined approach for building CLI applications with an emphasis on simplicity, extensibility, and robustness. Cyclopts aims to provide an intuitive and efficient developer experience, making python CLI development more accessible and enjoyable.

Why Cyclopts?

  • Intuitive API: Cyclopts features a straightforward and intuitive API, making it easy for developers to create complex CLI applications with minimal code.

  • Advanced Type Hinting: Cyclopts offers advanced type hinting features, allowing for more accurate and informative command-line interfaces.

  • Rich Help Generation: Automatically generates beautiful, user-friendly help messages, ensuring that users can easily understand and utilize your CLI application.

  • Extensible and Customizable: Designed with extensibility in mind, Cyclopts allows developers to easily add custom behaviors and integrate with other systems.

Installation

Cyclopts requires Python >=3.8; to install Cyclopts, run:

pip install cyclopts

Quick Start

  • Create an application using cyclopts.App.
  • Register commands with the command decorator.
  • Register a default function with the default decorator.
from cyclopts import App

app = App()


@app.command
def foo(loops: int):
    for i in range(loops):
        print(f"Looping! {i}")


@app.default
def default_action():
    print("Hello world! This runs when no command is specified.")


app()

Execute the script from the command line:

$ python demo.py
Hello world! This runs when no command is specified.

$ python demo.py foo 3
Looping! 0
Looping! 1
Looping! 2

With just a few additional lines of code, we have a full-featured CLI app. See the docs for more advanced usage.

Compared to Typer

Cyclopts is what you thought Typer was. Cyclopts's includes information from docstrings, support more complex types (even Unions and Literals!), and include proper validation support. See the documentation for a complete Typer comparison.

Consider the following short Cyclopts application:

import cyclopts
from typing import Literal

app = cyclopts.App()


@app.command
def deploy(
    env: Literal["dev", "staging", "prod"],
    replicas: int | Literal["default", "performance"] = "default",
):
    """Deploy code to an environment.

    Parameters
    ----------
    env
        Environment to deploy to.
    replicas
        Number of workers to spin up.
    """
    if replicas == "default":
        replicas = 10
    elif replicas == "performance":
        replicas = 20

    print(f"Deploying to {env} with {replicas} replicas.")


if __name__ == "__main__":
    app()