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.

Preview
Close

Transcript from the "Processing CLI Arguments" Lesson

[00:00:00]
>> 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.

[00:00:24]
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?

[00:00:43]
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.

[00:01:03]
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.

[00:01:20]
It has something like content, which would be the notes. And then we could do something like an id, which is just something like Date.now, 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.

[00:01:49]
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.

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

[00:02:30]
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.

[00:02:44]
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.

[00:03:05]
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.

[00:03:20]
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.

[00:03:38]
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