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.string[]
required
The command-line arguments to parse (e.g.,
process.argv.slice(2))parse.Options
Configuration for parsing, including schemas for args and options
parse.ReturnType
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.z.ZodObject<any>
required
The Zod schema defining expected environment variables
Record<string, string | undefined>
The environment variable source. Defaults to
process.env (Node.js) or Deno.env (Deno).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 theparse() function.
z.ZodObject<any> | undefined
Zod schema for positional arguments. Keys define the order of arguments.
z.ZodObject<any> | undefined
Zod schema for named options/flags.
Record<string, string> | undefined
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.
z.output<args> | {}
Parsed positional arguments, typed according to the schema. Empty object if no schema provided.
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

