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

The "Using the yargs Module" 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 demonstrates the installation and usage of the third-party yargs module to implement a help command for the custom note-taking CLI.


Transcript from the "Using the yargs Module" Lesson

>> We need to spice up our CLI is kind of garbage right now. I'm not gonna lie, it doesn't really do anything. So, like I said, I don't want to, I'm lazy. I don't really want to do all that stuff by hand. So I'm gonna use somebody else's code.

Somebody smarter than me, made this thing called yards. Which, let me go over there, GitHub. Sometimes you'll go to the NPM and there's like nothing here. And that's just because they set up their readme a little different. So, go over there, GitHub, and I'm going to use this thing.

This thing, basically, if you read its description, yargs helps you build interactive command line tools by parsing arguments and generating an elegant user interface. Yeah, sign me up for that. Take my money. That's exactly what I want. I don't wanna do this from scratch, so we're gonna use this.

And this is really gonna be your job. It's just coming here, reading documentation on somebody else's code, and understanding if you can copy and paste it or not, for the most part. And this is gonna get you all the way to senior level. And even beyond that. [LAUGH].

That's pretty much what it comes down to. It's just getting really good at this. We're gonna install this and use it. You can see that they have an example here that uses Require. I already said we're not using Require, so we're not going to even look at that.

Here we are, ESM, ECMAScript modules. This is where we want to be. And we want to do something like this. So I'm going to just copy this code here inside of our index dot js. Actually, I'm going to delete this folder that I have, because I don't need it.

Oops, not that one. Go back. Let's just clean up this index.js file here. I'm actually just gonna delete everything in it because I don't want it. Before I start doing the yarg stuff, I wanna organize some modules a little differently. I want the index js file to be light.

I don't want to do anything. I just wanted to import our app and that's it. I don't want any code in here at all. So I'm gonna make a new folder called source. So in our source folder, I'm gonna make a new command or new file called command.js.

And here is where I'll write our code for our CLI. So I'm just gonna paste in that yargs stuff that I copied, which if I go through it, it's just importing yargs, which we have to install, we'll do that in a minute. This other thing that we don't know, we'll talk about that.

And it just like, it does some stuff here. So let's just save this. And all I'm going to do is go to our index file on the root. I'm just going to import this file. So I say import. Source. Command.js, that's it. So if you're just gonna import a file that doesn't export anything, you don't need from, you can say import and then the path.

And that's just going to execute the file, all right? This command file isn't exporting anything. There's no export in here, anywhere. I just want this file to be executed. Whenever this file's executed, and we know this file's executed when someone types in note in the terminal. So that means when someone types in note it'll execute this file which will then execute this file.

So it seems kind of redundant but I prefer my CLI just to have a one line import at the top for the index. And everything else is in the source folder broken up. We also don't need to put this hash being at the top of every file, we only have to put at the top of the entry file.

Right? So now you're thinking like modules you have this you can think of modules like a tree. Right? And you have an entry into your app, which is typically one file. Most apps have one entry. Our entry file is index.js. So we only have to put this comment at the top of our entry file, because it's in the same process as every other file that will import from there.

It's a treat. So we're good there. And then now we have this Yargs thing. So, let's walk through what's happening here. Basically what this is saying is we know what process .argv is. It's an array of all the things. And the second thing in the array is any argument that comes after it.

This hide bin function just removes those two things from the beginning of the array. That's all it does. That's literally it. And then, we can register a command. So a command would be something like this, right? So note would be our CLI. A command would be something that comes after it, right?

So in the case of npm, npm is a CLI, install is the command. Right, so npm has an install command. Note right now in the case of my code has a curl command, so let's just try that out. So now I'm saying if I type note curl. This right here is an argument to the curl command.

So it's something you can pass it. In this case, it's saying, fetch the contents of a URL and it's just logging it. I should be able to say note and then curl, and then anything after this is going to be treated as a URL. So, I guess it doesn't matter what I put here, it's not really doing anything.

If I run this, it's going to break. Anybody know why this broke?
>> You didn't install yargs?
>> Didn't install yargs. And you would know that because it would say that, right? It would say, cannot find package yargs. That typically means you didn't install it. All right, so let's do that.

Npm install yargs. Cool. Now, let's run it. Oops, I ran install again. Now, when I curl it, it's doing what it's supposed to do. It's logging argv. You can see it looks way different than when we logged argv the first time. That's because yargs is turning it into an object for us.

It's formatting it. This is why this package is really cool. It makes us to where we don't have to go parse some array to do all this crazy stuff, and instead it'll parse it for us and put it in this object. It'll even like typecast things for us if we ask it to.

Like this should be a string. This should be a number. This should be a boolean. It'll do that for us, too. It's really optimal. And this is why other people's code is cool. We don't have to do anything. Okay, so we log into argv. This demand command thing just means you cannot run note without at least using one command, so you can't just run note by itself.

Pretty sure it'll just error out if you try. Exactly. It said you need at least one command and then parse just means go. Just okay, now execute. That's all that means, right? The other thing we get for free with yarg is that we can type in note, help dash dash help, and we get a list of all the commands and flags that we can use for free.

We didn't have to log that out ourselves. And we don't have to maintain that. Any new commands that we add automatically gets added to this help. And if you know anything about any CLI, every CLI has a dash dash help. Every CLI that you'll use will have this.

This is like the unspoken rule of CLIs, that you must add a dash dash help flag. No matter what. So we get that for free. But just a few lines of code.

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