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.The command-line arguments to parse (e.g.,
process.argv.slice(2))Configuration for parsing, including schemas for args and options
An object containing parsed
args and optionsUsage: 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.The Zod schema defining expected environment variables
The environment variable source. Defaults to
process.env (Node.js) or Deno.env (Deno).The parsed and validated environment variables
Usage: Basic Environment Parsing
Usage: Custom Environment Source
Usage: Boolean Coercion
Types
parse.Options
Configuration options for theparse() function.
Zod schema for positional arguments. Keys define the order of arguments.
Zod schema for named options/flags.
Map of option names to single-character aliases (e.g.,
{ verbose: 'v' }).parse.ReturnType
The return type of theparse() function, providing type-safe access to parsed arguments and options.
Parsed positional arguments, typed according to the schema. Empty object if no schema provided.
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

