Skip to main content

CLI framework for building command-line interfaces from Python functions, classes, and class instances

Project description

Interfacy

Tests PyPI version Supported versions Downloads license

Interfacy is a CLI framework for building command-line interfaces from Python functions, classes, and class instances using type annotations and docstrings.

Features

  • Generate CLIs from functions, class methods, or class instances.
  • Nested subcommands and command groups with aliases.
  • Type inference from annotations, with support for custom parsers.
  • --help text generated from docstrings.
  • Run a target function, class, or class instance directly from the CLI (e.g. interfacy path.py:main).
  • Multiple help layouts and color themes.
  • Optional class initializer arguments exposed as CLI options.
  • Argparse-compatible backend, including a drop-in ArgumentParser replacement.
  • 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 Argparser

def greet(name: str, times: int = 1) -> None:
    for _ in range(times):
        print(f"Hello, {name}!")

if __name__ == "__main__":
    Argparser().run(greet)

Classes as flags

from dataclasses import dataclass
from interfacy import Argparser

@dataclass
class Address:
    """Mailing address data for a user.

    Args:
        city: City name.
        zip: Postal or ZIP code.
    """
    city: str
    zip: int

@dataclass
class User:
    """User profile information for the CLI.

    Args:
        name: Display name.
        age: Age in years.
        address: Optional mailing address details.
    """
    name: str
    age: int
    address: Address | None = None

def greet(user: User) -> str:
    if user.address is None:
        return f"Hello {user.name}, age {user.age}"
    return f"Hello {user.name}, age {user.age} from {user.address.city} {user.address.zip}"

if __name__ == "__main__":
    Argparser(print_result=True).run(greet)

Help output:

usage: app.py greet [--help] --user.name USER.NAME --user.age USER.AGE
                    [--user.address.city] [--user.address.zip]

options:
  --help                      show this help message and exit
  --user.name                 Display name. [type: str] (*)
  --user.age                  Age in years. [type: int] (*)
  --user.address.city         City name. [type: str]
  --user.address.zip          Postal or ZIP code. [type: int]

Class-based commands

from interfacy import Argparser

class Calculator:
    def add(self, a: int, b: int) -> int:
        return a + b

    def mul(self, a: int, b: int) -> int:
        return a * b

if __name__ == "__main__":
    Argparser(print_result=True).run(Calculator)

Decorator-based commands

from interfacy import Argparser

parser = Argparser()

@parser.command()
def greet(name: str) -> str:
    return f"Hello, {name}!"

@parser.command(name="calc", aliases=["c"])
class Calculator:
    def add(self, a: int, b: int) -> int:
        return a + b

    def mul(self, a: int, b: int) -> int:
        return a * b

if __name__ == "__main__":
    parser.run()

CLI entrypoint

Use the CLI entrypoint to run a target function, class, or class instance from a module or file, or to inspect its help:

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

Interfacy is a CLI framework for building command-line interfaces 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.

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.4.6.tar.gz (183.7 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.4.6-py3-none-any.whl (116.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for interfacy-0.4.6.tar.gz
Algorithm Hash digest
SHA256 e2c18c909916a46f928b9f1bc5ba86c344af7007f1909c5f6b3dfcaba57a478a
MD5 2d50a9fd2b400b374b246443fd5881e0
BLAKE2b-256 4e41e4c55516ad21d126a0d4413bfafd8dce7319fc4b6a6e2b157030c5221469

See more details on using hashes here.

File details

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

File metadata

  • Download URL: interfacy-0.4.6-py3-none-any.whl
  • Upload date:
  • Size: 116.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.4.6-py3-none-any.whl
Algorithm Hash digest
SHA256 66cf89a228d76b85582b6551feb0b1166f477ba815901f58d610f12786da7ceb
MD5 553c971722e51835a75569459dd9ea6b
BLAKE2b-256 26d04b0acbad81716838bc8c2e3d15a36d684ad8beda76db02f269dd9e238936

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