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

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

Create a CLI with root command

Create CLI with environment variables

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.
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:
Parameters:
cli
Cli
A sub-CLI instance to mount as a command group. All subcommands will be namespaced under this CLI’s name.
Examples:

serve()

Parses argv, runs the matched command, and writes the output envelope to stdout.
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:

use()

Registers middleware that runs around every command.
Parameters:
handler
MiddlewareHandler
The middleware handler function. See Middleware for details.
Example: