Cli.create()
Creates a new CLI application. Can be used to create a single-command CLI with a root handler, or a router CLI that registers subcommands.
Signature
function create <
const args extends z . ZodObject < any > | undefined = undefined ,
const env extends z . ZodObject < any > | undefined = undefined ,
const opts extends z . ZodObject < any > | undefined = undefined ,
const output extends z . ZodType | undefined = undefined ,
const vars extends z . ZodObject < any > | undefined = undefined ,
>(
name : string ,
definition ?: create . Options < args , env , opts , output , vars >
) : Cli <{}, vars , env >
Parameters
The name of the CLI application. Used as the primary command name.
Configuration options for the CLI. Omit run for a router CLI that only registers subcommands, or include run to create a leaf CLI with a root command handler. Zod schema for positional arguments. Keys define argument names, schemas define types and validation.
A short description of what the CLI does. Shown in help text and agent discovery.
Zod schema for environment variables. Keys are the variable names (e.g. NPM_TOKEN). Parsed before middleware runs.
Zod schema for named options/flags. Keys define flag names, schemas define types and defaults.
Zod schema for the return value. Used for output validation and type inference.
Zod schema for middleware variables. Keys define variable names, schemas define types and defaults.
Map of option names to single-char aliases (e.g. { verbose: 'v' }).
Alternative binary names for this CLI (e.g. shorter aliases in package.json bin). Shell completions are registered for all names.
Usage examples for this command. Shown in help output.
format
'toon' | 'json' | 'yaml' | 'md'
Default output format. Can 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
Inherited by child commands when set on a group or root CLI. run
(context) => InferReturn<output> | Promise<InferReturn<output>> | AsyncGenerator
The root command handler. When provided, creates a leaf CLI with a root command. When omitted, creates a router CLI that only registers subcommands. Whether the consumer is an agent (stdout is not a TTY).
Positional arguments parsed according to the args schema.
Parsed environment variables according to the env schema.
Named options/flags parsed according to the options schema.
Variables set by middleware. Typed according to the vars schema.
Return a success result with optional metadata (e.g. CTAs). Calling this short-circuits execution.
Return an error result with optional CTAs. Calling this short-circuits execution.
Alternative usage patterns shown in help output.
The CLI version string. Displayed with --version flag.
Options for the built-in mcp add command. Target specific agents by default (e.g. ['claude-code', 'cursor']).
Override the command agents will run to start the MCP server. Auto-detected if omitted.
Options for the built-in skills add command. Working directory for resolving include globs. Pass import.meta.dirname when running from a bin entry. Defaults to process.cwd().
Default grouping depth for skill files. Overridden by --depth.
Glob patterns for directories containing SKILL.md files to include (e.g. "skills/*", "my-skill").
Example prompts shown after sync to help users get started.
Return Type
A CLI application instance. Also used as a command group when mounted on a parent CLI.
Examples
Create a simple router CLI
import { Cli } from 'incur'
const cli = Cli . create ( 'my-cli' , {
version: '1.0.0' ,
description: 'My awesome CLI'
})
cli . command ( 'greet' , {
args: z . object ({ name: z . string () }),
run ( c ) {
return { message: `Hello ${ c . args . name } !` }
}
})
cli . serve ()
Create a CLI with root command
const cli = Cli . create ( 'my-cli' , {
version: '1.0.0' ,
args: z . object ({ path: z . string () }),
run ( c ) {
return { path: c . args . path }
}
})
Create CLI with environment variables
const cli = Cli . create ( 'deploy' , {
env: z . object ({
API_TOKEN: z . string (),
API_URL: z . string (). default ( 'https://api.example.com' )
}),
run ( c ) {
// c.env.API_TOKEN is typed and validated
return { url: c . env . API_URL }
}
})
Cli Type
The CLI instance returned by Cli.create(). Also used as a command group when mounted on a parent CLI.
Properties
The name of the CLI application.
A short description of the CLI.
vars
z.ZodObject<any> | undefined
The vars schema, if declared. Use typeof cli.vars with middleware<vars, env>() for typed middleware.
env
z.ZodObject<any> | undefined
The env schema, if declared. Use typeof cli.env with middleware<vars, env>() for typed middleware.
Methods
command()
Registers a root command or mounts a sub-CLI as a command group. Returns the CLI instance for chaining.
command <
const name extends string,
const args extends z.ZodObject<any> | undefined = undefined ,
const cmdEnv extends z.ZodObject<any> | undefined = undefined ,
const options extends z.ZodObject<any> | undefined = undefined ,
const output extends z.ZodType | undefined = undefined ,
>(
name : name ,
definition : CommandDefinition < args , cmdEnv , options , output , vars , env >
): Cli <...>
Parameters:
The command name. Used to invoke the command.
The command definition. See Commands for details.
Mounting a sub-CLI:
command < const name extends string>(
cli : Cli & { name : name }
): Cli <...>
Parameters:
A sub-CLI instance to mount as a command group. All subcommands will be namespaced under this CLI’s name.
Examples:
// Register a command
cli . command ( 'greet' , {
args: z . object ({ name: z . string () }),
run ( c ) {
return { message: `Hello ${ c . args . name } !` }
}
})
// Mount a sub-CLI as a command group
const pr = Cli . create ( 'pr' , { description: 'PR management' })
pr . command ( 'list' , {
run () {
return { items: [] }
}
})
cli . command ( pr )
// Now available as: my-cli pr list
serve()
Parses argv, runs the matched command, and writes the output envelope to stdout.
serve ( argv ?: string [], options ?: serve . Options ): Promise < void >
Parameters:
argv
string[]
default: "process.argv.slice(2)"
Command-line arguments to parse. Defaults to process arguments.
Options for serve, primarily used for testing. env
Record<string, string | undefined>
Override environment variable source. Defaults to process.env.
Override exit handler. Defaults to process.exit.
Override stdout writer. Defaults to process.stdout.write.
Example:
// In your bin entry file
# !/ usr / bin / env node
import { cli } from './cli.js'
cli . serve ()
use()
Registers middleware that runs around every command.
use ( handler : MiddlewareHandler < vars , env > ): Cli < commands , vars , env >
Parameters:
The middleware handler function. See Middleware for details.
Example:
cli . use ( async ( c , next ) => {
console . log ( 'Before command' )
await next ()
console . log ( 'After command' )
})