Skip to main content

Framework for building CLIs from Python callables

Project description

Interfacy

Tests PyPI version Supported versions Downloads license

Interfacy is a CLI framework that turns Python functions, classes, and class instances into command-line interfaces. It derives the CLI from signatures, type annotations, and docstrings instead of making you define it twice.

Features

  • Generate CLIs from functions, classes, class methods, and class instances.
  • Nested subcommands and manual command groups with aliases.
  • Type-driven parsing from annotations, with support for custom parsers.
  • Model expansion for dataclasses, Pydantic models, and plain classes.
  • --help text generated from docstrings.
  • Highly customizable help output with multiple layouts, color themes, and configurable ordering.
  • Stdin piping support with configurable routing to parameters.
  • Optional tab completion via argcomplete.

Installation

From PyPI

pip install interfacy
uv add interfacy

From source

pip install git+https://github.com/zigai/interfacy.git
uv add "git+https://github.com/zigai/interfacy.git"

Quick Start

from interfacy import Interfacy

def greet(name: str, times: int = 1) -> None:
    """Print a greeting."""
    print(" ".join([f"Hello, {name}!" for _ in range(times)]))

if __name__ == "__main__":
    Interfacy().run(greet)
$ python app.py Ada
Hello, Ada!

$ python app.py Ada --times 2
Hello, Ada! Hello, Ada!

By default, required non-boolean parameters become positional arguments and optional parameters become flags.

Class-Based Commands

Classes become command namespaces. __init__ parameters live at the command level and public methods become subcommands.

from interfacy import Interfacy

class Calculator:
    def __init__(self, precision: int = 2) -> None:
        self.precision = precision

    def add(self, a: float, b: float) -> float:
        return round(a + b, self.precision)

    def mul(self, a: float, b: float) -> float:
        return round(a * b, self.precision)

if __name__ == "__main__":
    Interfacy(print_result=True).run(Calculator)
$ python app.py --precision 3 add 1.2345 2.3445
3.579

Structured Parameters

Dataclasses, Pydantic models, and plain classes with typed __init__ parameters can be expanded into nested flags and reconstructed before execution.

from dataclasses import dataclass
from interfacy import Interfacy

@dataclass
class Address:
    city: str
    postal_code: int

@dataclass
class User:
    name: str
    age: int
    address: Address | None = None

def greet(user: User) -> str:
    return f"Hello {user.name}, age {user.age}"

if __name__ == "__main__":
    Interfacy(print_result=True).run(greet)
$ python app.py --user.name Ada --user.age 32
Hello Ada, age 32

Manual Groups

Use CommandGroup when your command tree is not naturally rooted in one callable:

from interfacy import CommandGroup, Interfacy

def clone(url: str) -> str:
    return f"clone:{url}"

class Releases:
    def cut(self, version: str) -> str:
        return f"cut:{version}"

ops = CommandGroup("ops", description="Operational commands")
ops.add_command(clone)
ops.add_command(Releases)

if __name__ == "__main__":
    Interfacy(print_result=True).run(ops)

Interfacy CLI Entrypoint

Interfacy also ships a CLI that can run an existing function, class, or class instance directly from a module or Python file:

$ interfacy app.py:greet Ada
$ interfacy app.py:greet --help
$ interfacy package.cli:Calculator add 1 2

The entrypoint supports configuration via TOML, loaded from ~/.config/interfacy/config.toml or INTERFACY_CONFIG.

usage: interfacy [--help] [--version] [--config-paths] [TARGET] ...

Interfacy is a framework for building CLIs from Python callables.

positional arguments:
  TARGET                      Python file or module with a function/class/instance symbol (e.g. main.py:main, pkg.cli:App, pkg.cli:service).
  ARGS                        Arguments passed through to the target command.

options:
  --help                      show this help message and exit
  --version                   show version and exit.
  --config-paths              print config file search paths and exit.

Use 'interfacy TARGET --help' to display the help text for the target.

Backend Selection

Interfacy uses the argparse backend by default. To use the Click backend, install the optional dependency and pass backend="click":

pip install "interfacy[click]"
from interfacy import Interfacy

Interfacy(backend="click", print_result=True).run(greet)

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

interfacy-0.5.3.tar.gz (444.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

interfacy-0.5.3-py3-none-any.whl (157.1 kB view details)

Uploaded Python 3

File details

Details for the file interfacy-0.5.3.tar.gz.

File metadata

  • Download URL: interfacy-0.5.3.tar.gz
  • Upload date:
  • Size: 444.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for interfacy-0.5.3.tar.gz
Algorithm Hash digest
SHA256 2ad58b1c326fef688cfb0b7bdd0e4a841ca6bbfc333c0074347d5b34a74a329a
MD5 1f9be44721b5e9461e140a034cd47f71
BLAKE2b-256 b0a28fed9fb80eff85945a76b1247d2ab73e103e71143e9c5542df592298c629

See more details on using hashes here.

File details

Details for the file interfacy-0.5.3-py3-none-any.whl.

File metadata

  • Download URL: interfacy-0.5.3-py3-none-any.whl
  • Upload date:
  • Size: 157.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for interfacy-0.5.3-py3-none-any.whl
Algorithm Hash digest
SHA256 227857d886dfe75e07c70f5e80f07b4aa7f032b2ccc8ecddd038d2b5117f45fa
MD5 6e483f1565cb40bee820e8a68081a425
BLAKE2b-256 f9ea484e5fff451a619e0f80a13988659ff04e6e8000504f08414115ea481b38

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page