CLI (Command Line Interface) applications are simplistic text-based apps that run in the terminal to complete specific tasks. CLI applications play a crucial role in the workflow of almost every developer and IT professional.

They are mostly utility tools that interact with the operating system or applications that are either installed locally or available over the internet to perform a task according to the user’s input and directives.

Screenshot of Urban Dictionary API’s page on Rapid API show user API credentials

Understanding CLI Applications

A command-line interface lets you interact with a program by typing lines of text. Many CLI programs run differently depending on the command you use to start them.

For example,the ls program displays file informationand the contents of directories. You might run it like this:

Screenshot showing the output of running the help command

This command includes:

While each program may define its own command-line interface, certain elements are common and in wide use. You should follow these standards so that someone who is familiar with a command-line interface will be able to use your programs easily.

What Is Commander.js?

Commander.js isa package that lets you build CLI apps in Node.js. It has a rich library of features that let you build a standard CLI application, carrying out much of the heavy work. You only have to define commands, options, and functionality for your CLI app.

Combining it with other packages such as Chalk.js for styling, you can quickly create a fully functional CLI app in Node.js.

Screenshot of terminal showing results of installation command and a test run

Building a CLI Application in Node.js Using Commander.js

Consider an example CLI app,urbanary-cli,which looks up the meaning of words and social media abbreviations fromthe Urban Dictionary. You’ll learn how to create the CLI and publish it to thenpmpackage registry so that others can install it.

Create a new folder and initialize a new Node.js project with the following commands:

This CLI will useAxios to send HTTP requeststo the Urban Dictionary API. you may useRapid APIto check endpoints and view credentials.

A Simple CLI With a Subcommand and Help

To start building your CLI, install Commander and Axios with the following command:

Create a new folder,bin, in your project directory and a new empty file,index.js:

Thebin(short for “binary”) folder is important because it holds the entry point file that Node calls when you run your CLI. Theindex.jsfile is this entry point file. Now, edit index.js file and start building your CLI with the Commander.js API.

First, import theprogramobject from Commander:

You’ll use theprogramobject to define your program’s interface, including sub-commands, options, and arguments. The object has corresponding methods for each of these; for example, to define a sub-command, use thecommandmethod.

Define afindsubcommand for the CLI to look up words from Urban Dictionary and add a description for it using the code below:

This registers afindcommand, which expects a word after it, and a description for it. The use of angle brackets signifies that the word is a required argument; use square brackets instead ([]) to make it optional.

You should add a description because Commander.js uses it to generate help text. When you run the application with thehelpcommand, you’ll get a standard usage guide.

To test this, add the following:

Then run the program and pass it thehelpcommand to get the output below:

This is how any standard CLI application will display its help to users and, with Commander, you don’t have to worry about creating it yourself. The-hand–helpoptions are useful for checking the usage guide for a command.

Defining Options and Preparing the Final Program

You also define an option by chaining theoptionmethod to the command definition.

Here’s how to define an option to include examples in the definitions of words:

And here’s how to define an option specifying the number of definitions to return:

Theoptionmethod accepts two string parameters, one for the option’s name (both short and long forms), and the other for its description. The extra[amount]argument in thecountoption is the value for the number of definitions to display.

Now, the last method to add is theactionmethod. You will implement thefindcommand’s functionality within this method. Add it to the chain so that your code now looks like this:

With this setup, here’s what a command to get three definitions oflolwith examples will look like:

​Or, using the long form of each option:

Check outCommander’s npm pageto learn more about it and how to adapt its functions for your different use cases.

Implementing the Program’s Functionality

First, import Axios into yourindex.jsfile as follows:

Then, in the function body ofaction’s parameter, you can implement the logic to make requests to Urban Dictionary and display results according to your options.

Start by defining your request:

Then make the request to the API using Axios with the following code:

The only property you need from the response data is thelistproperty which holds definitions and examples.

Still in thetryblock, add this logic to handle options and display the results as follows:

This code evaluates the command arguments using if-else statements to determine how to display the output. If theexampleandcountoptions are passed, it iterates throughwordDataand prints the specified number of definitions and examples with them.

If you pass onlycount, it displays that amount of definitions without examples. If you pass onlyexample, it displays one definition with an example sentence. Theelsestatement is the default behavior to print just the definition if you don’t pass any options.

The application is now ready, so the next step is to make it executable. Start by addinga shebang lineto the beginning of your bin/index.js file so that you can run it as a standalone script:

Next, open yourpackage.jsonfile, edit the value of themainproperty, and add abinproperty after it like this:

The keyurbanary-cli, underbinis the command you’ll enter in your terminal to run your application. So, be sure to use a befitting name there when building your command line applications.

Runnpm install -gto install the application globally, and you will be able to execute the application as a command from your terminal.

The image below shows the installation process and a test command to find the meaning oflmk:

You can also publish it to the npm package registry by runningnpm publishin the terminal within the project directory. This makes it installable by anyone from anywhere usingnpm install.

It is easier to build and publish your application with Node.js, compared to when youbuild CLIs with technologies like Rust.

Build Functional CLI Applications With Node.js

Whether you’re working on a npm package and need a CLI utility to accompany it, or you just want to build a tool to improve your workflow as a developer. You have all you need to bring your idea to life with the Node.js Commander package.

You may also go further by using other libraries to create improved CLI experiences for your applications, Node.js is robust enough to serve your purposes without much hassle.