Skip to main content

CommandDefinition

Defines a command’s schema, behavior, and documentation. Commands are registered with cli.command().

Type Signature

Properties

args
z.ZodObject<any>
Zod schema for positional arguments. Arguments are parsed in the order they are defined in the schema.
description
string
A short description of what the command does. Shown in help text, agent discovery, and skill manifests.
env
z.ZodObject<any>
Zod schema for command-specific environment variables. Merged with CLI-level env schema. Keys are the variable names (e.g. DEPLOY_TOKEN).
options
z.ZodObject<any>
Zod schema for named options/flags. Options are passed as --key value or --flag for booleans.
output
z.ZodType
Zod schema for the return value. Used for output validation and type inference in the command registry.
alias
Record<string, string>
Map of option names to single-char aliases.
examples
Example[]
Usage examples for this command. Shown in help output with --help.
format
'toon' | 'json' | 'yaml' | 'md'
default:"'toon'"
Default output format for this command. Overrides CLI-level format. Can still be overridden by --format or --json flags.
outputPolicy
'all' | 'agent-only'
default:"'all'"
Controls when output data is displayed:
  • 'all' — displays to both humans and agents
  • 'agent-only' — suppresses data output in human/TTY mode while still returning it to agents
usage
Usage[]
Alternative usage patterns shown in help output. Useful for documenting multiple invocation styles.
middleware
MiddlewareHandler[]
Command-specific middleware that runs only for this command. Runs after CLI-level and group-level middleware.
run
(context: Context) => InferReturn<output>
required
The command handler function. Receives a context object with parsed arguments, options, environment variables, and utility functions.Can return:
  • A plain value (synchronous)
  • A Promise (asynchronous)
  • An AsyncGenerator (streaming)
See Context Type below for details.

Context Type

The context object passed to the run handler. Contains parsed inputs, CLI metadata, and control flow functions.

Properties

agent
boolean
Whether the consumer is an agent (stdout is not a TTY). Use this to customize output for different audiences.
args
InferOutput<args>
Positional arguments parsed according to the args schema. Fully typed based on the Zod schema.
name
string
The CLI name (not the command name).
env
InferOutput<env>
Parsed environment variables according to the env schema. Includes both CLI-level and command-level env vars.
options
InferOutput<options>
Named options/flags parsed according to the options schema. Fully typed based on the Zod schema.
var
InferVars<vars>
Variables set by middleware. Typed according to the CLI’s vars schema.
ok
(data: InferReturn<output>, meta?: { cta?: CtaBlock }) => never
Return a success result with optional metadata. Calling this function short-circuits execution and immediately returns.
error
(options: ErrorOptions) => never
Return an error result with optional CTAs. Calling this function short-circuits execution, exits with code 1, and displays the error.
version
string | undefined
The CLI version string, if provided.

Command Registration Patterns

Basic Command

Register a simple command with args and options:

Async Command

Commands can be async and use await:

Streaming Command

Return an AsyncGenerator to stream results:

Command with Environment Variables

Access typed environment variables:

Command with Aliases

Provide short aliases for options:

Command Groups

Mount sub-CLIs as command groups:

Error Handling

Use context methods for structured error handling:

Using Middleware Variables

Access variables set by middleware: