Registry / http-networking / cli-args-parser

cli-args-parser

JSON →
library1.0.6jsnpmunverified

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-parser
INSTALL
IMPORT
SIG · CLI-ARGS-PARSER
C
cli-args-parser
http-networkingjavascriptv1.0.6
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Parser
import { Parser } from 'cli-args-parser';
const Parser = require('cli-args-parser').Parser;
The library primarily uses ES Modules. Direct CommonJS require() may lead to issues or require specific configuration for interoperability in older Node.js versions.
z
import { z } from 'zod';
import * as z from 'zod';
Zod is a peer dependency used for schema definition. It's typically imported as a named export `z` for convenience.

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.

import { Parser } from 'cli-args-parser'; import { z } from 'zod'; // 1. Define your CLI argument schema using Zod const argsSchema = z.object({ name: z.string().min(1).describe('The name to greet.'), age: z.number().int().positive().optional().describe('The user\'s age.'), verbose: z.boolean().default(false).describe('Enable verbose output.'), output: z.enum(['json', 'text']).default('text').describe('Output format.'), }).strict(); // Use .strict() to disallow unknown arguments // 2. Initialize the parser with your schema and optional metadata const parser = new Parser({ schema: argsSchema, usage: 'Usage: my-cli --name <name> [--age <age>] [--verbose] [--output <format>]', description: 'A simple greeting CLI application with schema validation.', version: '1.0.6', // Make sure to match your package's actual version examples: [ 'my-cli --name Alice --age 30 --verbose', 'my-cli --name Bob --output json' ] }); // 3. Parse command-line arguments (excluding 'node' and script path) try { const parsedArgs = parser.parse(process.argv.slice(2)); if (parsedArgs.output === 'json') { console.log(JSON.stringify(parsedArgs, null, 2)); } else { let greeting = `Hello, ${parsedArgs.name}!`; if (parsedArgs.age) { greeting += ` You are ${parsedArgs.age} years old.`; } if (parsedArgs.verbose) { greeting += ` (Verbose mode active.)`; } console.log(greeting); } } catch (error) { if (error instanceof Error) { console.error(`CLI Error: ${error.message}`); } else { console.error('An unknown error occurred during CLI parsing.'); } process.exit(1); // Exit with a non-zero code on error }
Debug
Known issues
breakingThis library requires Node.js version 18.0.0 or higher. Running in older Node.js environments will result in errors, particularly with ES Module syntax.
fix
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.
affects: >=1.0.0
gotchaThe library relies on 'zod' for schema definition and validation. You must install 'zod' as a direct dependency in your project: `npm install zod` or `yarn add zod`.
fix
Add `zod` to your project's dependencies. Consult Zod's documentation for advanced schema definition and validation patterns.
affects: >=1.0.0
gotchaBy default, Zod schemas are not 'strict' and will allow unknown properties. To ensure your CLI parser rejects arguments not defined in your schema, always apply `.strict()` to your Zod object schema.
fix
When defining your `z.object` schema, chain `.strict()` at the end, e.g., `z.object({...}).strict()`.
affects: >=1.0.0
gotchaUnlike some other CLI parsers (e.g., Commander, Yargs), `cli-args-parser` does not provide extensive built-in functionality for generating complex help text or subcommands automatically beyond what's defined in the `usage` and `description` options. You are responsible for structuring your help output using the provided metadata.
fix
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.
affects: >=1.0.0
Errors
Common errors & fixes
Error [ERR_REQUIRE_ESM]: require() of ES Module ...cli-args-parser/dist/index.js from ... not supported.
Attempting to import `cli-args-parser` using CommonJS `require()` syntax in a CommonJS module, while the library is ESM-only.
fix
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`.
CLI Error: Validation Error: Invalid input
The command-line arguments provided do not match the schema defined using Zod. This could be due to incorrect types, missing required arguments, or unrecognized arguments (if `.strict()` is used).
fix
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.
ZodError: [ { "code": "invalid_type", "expected": "number", "received": "string", "path": [ "age" ], "message": "Expected number, received string" } ]
An argument expected to be a number (e.g., `--age 30`) was provided as a non-numeric string, and Zod's validation failed to coerce or validate the type.
fix
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)`).
Upgrade
Version history
1.0.6latest on npm
Audit
Dependencies
zodrequiredUsed for defining and validating argument schemas. This is a core feature of cli-args-parser.
Agent activity
19 hits · last 30 days
node
16
Amazon
1
OpenAI (training)
1
Resources
cli-args-parser — npm install cli-args-parser · libregistry