Nikita SemenistyiTech Lead

Three useful modules for creating your own Node.js command-line interface


Node.js is without any doubt the most developing programming platform nowadays and many programmers one way or another getting acquainted with it. Once you may face a challenge of creating command-line interface with help of Node.js and in this article we will cover three useful modules, that will help you to complete this task.

Parsing

Parsing options is probably the most important task majority of command-line interfaces handle. So far the most popular node.js module for options parsing is optimist. This one is written by Substack, though in repo description he claims that he uses minimist. Seems like this dependencies relationship, when one module depends on another, performing mostly the same task and written by the same person reflects peculiarities of Node.js development cycle encouraged by npm package manager.

Still I would like to explore another one module, also based on minimist, called realist. It completes the variety of programmer’s mental attitudes, initiated by optimist and minimist.

There are two main features presented in the API of realist, which make me use it. The first one is beautiful commands schema template:

In this hash we see reset command, which is followed by the required argument and also optional one. Any number of commands can be created with required, optional or even without arguments at all, throw pillows on saleof them were matched, default command will be executed.
The second feature I would like to describe is event model used in application. Here is the list of events send within the application:
option <option name>- sent if option was specified.

candidates - command not found, but there are some candidates. By default it's handled by missingRequiredArgument handler.

default - default command was called.

Coloring

The next priority for the command-line interface apps in my opinion is output beautifying. Many of users are afraid of console because it seems to them unclear and coloring the output may dramatically improve the readability of the data written.

Let’s look at the module chalk  by sindresorhus. Here is the image of transformations it allows to perform to text:

Untitled

Chalk comes with an easy to use composable API where you just chain and nest the styles you want:

Autocompletion

Autocompletion in the command line is one of the main features, which allow it to stay in many cases much more effective than any other interfaces. Recently a module implementing autocompletion in Node.js CLI apps gained huge popularity.

It is called omelette. Here’s a demo from the repository:


Here’s how we configure it:

And then all we need to do is install autocomplete to the console, which is basically to add appropriate commands to configuration file. Made easy with such commands:
In zsh, you can write these:

In bash, you should write:

I hope these modules will be helpful for you when developing console application. I just have only one thing to add: if you intend to develop a module which should be installed globally, never use CRLF as bash will fail on Linux while trying to interpret it.