Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Reading Command-Line Arguments

Reading Command-Line Arguments

In the welcome video, the JavaScript port of Dev’s first lines printed the path of digest.js and NaN, with no error. In this lesson, you found out why, and read Dev’s arguments the JavaScript way.

Dev’s Python is trimmed to its argument lines: the week’s log is sys.argv[1] and the limit sys.argv[2], and any reading over the limit gets flagged. A line-by-line port swaps sys.argv for process.argv, a global, and keeps the same indexes. float() becomes Number(), which also turns a string into a number:

Language: JavaScript
const path = process.argv[1];
const threshold = Number(process.argv[2]);

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

Run with Dev’s arguments, it prints the welcome video’s line again, with no error:

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

00:00 In the welcome video, the JavaScript port of Dev’s first lines printed the path of digest.js and NaN, with no error. In this lesson, you’ll find out why, and read Dev’s arguments the JavaScript way.

00:14 Dev’s Python is trimmed to its argument lines: the week’s log is index one of sys.argv, and the limit index two. Any reading over the limit gets flagged.

00:27 A line-by-line port swaps sys.argv for process.argv, a global, and keeps the same indexes. float() becomes Number(), which also turns a string into a number.

00:40 Run it with Dev’s arguments, and you get the welcome video’s line again.

00:45 The path of digest.js stands where the log’s name should be. For text like a file name, Number() gives NaN, where float() would raise an error.

00:55 So not even Thursday’s 78.0, the week’s peak, is over the limit, and nothing warns Dev.

01:03 To see why, log the whole list in both files. Python prints three items. How many items does JavaScript’s process.argv hold for the same command?

01:15 Your turn: pause the video and predict.

01:23 Four items. The program you ran is node, and digest.js is its first argument, so process.argv starts with the node program, where Python’s leaves python out.

01:38 Every argument sits one place along: index one is the script’s path, and index two the log’s name. Dev’s full script also checks for three items in sys.argv.

01:51 Ported as it is, that check is off by one too. On a correct run, process.argv has four items, so the usage line would print every time.

02:01 The JavaScript way is to take the user’s arguments once, at the top, with .slice(2): Python’s sys.argv from one onward. The start is two, not one, because of the node program’s extra item.

02:15 Destructuring unpacks args into path and limit, by position, and Number() converts the limit, as float() does.

02:23 Run both again: the log’s name, then 75. Arguments are strings, as in Python, so the Number() call stays.

02:34 That’s the JavaScript way: take the user’s arguments once, with .slice(2), unpack them into names, and convert the numbers. Next up: Reporting Usage Errors With an Exit Code, a text lesson.

02:47 You’ll send the usage line to standard error, and set an exit code other tools can read.

Become a Member to join the conversation.