Context
The central API surface threaded through every handler and middleware. Provides typed access to args, config, log, format, store, error handling, and CLI metadata.
Properties
ctx.args
Deeply readonly parsed args for the matched command. The type is a merge of MalttyArgs (global augmentation) and the command's own args definition.
ctx.config
A ConfigHandle decorated by the config() middleware from maltty/config. Only present when the config middleware is registered. Config loads lazily by default -- call ctx.config.load() to read and validate the config file, which returns a Result tuple.
Use ConfigType with module augmentation on maltty/config to derive ConfigRegistry from your Zod schema:
Then register the config middleware:
Commands load and access config via the handle:
Pass { layers: true } to load() to include layer metadata:
Run maltty add config to scaffold this setup in an existing project, or pass --config to maltty init when creating a new project.
ctx.log
Structured logging API on the base context. All logging methods write to stderr.
ctx.prompts
Interactive prompts that suspend execution until the user provides input. Cancellation (Ctrl-C) throws a ContextError with code PROMPT_CANCELLED.
ctx.status.spinner
Manage a spinner for long-running operations.
ctx.colors
Color formatting utilities powered by picocolors. Use for coloring summary values, diagnostic output, and other terminal text.
Available formatters: bold, dim, italic, underline, red, green, yellow, blue, magenta, cyan, white, gray, and more.
ctx.format
Pure string formatters for data serialization. These return strings and perform no I/O -- write the result to stdout yourself.
ctx.store
Typed in-memory key-value store for passing data between middleware and handlers. This is the recommended way to communicate state through the middleware chain.
ctx.fail()
Throws a ContextError with a clean user-facing message. No stack trace is shown in production. The process exits with the specified exit code.
ctx.meta
Deeply readonly CLI metadata.
dirs contains local (resolved relative to the project root) and global (resolved relative to ~). Both default to .<cli-name>.
ctx.auth
Auth context decorated by the auth() middleware from maltty/auth. Only present when the auth middleware is registered.
See Authentication for the full auth system reference.
Module Augmentation
maltty exposes empty interfaces that consumers extend via TypeScript declaration merging. This adds project-wide type safety without threading generics through every handler.
For ConfigRegistry, use the ConfigType utility to derive the type from your Zod schema (see ctx.config above). Note that config augmentation targets maltty/config, while other interfaces target maltty:
Context in screen commands
Screen commands defined with screen() do not receive a CommandContext object. Instead, parsed args are passed directly as props to the React component, and runtime values are accessed via hooks:
See Screens for details.