Skip to main content
Declare a vars schema on Cli.create() to enable typed variables. Middleware sets them with c.set(), and both middleware and command handlers read them via c.var.

Basic Usage

How It Works

  1. Declare the schema — pass vars to Cli.create() with a Zod object schema
  2. Set values in middleware — call c.set(key, value) to populate variables
  3. Read in handlers — access c.var.key in middleware or command handlers

Variable Defaults

Use .default() for variables that don’t need middleware:

Middleware Example

Authentication

Request Context

Type Inference

The vars schema flows through generics to middleware and command handlers:

Middleware Type Hints

For external middleware, use the middleware helper with type parameters:

Environment + Variables

Combine CLI-level env with vars for environment variables plus middleware state:

Typing External Middleware

For middleware defined outside your CLI, pass both vars and env as type parameters:

Per-Command Middleware

Per-command middleware inherits the CLI’s vars schema:

Best Practices

Do

  • Use vars for state set by middleware (user, session, etc.)
  • Use .default() for static configuration (timeouts, flags)
  • Use z.custom<T>() for complex types
  • Type external middleware with middleware<typeof cli.vars, typeof cli.env>()

Don’t

  • Don’t use vars for arguments or options — use args and options schemas
  • Don’t use vars for environment variables — use env schema
  • Don’t mutate c.var directly — always use c.set()