CLI
Node.js CLI argument and exit contract
A CLI contract specifies accepted arguments, output and exit behavior. Parse only the script arguments, validate before processing, and keep diagnostic output separate from machine-readable results. This fixture demonstrates argument validation using a synthetic argument array; process.argv and process.exitCode have separate API references. Tested on Node.js 22.16.0. This sample accepts one nonnegative decimal integer from 0 to 1000; it models validation, not a live exit-status test. Example output (one line per entry): ["{\"ok\":true,\"limit\":4}", "{\"ok\":false}"].
Formal Syntax & Structure
nodejs syntaxnodejs
node script.mjs [arguments]Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| arguments | string[] | Application arguments. |
Return Value
Process result
Application-defined stdout, stderr and status.
Working Example
Example Snippetnodejs
function parse(args) { if (args.length !== 1 || !/^\d+$/.test(args[0])) return { ok: false }; const limit = Number(args[0]); return Number.isSafeInteger(limit) && limit <= 1000 ? { ok: true, limit } : { ok: false };}console.log(JSON.stringify(parse(['4'])));console.log(JSON.stringify(parse(['oops'])));