Skip to main content

CommandDefinition

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

Type Signature

Properties

z.ZodObject<any>
Zod schema for positional arguments. Arguments are parsed in the order they are defined in the schema.
string
A short description of what the command does. Shown in help text, agent discovery, and skill manifests.
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).
z.ZodObject<any>
Zod schema for named options/flags. Options are passed as --key value or --flag for booleans.
z.ZodType
Zod schema for the return value. Used for output validation and type inference in the command registry.
Record<string, string>
Map of option names to single-char aliases.
Example[]
Usage examples for this command. Shown in help output with --help.
'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.
'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[]
Alternative usage patterns shown in help output. Useful for documenting multiple invocation styles.
MiddlewareHandler[]
Command-specific middleware that runs only for this command. Runs after CLI-level and group-level middleware.
(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

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