Node.js Command-line Arguments
Command-line arguments arrive as strings. Parse the ones your program accepts and reject ambiguous values before using them as quantities or paths.
How it works
process.argv includes the executable and script path before the application arguments. Use a small parser whose contract you can test without spawning another terminal. Number conversion alone accepts representations you may not want, such as an empty string becoming zero. Here a decimal digit rule is checked before conversion, followed by a safe integer and range rule. The complete script runs its own deterministic fixture; an interactive tool could pass process.argv.slice(2) instead.
Run the code
Run this example locally with Node.js. The exact sources are reviewed on Node 22.15.1 and Node 24.22.1 unless a specialised lab states otherwise. Save the complete source as terminal-input.mjs in a separate empty exercises folder and run node terminal-input.mjs. There is no Node.js browser runner on Bhylu; the plain JavaScript playground does not provide these filesystem, server or process APIs.
Download the complete source. Ordinary Node scripts need no Apache, PHP or MySQL service. File examples own their temporary folders; network fixtures use loopback and available temporary ports.
Example
function parseQuantity(args) {
if (args.length !== 1 || !/^[1-9][0-9]*$/.test(args[0]))
throw new Error('Use one positive quantity');
const value = Number(args[0]);
if (!Number.isSafeInteger(value) || value > 20)
throw new Error('Quantity out of range');
return value;
}
console.log(parseQuantity(['3']));
try {
parseQuantity(['3kits']);
} catch (error) {
console.log(error.message);
}
Expected result
3 Use one positive quantity
The parser accepts one clear quantity and rejects a token with a unit suffix. The same helper can be tested separately from process startup, which keeps argument errors from being confused with application failures.
Use the Node.js API reference for the precise version contract.