LEARNNode CLI Automation

Node CLI Frameworks

You have to use the argv array of the process object to get arguments from the terminal. Having something set to each argument can get messy really quickly. There are a couple of Node CLI frameworks available to handle just this. Let's take a look at a couple of them.

Commander.js

This is the Node CLI framework the Create React App uses under the hood. You can install it in your CLI project by running the following command on the terminal:

npm install commander

Now you can use it in your project like this:

const { Command } = require('commander');
const program = new Command();
program.version('0.0.1');
program
.option('-d, --debug', 'output extra debugging')
.option('-s, --small', 'small pizza size')
.option('-p, --pizza-type <type>', 'flavour of pizza');

If you are interested in reading its detailed documentation then you can find it here.

Sade

This is another open-source package that you can use in your Node CLI to handle your CLI arguments. The installation is quite simple. Just run the following command inside your project's terminal:

npm install --save sade

Now you can use it to define aliases to different flags. You can read more about it here.

Meow

It is another good option that you can use to handle your CLI arguments. It is developed by Sindre Sorhus, the same person who developed the chalk package.

I personally like this package as it is quite simple to use. You can easily install it with the npm in your project. I am going to write more about this package in detail in later articles. For now, you can read its documentation here.

Wrapping Up

Other than these three packages, there are many other open-source packages available just to help you handle your CLI arguments. For instance, cac, ink, gluegum, etc. You can use any of them in your CLI project.

Posted by Saad (It's a work in progress: Needs copy editing review by Awais.)