OpenViSTAI · Agents · Blockchain · Crypto
Menu

OPENVIST LEARN · FREE LESSONS

CLI arguments and exit status

Make a command repeatable and separate report data from failure messages.

Lesson 21 of 25 · About 25 minutes · Beginner · English

By the end of this lesson

Read arguments, provide help and signal failure with a nonzero status.

Before you start

  • Node.js 24 with a terminal and plain-text editor; the browser-only lesson is labelled separately.
  • Complete lesson 20 or be comfortable with its examples.

Three words to know

argv
The command-line argument array supplied by Node.
stdout
The ordinary output stream, useful for report data.
stderr
A separate output stream for diagnostics.

1. Define a small command interface

The first two process.argv entries describe Node and the script; slice(2) selects arguments for your program. This example needs exactly one whole-number argument. It uses process.exitCode so Node can finish pending output instead of forcing an immediate exit.

lesson.mjsJAVASCRIPT · Node.js · save as lesson.mjs
const args = process.argv.slice(2);
if (args.length === 1 && args[0] === "--help") {
  console.log("Usage: node lesson.mjs PAGES");
} else {
  const raw = args[0] ?? "";
  const pages = Number(raw);
  if (args.length !== 1 || raw.length === 0 || /[^0-9]/u.test(raw) ||
      !Number.isSafeInteger(pages) || pages < 1 || pages > 50) {
    console.error("error: choose 1 to 50 pages");
    process.exitCode = 2;
  } else {
    console.log(`Checking ${pages} pages`);
  }
}
What you should see · example output
Checking 4 pages

2. Test help, success and failure

Run a successful caseTERMINAL · Bash or Windows PowerShell
node lesson.mjs 4
What you should see · example output
Checking 4 pages

Run --help to read usage, apple to fail the grammar, and 0 to fail the range. Both invalid cases write to stderr and exit with status 2. Report data should not be mixed with a friendly error line on stdout if another program expects machine-readable JSON.

3. Inspect the process result

Observe the exit code in BashTERMINAL · Bash
node lesson.mjs 0
printf "exit=%s\n" "$?"
What you should see · example output
An error message, then exit=2

In PowerShell run node lesson.mjs 0 and then $LASTEXITCODE. A shell redirect such as > report.json can create or truncate the output file before Node validates anything. For practice, inspect stdout directly; redirect only to a new disposable filename you intend to create.

YOUR TURN

A small check before you continue

Why does Number(raw) alone not establish the stated input grammar?

Show a suggested answer

It accepts more forms, including an empty string as zero and exponent notation. The command separately enforces nonempty decimal digits and the 1-to-50 range.

Sources and scope

Documentation and examples reviewed 2026-10-18. Provider screens, package versions and output wording can differ. Examples show expected outcomes; they are not a claim that OpenViST connected to your server or verified your setup.