Skip to main content

@alt-stack/cli API Documentation

Package: @alt-stack/cli

The CLI package defines typed arguments and options, immutable procedures, hierarchical command routers, a process-independent execution application, and an injected-I/O terminal runner. Zod supplies runtime parsing and output inference; command handlers use @alt-stack/result.

Initialization

initCli<TContext>()

Creates one context-bound definition factory. The default TContext is an empty record. Use the same factory for every router and procedure that will be composed.

InitCliResult<TContext>

The object returned by initCli:

MemberBehavior
router(config, metadata?)builds a validated hierarchical router while preserving literal command paths
combineRouters(...routers)merges routers with distinct root commands and the same base context
procedurestarts an immutable CliProcedureBuilder for TContext
argument(schema, metadata?)declares one positional argument
variadicArgument(elementSchema, metadata?)declares the final zero-or-more positional argument
option(schema, metadata?)declares a value-taking long option and optional short spelling
flag(metadata?)declares a boolean option that defaults to false

The procedure builder supports:

MethodBehavior
.description(text)sets command help text and returns a new builder
.args(descriptors)appends positional descriptors in object insertion order
.options(descriptors)appends value-option or flag descriptors
.use(middleware)appends context-aware middleware and carries its context override to later stages
.command(handler)creates the executable leaf command

An optional positional argument cannot precede a required one, and one variadic argument may appear only at the end. Option keys become kebab-case long names; invalid names, duplicate long/short names, and collisions with help or version throw CliDefinitionError while the tree is defined.

Argument and option descriptors

All descriptor schemas are Zod schemas. Inferred handler fields use Zod output types, so defaults, coercions, transforms, and async parsing are reflected after validation.

ArgumentMetadata<TOptional>

PropertyMeaning
descriptionoptional help text for the positional argument
metavaroptional display name in usage/help output
optionalwhen true, wraps the schema with optional() and permits an omitted token

ArgumentDescriptor<TSchema>

The frozen descriptor returned by argument.

PropertyMeaning
kindthe literal discriminant "argument"
schemaeffective Zod schema, including the optional wrapper when requested
descriptioncopied help description
metavarcopied display name
optionalwhether the CLI grammar permits omission

VariadicArgumentMetadata

PropertyMeaning
descriptionoptional help text for the positional collection
metavaroptional singular display name used before ...

VariadicArgumentDescriptor<TSchema>

The frozen descriptor returned by variadicArgument.

PropertyMeaning
kindthe literal discriminant "variadic-argument"
schemaa Zod array around the supplied element schema
descriptioncopied help description
metavarcopied display name

OptionMetadata

PropertyMeaning
descriptionoptional help text for the option
metavaroptional value label in help output
shortoptional single alphanumeric short name other than reserved h

OptionDescriptor<TSchema>

The frozen descriptor returned by option.

PropertyMeaning
kindthe literal discriminant "option"
schemaZod schema applied to the string value or undefined
descriptioncopied help description
metavarcopied value label
shortcopied one-character short name

FlagMetadata

PropertyMeaning
descriptionoptional help text for the flag
shortoptional single alphanumeric short name other than reserved h

FlagDescriptor<TSchema>

The frozen descriptor returned by flag.

PropertyMeaning
kindthe literal discriminant "flag"
schemaa boolean Zod schema with default false
descriptioncopied help description
shortcopied one-character short name

InferDescriptor<TDescriptor>

Extracts the z.output type from one descriptor's schema.

InferDescriptorMap<TMap>

Maps every descriptor key to its InferDescriptor output. All declared keys exist on normalized command input; an omitted optional option therefore has an explicit undefined value.

Command input and handlers

CommandInput<TArguments, TOptions>

Contains args, the normalized positional descriptor map, and options, the normalized value-option/flag descriptor map.

CommandHandler<TContext, TArguments, TOptions, TValue, TError>

Receives { input, ctx } and returns Result<TValue, TError> synchronously or asynchronously. TError must satisfy ResultError; returning a non-Result value is rejected at runtime.

Middleware

MiddlewareFunction<TContext, TContextOverride>

Receives { ctx, next }. Calling next() preserves the current context object. Calling next({ ctx: override }) shallowly composes the plain-object override for downstream middleware and the handler, and overwrites corresponding fields in the inferred handler context.

Middleware must return the exact result of its one next call. It may instead return err(resultError) before calling next to short-circuit. The continuation is lazy until its promise is consumed by awaiting, returning, or chaining it. Once consumed, downstream effects are not rolled back if middleware later returns a different result. Calling next more than once, replacing its returned result, or reusing a result from another invocation becomes a command-error; an unconsumed continuation cannot run downstream after middleware settlement.

