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

The "Notes App Commands" 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 sets up the foundation for CLI commands such as new, all, find, remove, web, and clean.

Preview
Close

Transcript from the "Notes App Commands" Lesson

[00:00:00]
>> Now, I just wanna set up the basic commands. They're not gonna do anything right now, we're gonna do that later, and actually have the commands do things. But for now, we'll just set up the commands that don't actually do anything. So I have them all here, but I'm just gonna write them out.

[00:00:12]
So let's think about all the stuff we wanna do for a note. I'm gonna get rid of this command here, we don't need the curl one. Instead I wanna make a command that you can use for creating a new note. So I'll call it new like this. And it's going to take an argument called the note or you can call it the content, you can call whatever you want but the actual note that you wanna pass.

[00:00:37]
So this will allow me to go to say note new and then pass in something like this without the tags, right? So we got the note, I'm going to describe it, I'm gonna say create a new note like this, Oops, right, don't do that, thank you. And then there's basically two functions that come after this.

[00:01:08]
So it's four arguments. So the first argument is the command you want, followed by any arguments for that. The second one is the description for it, the third one is optional. We won't use this for everyone, but it's great for formatting things. In this third function right here, I'm gonna get another instance of yargs, which is not the same yargs as the one that we imported.

[00:01:30]
This one is scoped to this command, so you have to use it. You can call it the same, it's fine to shadow it. And I'm gonna return yargs.positional( 'note') like this, and I'm gonna say its type is a string, its description, is the note to create, or the content.

[00:02:01]
You can call it wherever you want, the content of the note to create and, You can put a default here too if you want but we don't want a default, so we got that. And then now here I'll get argv and yeah we're not gonna do anything with it right now, we'll do that later but for now we could just log it and we could just say argv.note, and we should be able to get something.

[00:02:34]
So this is basically saying get the note argument, ensure its type string. Here's the description for if somebody asked for help, and then this is like okay, someone called that command, what do you wanna do? Right now we're just logging in, eventually we'll actually add a new note somewhere, but for now we are just going to log it.

[00:02:55]
So to test that out, I can run that. And you can see it does return back the content that I put clean my room because that's what I logged here. All right, just so I can just tag this just so we can see hello. So this is a way better way to build a CLI than what we were gonna do, which would have been a bunch of if statements and regexs everywhere, so this is much better.

[00:03:33]
Any questions on this or how to like create the command? Cool, well then we just have the other ones that we can go through. Actually, I do wanna add one more for a note, I wanna add an option here. So, it's an option or options, there we go.

[00:03:52]
So I wanna add an option here, I'm gonna call it tags. So I wanna be able to tag our notes with tags so we can search by them if we want to or however we wanna do it. And that one's pretty easy, you just add an option after a command and you pass it an object.

[00:04:11]
Like this, and I can set an alias to t, I'll show you what that means in a minute. I can enforce the type to be string in this case, and then put a description if someone asks for help, It'll tell you what the description is. So now we have tags, and I'm able to access those tags on the argv as well.

[00:04:33]
So what does that actually look like? Well now if I go here, I can do something like --tags like this and I can parse in a string. In this case, I can separate them by, I'll say, for me, I'll do commas. It doesn't matter, so I can say this is work, this is serious.

[00:04:53]
This is whatever these are my tags that I wanna add. That's what an option is or a flag is or an option. And when we said alias t, that just means we could do like a shorthand like that or maybe T or lowercase. This means you could just one dash t or you could do like two dashes and you can spell the whole thing out, every CLI does that.

[00:05:18]
They have an a list for all their flags. I'm not cool enough, so I'll just do the longhand one cuz when you see like some command with like -t, -a, -m, -v, I'm like, what is this? Just spell it out, I don't know any of this shit is, so I'm just gonna spell myself out.

[00:05:41]
Cool, all right, and then yeah from there, we have all these other commands. We can just copy these in here if you want, but basically we have one for getting a new note, one for getting all the notes, one for finding a note by a search criteria. One for removing a note by an id, got this crazy one called Web, which we'll launch a website to see all of our notes.

[00:06:06]
And then the last one is just like, delete everything, right? So we're gonna add all of those and I'm just gonna just paste them in cuz they don't do anything right now. You can grab them off the notes website and boom. So now we have all of those.

[00:06:25]
Like I said, they don't really do anything, I'm not really introducing anything new here. The only I guess that's slightly new than what I've just talked about is just one for port. This one has square brackets around it other than the angle brackets, that just means it's optional.

[00:06:42]
Angle brackets means it's required, square brackets means this is an option cuz I have a default here if you don't pass it. That's all that means. So to verify that all that is working I can just say note --help. And there you go, there's all my commands.

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