Skip to main content

Lean CLI framework for AI-friendly micro-apps

Project description

microcli-toolkit

Lean CLI framework for AI-friendly micro-apps

Microcli is a decorator-based CLI framework designed for building tools that agents can use. Unlike traditional CLIs that return data, microcli tools return instructions that guide the next step.

Installation

pip install microcli-toolkit
# or
uv add microcli-toolkit

Quick Start

#!/usr/bin/env python3
from typing import Annotated
import microcli as m

@m.command
def hello(name: Annotated[str, "Your name"]):
    """Greet a user."""
    m.ok(f"Hello, {name}!")

if __name__ == "__main__":
    m.main()
python hello.py hello Alice
# ✓ Hello, Alice!

The Three Principles

  1. Validate before acting — Check inputs before executing
  2. Return descriptive messages — Tell the agent what to do next
  3. Use two-phase patterns — Draft → Save for safety

Parameters

Style Becomes
No default Positional argument (required)
Has default --flag optional argument
bool type Boolean flag (--flag or nothing)
def cmd(name):                    # python cmd.py cmd John
def cmd(name="World"):            # python cmd.py cmd --name John  
def cmd(verbose: bool = False):  # python cmd.py cmd --verbose

Use Annotated[type, "help text"] to add help documentation.

Utilities

Status Helpers

  • m.ok(msg) — ✓ Success message
  • m.fail(msg) — ✗ Error message + exit(1)
  • m.info(msg) — → Informational message
  • m.warn(msg) — ⚠ Warning message
  • m.step(msg) — → Step indicator

File Operations

  • m.read(path) — Read file contents
  • m.write(path, content) — Write to file
  • m.ls() / m.glob(pattern) — List files
  • m.touch(path) — Create empty file
  • m.rm(path, recursive) — Remove file/directory
  • m.cp(src, dst) — Copy file/directory
  • m.mv(src, dst) — Move/rename

Shell

  • m.sh(cmd, timeout) — Run shell command, returns Result
  • m.which(cmd) — Find command in PATH

Navigation

  • with m.cd(path): — Context manager for directory changes
  • m.env(name) — Get environment variable

Design Patterns

Two-Phase (Safety)

if not save:
    m.info("Draft mode. Rerun with --save to persist")
    return
# ... save operation

Validation First

content = sys.stdin.read().strip()
if not content:
    m.fail("No content provided via stdin")

Descriptive Outputs

# Bad:  return  # silent
# Good: m.ok("Saved to: " + str(filepath))

Follow-up Commands (.explain)

if not save:
    m.info("Draft mode. Rerun with --save:")
    m.info("  " + create.explain(title=title, save=True))
    return

--learn Mode

Auto-discovers command tours by analyzing source code:

python tool.py --learn              # Show all commands
python tool.py --learn create       # Show focused view for 'create'

Output includes:

  • Next steps — Commands discovered via .explain() calls
  • Failure modes — Errors discovered via m.fail() calls
  • Happy paths — Success messages via m.ok() calls

Complex Types from stdin

For complex data structures, use stdin[T] to read JSON from stdin. Requires pydantic extra:

uv add microcli-toolkit --extra pydantic

Basic Usage

from microcli import command, stdin

@command
def create(
    title: str,
    metadata: stdin[dict],
):
    """Create a resource with metadata."""
    m.ok(f"Creating {title} with {len(metadata)} properties")
echo '{"tags": ["a", "b"], "priority": 1}' | python cmd.py create "My Title"

With Pydantic Models

from microcli import command, stdin
from pydantic import BaseModel

class NoteMetadata(BaseModel):
    title: str
    tags: list[str] = []
    priority: int = 1

@command
def create(
    content: str,
    metadata: stdin[NoteMetadata],
):
    """Create a note with metadata."""
    m.ok(f"Creating note: {metadata.title} (priority: {metadata.priority})")
echo '{"title": "My Note", "tags": ["work", "urgent"]}' | python note.py create "Note body"

Error Handling

Invalid JSON:

✗ Invalid JSON: Expecting value

Pydantic validation errors:

✗ Validation error: 2 validation errors

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

microcli_toolkit-0.3.0.tar.gz (43.7 kB view details)

Uploaded Source

Built Distribution

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

microcli_toolkit-0.3.0-py3-none-any.whl (14.1 kB view details)

Uploaded Python 3

File details

Details for the file microcli_toolkit-0.3.0.tar.gz.

File metadata

  • Download URL: microcli_toolkit-0.3.0.tar.gz
  • Upload date:
  • Size: 43.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for microcli_toolkit-0.3.0.tar.gz
Algorithm Hash digest
SHA256 991c3193f9365d185fa36405bd3375cfc911013dd11ff0200d0ae378e917975c
MD5 64f103d74fd94d3216169e27fb680df4
BLAKE2b-256 eff1741e869e34ec92082a5c0afc736291c4c9fe429d195868a74dbbd9fa3f52

See more details on using hashes here.

File details

Details for the file microcli_toolkit-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: microcli_toolkit-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 14.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for microcli_toolkit-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e0a74ad32dde615a9a3a77fee1ca86cbdbfaba62082b9f4ff9cd7a0ce2a61b33
MD5 87f3583daa1c0a2c2baa14108c9e14d1
BLAKE2b-256 36762ab3bc44013bd69b1c15124c67eec8767b44e85be61225c0b91b2ac31069

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