Reporting Usage Errors With an Exit Code

Run without its two arguments, Dev’s Python tool prints a usage line to sys.stderr and calls sys.exit(1). Your digest.js has no such check yet, so a bad run still reports success, and the job that mails the season file to Halden Heating would think the digest worked. One line stands in for the digest’s work:

Language: JavaScript Filename: digest.js
const args = process.argv.slice(2);
const [path, limit] = args;
const threshold = Number(limit);

console.log(`Checking ${path} against ${threshold}`);

Where Python’s sys.argv[1] raises IndexError and unpacking raises ValueError, the port runs on:

Language: Shell
$ node digest.js; echo $?
Checking undefined against NaN
0

Destructuring fills missing items with undefined, and Number(undefined) is NaN, yet the exit code is still 0.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.