Skip to main content

A modern CLI toolkit for building beautiful terminal applications

Project description

Clir

A modern CLI toolkit for building beautiful terminal applications in Python.

Features

  • Command Framework - Decorator-based command registration with type-annotated arguments
  • Advanced Options - Repeatable options, fixed-arity options, and flag-to-parameter aliasing
  • Rich Terminal Output - Colored text, tables, panels, progress bars, and spinners
  • Interactive Prompts - Text input, confirmation dialogs, and selection prompts
  • Type Inference - Automatically infer argument types from function signatures
  • Async Support - Commands can be async functions
  • Testing Utilities - Built-in tools for testing CLI applications

Installation

pip install pyclir

pyclir is the PyPI distribution name; the import package is clir (import clir). The name clir was already taken on PyPI.

Quick Start

from clir import ClirApp, argument, option
from clir.output import success, error, Table

app = ClirApp(
    name="mycli",
    description="My awesome CLI",
)

@app.command()
@argument("name")
@option("--count", "-c", default=1)
def greet(name: str, count: int):
    """Greet someone warmly."""
    for _ in range(count):
        success(f"Hello, {name}!")

@app.command()
def status():
    """Show system status."""
    t = Table("Component", "Status")
    t.add_row("Database", "Connected")
    t.add_row("API", "Running")
    t.show()

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

Advanced Options

@option supports repeatable options, fixed-arity options, and binding a flag to a differently-named Python parameter.

from pathlib import Path
from clir import ClirApp, argument, option

app = ClirApp(name="mycli")

@app.command()
# Repeatable: pass --salvage more than once; values collect into a list.
@option("--salvage", multiple=True, default=None, help="Salvage file (repeatable)")
# Fixed-arity: --compare A B consumes exactly two values.
@option("--compare", nargs=2, default=None, help="Two models to compare")
# dest-aliasing: the CLI flag is --in, the function parameter is in_path.
@option("--in", dest="in_path", help="Input file")
# pathlib.Path is a supported type; the function receives a Path.
@option("--out", type=Path, default=None, help="Output path")
def run(salvage, compare, in_path, out):
    # mycli run --salvage a.jsonl --salvage b.jsonl --compare m1 m2 --in data.txt
    #   salvage == ["a.jsonl", "b.jsonl"]   (None when not passed)
    #   compare == ["m1", "m2"]             (None when not passed)
    #   in_path == "data.txt"
    ...
  • multiple=True — the option may be passed repeatedly; every value is collected into a list. Use default=None to distinguish "not passed" from "passed empty".
  • nargs=N — the option consumes exactly N values at once; the bound value is an N-element list. Combine with multiple=True for a list of fixed-length lists.
  • dest="..." — binds the option (or argument) to a Python parameter whose name differs from the flag spelling — useful when the flag would be a Python keyword (--in, --class). Works on @argument too.
  • type=str, int, float, bool, and pathlib.Path are all supported; for list-valued options the type converts each element.

Output Functions

from clir.output import success, error, warning, info, debug, Table, Panel

success("Operation completed!")  # Green bold
error("Something went wrong")     # Red bold
warning("Be careful")             # Yellow bold
info("Here's some info")          # Cyan
debug("Debug message")            # Dim

# Tables
t = Table("Name", "Age")
t.add_row("Alice", "30")
t.add_row("Bob", "25")
t.show()

# Panels
Panel("Content", title="My Panel").show()

Interactive Prompts

from clir.prompts import prompt, confirm, select

# Text input
name = prompt("What is your name?")

# Confirmation
if confirm("Continue?", default=True):
    print("Continuing!")

# Selection
choice = select("Choose:", ["Option 1", "Option 2", "Option 3"])

Testing

from clir import ClirApp
from clir.testing import CliRunner

app = ClirApp(name="test")

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

runner = CliRunner(app)
result = runner.invoke(["hello"])

assert result.exit_code == 0
assert "Hello" in result.output

License

MIT

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

pyclir-0.2.1.tar.gz (62.9 kB view details)

Uploaded Source

Built Distribution

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

pyclir-0.2.1-py3-none-any.whl (62.5 kB view details)

Uploaded Python 3

File details

Details for the file pyclir-0.2.1.tar.gz.

File metadata

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

File hashes

Hashes for pyclir-0.2.1.tar.gz
Algorithm Hash digest
SHA256 98889aeed885771f33277eec7876e03c243dbe5291fe31431e8946445b8bfc9f
MD5 59feafbeb8c6d81475da8ab1444f5880
BLAKE2b-256 62c7a64cc9701f2eb7196e39c5194bb86a07545ba9673a0533fa9eb02b9ab7ae

See more details on using hashes here.

File details

Details for the file pyclir-0.2.1-py3-none-any.whl.

File metadata

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

File hashes

Hashes for pyclir-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e5426e16a104027d26eaed8bdceb1f12969fa5d1b0d83192c9d97f134f14838e
MD5 0f474692175d6ccdb5ef0f5ffe2a380a
BLAKE2b-256 00e72f451ef6fa5c8b071c9cde5de396fdb1030c43e1a9a543be27faaf12bcf6

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