Introduction to Node.js, v3

Processing CLI Arguments

Scott Moss

Scott Moss

Superfilter AI
Introduction to Node.js, v3

Check out a free preview of the full Introduction to Node.js, v3 course

The "Processing CLI Arguments" Lesson is part of the full, Introduction to Node.js, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Scott walks through the process of building out the logic for a custom note-taking CLI, enabling it to accept input from the terminal as a new note.


Transcript from the "Processing CLI Arguments" Lesson

>> Hopefully everybody had enough time to digest what it means and is to create a simple CLI. But right now, our CLI doesn't really do anything. It just logs hello world. So let's start building out that logic so we can actually add new notes from our CLI, and then we'll figure it out from there, how to save them, how to retrieve them, edit them, whatever.

First thing is, let's just create a, I don't know, a data model, I guess, for what a note is. So we want to be able to do something like this. Why don't we go to the terminal and type in, this is my new note like this. I wanna be able to capture that in our program, right?

So knowing what we know about CLIs and arguments to CLIs, we can do something like this. So we can say the note would be process.argv, which we know is an array. And remember the first two things in the array is always gonna be the environment. So the point to note, the second thing will be the file, which in this case will be this file.

The third thing is whatever we pass after the command. So this will be the third thing in the array, which will be index 2, all right? So now we have our note. And then let's just create a data model, I guess, of newNote. It's gonna be an object.

It has something like content, which would be the notes. And then we could do something like an id, which is just something like, which should be the ID. I think we just logged that just to make sure it's doing what we thought it was gonna do. All right, so nothing complicated here, taking in an argument from our environment.

Putting that in an object, putting another property owner with the ID, and then just logging it. So let's run this. And you should get something like this. You get back an object that has the content and then an ID, All right? Pretty cool. So we're able to pass in that argument and use it.

So the reason I had to put quotes around those, anybody know? Why did I make this string in the terminal?
>> That would be five separate arguments [INAUDIBLE]?
>> Exactly, it'd be five separate arguments because each argument is separated by space. So if I removed the quotes it would think each one of these words is an argument.

Which is not what I want. So I have to wrap this in a string so I can say this whole block is one argument. Cool, okay, so, so far, this is the beginning stages of it, but you could probably use your imagination and see where we can go with this.

But right now it's really not that functional, we can't do things like, what if I wanted to come in here and add a tag to this, or some tags? Or what if I want to retrieve all the notes or search notes by content? It's really not that useful, one, because we're not even saving this anywhere.

Where are we saving this? It just gets executed and then it goes away. Once this program executes and it stops, the memory is wiped. It's not storing it somewhere. So it's saying that if you ran a website, shut it down and came back to it, the memory is wiped.

So this note isn't here anywhere. So we wanna be able to persist it somewhere so we can recall them and do things on those notes. So right now it's not that helpful. But we figured out the IO. We can input some content and have it be a note, and then we can see that it's there in our program.

And now that it's there, we can actually do things with it. But doing all that by hand is actually quite tedious. So instead, we're gonna rely on some modules that node has for us, and then also some third-party ones that we'll download. So we'll get there so we're not parsing all these things by hand which can be a job on its own.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now