Skip to main content

Your all-in-one for beautiful, lightweight, prod-ready CLIs

Project description

🦄 clypi

PyPI version License PyPI - Python Version PyPI - Downloads Contributors

Your all-in-one for beautiful, lightweight, prod-ready CLIs

Get started

uv add clypi  # or `pip install clypi`

Examples

Check out the examples in ./examples! You can run them locally with uv run --all-extras -m examples.<example>. E.g.:

uv run --all-extras -m examples.cli

# Or:
pip install .[examples]
python -m examples.cli

Docs

Read the API docs for examples and a full API reference.

[!IMPORTANT] This project is still in development. Expect frequent and (some) breaking changes. For upcoming releases, you can follow the planned work section.

CLI

Read the docs

# examples/basic_cli.py
from clypi import Command, Positional, config

class Lint(Command):
    files: Positional[tuple[str, ...]]
    verbose = config(...)  # Comes from MyCli but I want to use it too

    async def run(self):
        print(f"Linting {', '.join(self.files)} and {self.verbose=}")

class MyCli(Command):
    """
    my-cli is a very nifty demo CLI tool
    """
    subcommand: Lint | None = None
    verbose: bool = config(
        help="Whether to show extra logs",
        prompt="Do you want to see extra logs?",
        default=False,
        short="v",  # User can pass in --verbose or -v
    )

    async def run(self):
        print(f"Running the main command with {self.verbose}")

if __name__ == "__main__":
    cli: MyCli = MyCli.parse()
    cli.start()
uv run -m examples.basic_cli lin (Typo)

image

uv run -m examples.basic_cli -h (Main help page)

image

uv run -m examples.basic_cli lint -h (Subcommand help page)

image

uv run -m examples.basic_cli (Normal run)

image

uv run -m examples.basic_cli lint (Missing args error)

image

🌈 Colors

Read the docs

# demo.py
import clypi

# Style text
print(clypi.style("This is blue", fg="blue"), "and", clypi.style("this is red", fg="red"))

# Print with colors directly
clypi.print("Some colorful text", fg="green", reverse=True, bold=True, italic=True)

# Store a styler and reuse it
wrong = clypi.styler(fg="red", strikethrough=True)
print("The old version said", wrong("Pluto was a planet"))
print("The old version said", wrong("the Earth was flat"))
uv run -m examples.colors

image

uv run demo.py

image

🌀 Spinners

Read the docs

# demo.py
import asyncio
from clypi import Spinner

async def main():
    async with Spinner("Downloading assets") as s:
        for i in range(1, 6):
            await asyncio.sleep(0.5)
            s.title = f"Downloading assets [{i}/5]"

asyncio.run(main())
uv run -m examples.spinner

uv run demo.py

❓ Prompting

Read the docs

First, you'll need to import the clypi module:

import clypi

answer = clypi.prompt("Are you going to use clypi?", default=True, parser=bool)

🔀 Async by default

clypi was built with an async-first mentality. Asynchronous code execution is incredibly valuable for applications like CLIs where we want to update the UI as we take certain actions behind the scenes. Most often, these actions can be made asynchronous since they involve things like file manipulation, network requests, subprocesses, etc.

🐍 Type-checking

This library is fully type-checked. This means that all types will be correctly inferred from the arguments you pass in.

In this example your editor will correctly infer the type:

hours = clypi.prompt(
    "How many hours are there in a year?",
    parser=lambda x: float(x) if x < 24 else timedelta(days=x),
)
reveal_type(hours)  # Type of "res" is "float | timedelta"

Why should I care?

Type checking will help you catch issues way earlier in the development cycle. It will also provide nice autocomplete features in your editor that will make you faster 󱐋.

Integrations

Parsers (v6e, pydantic, etc.)

CLIPy can be integrated with many parsers. The default recommended parser is v6e, which is automatically used if installed in your local environment to parse types more accurately. If you wish you specify any parser (from v6e or elsewhere) manually, you can do so quite easily:

CLI

import v6e
from clypi import Command, config

class MyCli(Command):
    files: list[Path] = config(parser=v6e.path().exists().list())

    async def run(self):
        files = [f.as_posix() for f in self.files]
        print(f"Linting {', '.join(files)}")

if __name__ == "__main__":
    cli: MyCli = MyCli.parse()
    cli.start()

Prompting

import v6e

hours = clypi.prompt(
    "How many hours are there in a year?",
    parser=v6e.float().lte(24).union(v6e.timedelta()),
)
reveal_type(hours)  # Type of "res" is "float | timedelta"

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

clypi-0.1.22.tar.gz (47.3 kB view details)

Uploaded Source

Built Distribution

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

clypi-0.1.22-py3-none-any.whl (29.8 kB view details)

Uploaded Python 3

File details

Details for the file clypi-0.1.22.tar.gz.

File metadata

  • Download URL: clypi-0.1.22.tar.gz
  • Upload date:
  • Size: 47.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for clypi-0.1.22.tar.gz
Algorithm Hash digest
SHA256 6e00d1e62d33af885609b075ba4cac3edac9f37718e25e50870efd50f539592c
MD5 dc9e0e690702394c45e88d3f5ba6f981
BLAKE2b-256 ca5b8f3ecd5b03c627acbe28eea151f3145882a3b474dad585704567dc54a0a6

See more details on using hashes here.

Provenance

The following attestation bundles were made for clypi-0.1.22.tar.gz:

Publisher: release.yml on danimelchor/clypi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file clypi-0.1.22-py3-none-any.whl.

File metadata

  • Download URL: clypi-0.1.22-py3-none-any.whl
  • Upload date:
  • Size: 29.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for clypi-0.1.22-py3-none-any.whl
Algorithm Hash digest
SHA256 7b69b44b39bc2be2e54b4cb72c815b801796453693c8999fddee9dbb39d4a9ca
MD5 56364163f50efbb83dcdb87e981505b9
BLAKE2b-256 c9da4be5e9aeddd8438a01e3de780fec077e9e8ba549d914f70eb209cdf8130b

See more details on using hashes here.

Provenance

The following attestation bundles were made for clypi-0.1.22-py3-none-any.whl:

Publisher: release.yml on danimelchor/clypi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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