Routers

RouterMetadata

PropertyMeaning
descriptionoptional router-group description rendered in group help

Router keys must be one token beginning with a letter or number and then containing letters, numbers, hyphens, or underscores. Configs must expose commands as own properties on a standard or null-prototype object. A node is another router or a terminal command. Selecting a router group without a child command returns that group's help.

RouterCommandPaths<TRouter>

Extracts the router's executable leaf paths as a string-literal union. A tree containing users.create and root status commands produces "users create" | "status".

Application creation

createCli(options)

Validates the CLI identity and returns a CliApplication. The function does not read process.argv, write streams, or exit the process.

CreateCliOptions<TContext, TCommandPath>

PropertyMeaning
nameone non-empty executable token used in help
versionnon-empty version text returned for root --version
descriptionoptional root help description
routerroot router from the matching initCli<TContext>() factory
createContext(options)creates a plain-object TContext container once after routing and input validation for an executable command

The context container must have a standard or null prototype. Class-based services remain supported as properties of that plain container; returning a class instance as the container produces a command-error before middleware or the handler runs.

CliContextFactoryOptions<TCommandPath>

PropertyMeaning
commandPathinferred executable path such as "users create"
inputnormalized readonly args and options records passed to the selected command

CliApplication<TCommandPath>

MemberMeaning
execute(argv)resolves an explicit CliOutcome for application-only argument tokens

Definition errors may be thrown while constructing the CLI. For a valid definition, routing/validation is represented as usage outcomes and context/middleware/handler failures are represented as command-error outcomes.

Outcomes

CliOutcome<TCommandPath>

The discriminated union of CliExecutedOutcome, CliHelpOutcome, CliVersionOutcome, CliUsageErrorOutcome, and CliCommandErrorOutcome. Narrow on type.

CliExecutedOutcome<TCommandPath>

PropertyMeaning
type"executed"
exitCodeliteral 0
commandPathselected inferred executable path
valuesuccessful handler value, exposed as unknown in v1

CliHelpOutcome

PropertyMeaning
type"help"
exitCodeliteral 0
commandPathroot/group/command path as string tokens
textrendered contextual help

CliVersionOutcome

PropertyMeaning
type"version"
exitCodeliteral 0
textconfigured version string

CliUsageErrorOutcome

PropertyMeaning
type"usage-error"
exitCodeliteral 2
errorstructured CliUsageError
helpnearest relevant router or command help text

CliCommandErrorOutcome<TCommandPath>

PropertyMeaning
type"command-error"
exitCodeliteral 1
commandPathselected inferred executable path
errororiginal schema-execution, context, middleware, handler, or Result error as unknown

Terminal runner

runCli(application, options)

Executes the application, renders its outcome to injected writers, and resolves exit code 0, 1, or 2. It never assigns process.exitCode itself.

CliWriter

MemberMeaning
write(text)accepts rendered text; Node writable streams satisfy this interface

RunCliOptions

PropertyMeaning
argvapplication arguments, normally process.argv.slice(2)
stdoutreceives help, version, and optionally formatted success values
stderrreceives usage and command errors
formatValueoptional successful-value formatter; undefined suppresses output
formatErroroptional command-error formatter; defaults to Error.message or String(error)

Definition and usage errors

CliDefinitionError

new CliDefinitionError(code, message) extends Error and is thrown for an invalid static command tree.

MemberMeaning
constructoraccepts a CliDefinitionErrorCode and human-readable message
codestable definition-error category

CliDefinitionErrorCode

The union "invalid-cli-identity" | "invalid-command-name" | "invalid-argument-definition" | "invalid-option-definition" | "command-conflict".

CliUsageError

new CliUsageError(code, message, commandPath, issues?) extends Error and is returned inside a usage outcome.

MemberMeaning
constructoraccepts the code, message, nearest command path, and optional Zod issues
codestable usage-error category
commandPathtoken path used to select contextual help
issuesoptional Zod issue list for invalid schema input

CliUsageErrorCode

The union "unknown-command" | "unknown-option" | "missing-option-value" | "duplicate-option" | "invalid-input" | "unexpected-argument".

Result re-exports

For command implementations, the package re-exports ok, err, isOk, isErr, TaggedError, Result, ResultError, Ok, and Err from @alt-stack/result. Their complete contracts are documented in the Result API.