cli-args-parser is an expressive and modern TypeScript library for parsing command-line arguments in Node.js environments (requiring Node.js >=18.0.0). Its core differentiator is the integration of Zod for robust, declarative schema validation, ensuring that CLI inputs conform to predefined types and structures. This prevents common errors by providing immediate feedback on invalid arguments or missing required options. Currently stable at version 1.0.6, the library focuses on a straightforward API for defining expected arguments, options, and flags, making it suitable for building well-structured and user-friendly CLI tools. Unlike more opinionated or heavier alternatives, it provides schema-driven validation out-of-the-box, simplifying argument processing and error handling. Release cadence appears stable with incremental 1.x updates.
npm install cli-args-parserVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to define a CLI argument schema using Zod, parse arguments with `cli-args-parser`, and handle validation errors, showcasing basic typed input and flag usage.
Upgrade your Node.js environment to version 18.0.0 or newer. Ensure your project's `package.json` `type` field is set to `module` or use `.mjs` file extensions for ESM files.
Add `zod` to your project's dependencies. Consult Zod's documentation for advanced schema definition and validation patterns.
When defining your `z.object` schema, chain `.strict()` at the end, e.g., `z.object({...}).strict()`.Manually construct detailed help messages or integrate with a separate library for advanced help generation if required for complex CLIs. The `usage`, `description`, `version`, and `examples` fields in the `Parser` constructor are intended for basic display.
Convert your project or the offending file to an ES Module by setting `"type": "module"` in your `package.json` and using `import` statements, or by renaming your file to `.mjs`.
Check the arguments passed to your CLI against the defined Zod schema. Ensure types match (e.g., number for `z.number()`), all required arguments are present, and no extraneous arguments are provided if the schema is strict. The error message usually provides details on which specific validation failed.
Ensure that argument values match their expected Zod types. For numbers, provide numeric values. For booleans, ensure flags are handled correctly (e.g., presence implies `true`, absence `false` for `z.boolean().default(false)`).