Skip to main content

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

name
string
required
The name of the CLI application. Used as the primary command name.
definition
create.Options
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.

Return Type

Cli
Cli<commands, vars, env>
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

name
string
The name of the CLI application.
description
string | undefined
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:
name
string
required
The command name. Used to invoke the command.
definition
CommandDefinition
The command definition. See Commands for details.
Mounting a sub-CLI:
command<const name extends string>(
  cli: Cli & { name: name }
): Cli<...>
Parameters:
cli
Cli
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
serve.Options
Options for serve, primarily used for testing.
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:
handler
MiddlewareHandler
The middleware handler function. See Middleware for details.
Example:
cli.use(async (c, next) => {
  console.log('Before command')
  await next()
  console.log('After command')
})

Build docs developers (and LLMs) love