Get in touch
Thank you
We will get back to you as soon as possible
.pdf, .docx, .odt, .rtf, .txt, .pptx (max size 5 MB)

21.5.2015

3 min read

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:

var realist = require('realist'); 
var commands = { 
    // Required + optional arguments 
    'reset [revision]': function(opt, target, revision) { 
        console.log('reset', opt, target, revision); 
    }, 
    // Default command 
    'default': function(opt) { 
        console.log('I am default command!'); 
    }
}; 
realist(commands);

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 **- 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:

console.log( chalk.blue('Hello'), 'World' + chalk.red('!') );
console.log( chalk.red('Hello', chalk.underline.bgBlue('world') + '!') );

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:

Three useful modules for creating your own Node.js command-line interface-1
Here’s how we configure it:

// Write your CLI template. 
var complete = omelette("githubber "); 
complete.on("action", function() { 
    return this.reply(["clone", "update", "push"]); 
}); 
complete.on("user", function(action) { 
    return this.reply(fs.readdirSync("/Users/")); 
}); 
complete.on("repo", function(user) { 
    return this.reply([ "http://github.com/" + user + "/helloworld", "http://github.com/" + user + "/blabla" ]); }); 
// Initialize the omelette
complete.init();

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:

echo '. <(./githubber --completion)' >> .zshrc

In bash, you should write:

./githubber --completion >> ~/githubber.completion.sh echo 'source ~/githubber.completion.sh' >> .bash_profile

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.

Thank you
We will get back to you as soon as possible

Looking for a tech partner to help you scale your project?

Let’s schedule a quick call to explore how we can support your business objectives.

Edward Robe

Let’s schedule a quick call to explore how we can support your business objectives.

Edward Robe

Senior Client Manager

0 Comments
name *
email *

Featured Case Studies