Skip to main content
The Parser module provides utilities for parsing command-line arguments (argv) and environment variables against Zod schemas, with automatic type coercion and validation.

Functions

parse

Parses raw argv tokens against Zod schemas for positional arguments and named options.
argv
string[]
required
The command-line arguments to parse (e.g., process.argv.slice(2))
options
parse.Options
Configuration for parsing, including schemas for args and options
return
parse.ReturnType
An object containing parsed args and options

Usage: Basic Parsing

Usage: Flag Aliases

Usage: Stacked Boolean Flags

Usage: Array Options

Usage: Kebab-case to camelCase

Usage: Boolean Negation

Usage: Equals Syntax

parseEnv

Parses environment variables against a Zod schema with automatic type coercion.
schema
z.ZodObject<any>
required
The Zod schema defining expected environment variables
source
Record<string, string | undefined>
The environment variable source. Defaults to process.env (Node.js) or Deno.env (Deno).
return
z.output<env>
The parsed and validated environment variables

Usage: Basic Environment Parsing

Usage: Custom Environment Source

Usage: Boolean Coercion

Types

parse.Options

Configuration options for the parse() function.
args
z.ZodObject<any> | undefined
Zod schema for positional arguments. Keys define the order of arguments.
options
z.ZodObject<any> | undefined
Zod schema for named options/flags.
alias
Record<string, string> | undefined
Map of option names to single-character aliases (e.g., { verbose: 'v' }).

parse.ReturnType

The return type of the parse() function, providing type-safe access to parsed arguments and options.
args
z.output<args> | {}
Parsed positional arguments, typed according to the schema. Empty object if no schema provided.
options
z.output<options> | {}
Parsed named options, typed according to the schema. Empty object if no schema provided.

Error Handling

The parser throws specific error types for different failure scenarios:
  • ParseError: Thrown when argument parsing fails (e.g., unknown flags, missing values, invalid syntax)
  • ValidationError: Thrown when values fail Zod schema validation