Let’s Build a CLI | Command Line Interface with Node.js

Manav Shrivastava
4 min readMar 29, 2022

We use command-line utilities every day, whether it be git, grep, awk, npm, or any other terminal app.

CLIs are super useful and usually the fastest way to get something done.

Today we will learn how can we create our own CLI tool.

Setting up the project

We’ll be setting up a basic Node.js project:

  1. Open up your terminal.

2. Create a folder for the project.

~$mkdir mycli

3. Navigate to it.

~$cd mycli

4. Initialize a Node.js project in that folder.

~$npm init

5. Fill in the prompt.

Your project is now set up.

Building the basic CLI

Now that we have our node project ready we move to actually make the CLI. Here’s what you have to do:

  1. Create a folder named bin in the root directory of your project.
  2. Inside bin create a file called index.js This is going to be the entry point of our CLI.
  3. Now open the package.json file and change the “main” part to bin/index.js.
  4. Now manually add another entry into the package.json file called bin and set its key to mycli and it's value to ./bin/index.js. The addition should look something like this:
"bin": {  
"mycli": "./bin/index.js"
}

The key, mycli, is the keyword for calling the CLI. This is the keyword that people will type in the terminal for using your CLI. Be free to name it whatever you like, although I would suggest keeping the name short
and semantic so that it’s quick to type and easy to remember.

The name defined here is not permanent and can be changed whenever you like.

Now let’s install and test our CLI.

People may call our CLI from anywhere in the system so let’s install it globally.

Navigate to the root directory of the project and then run

~$npm install -g .

The -g the flag tells npm to install the package globally on the system.

Test the CLI by typing the specified keyword in the terminal.

~$mycli

Handling Command Line arguments

Our basic CLI is ready and now we move to add further functionality.

The most basic task that any CLI does is handling command-line arguments. In our CLI, we will be receiving the language name and the sentence to be translated as arguments and then we will parse it.

Although Node.js offers built-in functionality for handling command-line arguments, we are going to use an npm package called yargs 🏴‍☠ which is specifically made for building CLI
s. yargs will simplify our process of parsing arguments and help us organize command-line flags.

  1. Install yargs
~$npm i yargs

2. After installing it, include the module in your index.js :

~$const yargs = require("yargs");

3. Then create the options object containing all your command line flags:

const options = yargs.usage(usage).option("l", {alias:"language", describe: "Translate to language", type: "string", demandOption: false }).option("s", {alias:"sentence", describe: "Sentence to be translated", type: "string", demandOption: false }).help(true).argv;

In the above code, I have defined an option -l which, when passed will print all the supported languages by the API, we will implement this later. Yargs provides us with --help and --version flags by default.

If you want an option to be compulsory then you can set it’s demandOption value to true , this will get yar
gs to throw a Missing argument error if the flag is not provided.

mycli <command>

mycli demo

Beautification using Boxen and Chalk

We can use terminal colors using chalk and boxes to decorate our output using boxen.

  1. Install chalk and boxen.
npm install chalk boxen

2. Include them in your index.js

const chalk = require('chalk');  
const boxen = require('boxen');

3. Add color to the usage constant.

const usage = chalk.keyword('violet')("\nUsage: mycli -l <language>  -s <sentence> \n"+ boxen(chalk.green("\n" + "Translates a sentence to specific language" + "\n"), {padding: 1, borderColor: 'green', dimBorder: true}) + "\n");
mycli

4. Display the output using a beautiful box.

translate(sentence, {to: language}).then(res => {console.log("\n" + boxen(chalk.green("\n" + res.text + "\n"
), {padding: 1, borderColor: 'green', dimBorder: true}) + "\n");}).catch(err => {
console.error(err);
});

Hope you had fun learning how to build your own CLI :)

because I had a lot of fun.

Happy coding!

Here’s a list of the packages you can use specifically for developing the command-line tool:

  • chalk — colorizes the output
  • clear — clears the terminal screen
  • clui — draws command-line tables, gauges and spinners
  • figlet — creates ASCII art from text
  • inquirer — creates interactive command-line user interface
  • minimist — parses argument options
  • configstore — easily loads and saves config without you having to think about where and how.
  • oclif — The Open CLI Framework

If you want to create and publish your npm package, you can read my below article -

https://betterprogramming.pub/create-and-publish-your-npm-package-node-module-in-just-10-minutes-4b91e30fe9d7

The complete code for this can be found at: https://github.com/manavshrivastavagit/mycli

Fork me on Github maybe :)

Connect with me on LinkedIn.

--

--