This course has been updated! We now recommend you take the Introduction to Node.js, v3 course.

Check out a free preview of the full Introduction to Node.js course:
The "commander Module for Building CLIs" Lesson is part of the full, Introduction to Node.js course featured in this preview video. Here's what you'd learn in this lesson:

Scott shows the "hack" around the process module called commander that was used to create commands, have aliases, and have a full CLI.

Get Unlimited Access Now

Transcript from the "commander Module for Building CLIs" Lesson

>> Scott Moss: Cool, so I'll just go briefly into this file. This is using one of many CLI program tools out there called commander. Commander's pretty legit, but basically it is a hackaround process. So remember I said I almost never write an app that doesn't use process? Even though you didn't use process directly, the Node module that you installed did use it.

[00:00:22] Because process has got so much good information on it, the one that's important here is this argv. If you would've logged this out, it's just an array of all the arguments that you passed in, right? So if I were to do something like this,
>> Scott Moss: So argv, I would just log that, and then rerun this command, and pass in some more stuff here.

[00:00:42] Actually, it might break cuz, there it is. So yeah, you can see this array is all the arguments. The first one is always gonna be the path to the executable node itself. That's always gonna be the first argument in the array. The second argument is always gonna be the path to the file that's currently executing.

[00:00:58] And then everything after that are gonna be whatever you passed in in that order. So you can see, I passed in new, then I passed in thing, then I passed in hello, then I passed in this stuff. That's what happened here. So this is how you can capture arguments from a CLI, and the commander module just did all that for you.

[00:01:18] It knew what it was doing, and it allowed you to create commands and have aliases with descriptions. And it helps you build a full CLI, this is just one of many. This is why I said it's not important that you don't need to know what this is because you didn't.

[00:01:30] You didn't need to know what this was in order to do your job. But that's what this does, and it allows you to create CLIs with flags and arguments and then execute some type of action when those arguments are ran. And that's what this command does, there's tons of these out there that do this.

[00:01:47] Inquirer is basically, it's the part of the CLI that had the prompts that was asking you questions and stuff. That's what inquirer does. So if you were to look in this other file, you might see something called prompts, and it's an array of prompts. That's basically what that does.

[00:02:03] It allows you to have that interactive CLI experience. So if you can think of it, you can do it in Node. I mean, when I was teaching people how to code, I used to have this script that hooked into the testing framework. And if they passed every single test, a unicorn would pop up with glitter and stuff.

[00:02:21] Just [LAUGH] if you can think of it, you can make it. There's literally anything you can do, so it's pretty cool. Or I would have like if somebody got all the tests wrong, or if they got them all right on the first time, it'll load up Matrix and just sprinkle on their screen the Matrix stuff.

[00:02:36] Cuz they were Neo and they got it right the first time. So it was just a lot of stuff like that.