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 two command-line arguments the JavaScript way.

Both files are trimmed to their argument lines for now, with one line standing in for the digest’s work. Dev runs python digest.py logs/week-44.log 75, and any reading over the limit, 75, gets flagged. Python’s sys.argv holds the words he typed after python, starting with the script, so the week’s log is at index 1 and the limit at index 2:

Language: Python
import sys

def main():
    path = sys.argv[1]
    threshold = float(sys.argv[2])

    print(f"Checking {path} against {threshold}")

if __name__ == "__main__":
    main()

It prints Checking logs/week-44.log against 75.0. A line-by-line port swaps sys.argv for process.argv and keeps the same indexes. Node’s process object is a global, so nothing is imported. float() becomes Number(), which also turns a string into a number:

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:02 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:17 Both files are trimmed to their argument lines, with one line standing in for the digest’s work. Python’s sys.argv holds the words Dev typed after python, starting with the script.

00:30 So index one is the week’s log, and index two the limit: any reading over it gets flagged. Run it, and Python prints the log’s name and the limit. A line-by-line port swaps sys.argv for process.argv, and keeps the same indexes.

00:50 Node’s process object is a global, so nothing is imported. float() becomes Number(), which also turns a string into a number.

01:00 The f-string becomes a template literal.

01:04 With no main() function or guard, the entry file’s top level is the program.

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

01:16 The path of digest.js stands where the log’s name should be. For text like a file name, Number() gives NaN, the not-a-number value Python also has, where float() would raise an error.

01:29 With NaN as the limit, even Wednesday’s 76.5 isn’t over it, and neither is Thursday’s 78.0. NaN is never greater or less than any number, as in Python.

01:42 So no reading is ever over the limit, and nothing warns Dev.

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

02:03 Your turn: pause the video and predict.

02:11 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.

02:25 The script comes second, as a full path, and each argument sits one place along. Index two holds the log’s name, so Number() gives NaN for the limit.

02:37 Dev’s full script also checks the count first: if sys.argv doesn’t hold three items, it prints how to use the tool. Ported as it is, that check is off by one too.

02:50 On a correct run, process.argv has four items, so the usage line would print every time. What the script should do with a wrong count is the next lesson’s job.

03:04 You could add one to every index, and to the count. The JavaScript way is to take the user’s arguments once, at the top, so nothing after that line has to skip node and the script.

03:17 The .slice() method copies an array from the index you give it to the end. So .slice(2) does what sys.argv[1:] does: it keeps the two strings Dev typed.

03:30 The start is two, not one, because of the node program’s extra item.

03:36 Destructuring unpacks args into path and limit, by position. Number() converts the limit, as float() does in Python.

03:46 Run it again: the log’s name, then seventy-five. JavaScript’s numbers don’t split into int and float, so it prints 75 where Python adds a point zero.

03:57 Arguments are always strings, as in Python: args holds '75' in quotes, which is why the Number() call stays.

04:08 That’s the JavaScript way: take the user’s arguments once, with .slice(2), unpack them into names, and convert the numbers.

04:17 Next up: Reporting Usage Errors With an Exit Code, a text lesson. 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.