How I build command-line apps in JavaScript

Including options for your users is an important feature for any application, and Commander.js makes it easy to do. What's your favorite JavaScript command-line builder? Take our poll.
53 readers like this.
Javascript code close-up with neon graphic overlay

Photo by Jen Wike Huger

JavaScript is a language developed for the web, but its usefulness has gone far beyond just the Internet. Thanks to projects like Node.js and Electron, JavaScript is as much a general-purpose scripting language as a browser component. There are JavaScript libraries specially designed to build command-line interfaces. Yes, you can run JavaScript in your terminal.

Now, when you enter a command into your terminal, there are generally options, also called switches or flags, that you can use to modify how the command runs. This is a useful convention defined by the POSIX specification, so as a programmer, it's helpful to know how to detect and parse the options. To get this functionality from JavaScript, it's useful to use a library designed to make it easy to build command-line interfaces. My favorite is Commander.js. It's easy, it's flexible, and it's intuitive.

Installing node

To use the Commander.js library, you must have Node.js installed. On Linux, you can install Node using your package manager. For example, on Fedora, CentOS, Mageia, and others:

$ sudo dnf install nodejs

On Windows and macOS, you can download installers from the nodejs.org website.

Installing Commander.js

To install Commander.js, use the npm command:

$ npm install commander

Adding a library to your JavaScript code

In JavaScript, you can use the require keyword to include (or import, if you're used to Python) a library into your code. Create a file called example.js and open it in your favorite text editor. Add this line to the top to include the Commander.js library:

const { program } = require('commander');

Option parsing in JavaScript

The first thing you must do to parse options is to define the valid options your application can accept. The Commander.js library lets you define both short and long options, along with a helpful message to clarify the purpose of each.

program
  .description('A sample application to parse options')
  .option('-a, --alpha', 'Alpha')
  .option('-b, --beta <VALUE>', 'Specify a VALUE', 'Foo');

The first option, which I've called --alpha (-a for short), is a Boolean switch: It either exists or it doesn't. It takes no arguments. The second option, which I've called --beta (-b for short), accepts an argument and even specifies a default value when you've provided nothing.

Accessing the command-line data

Once you've defined valid options, you can reference the values using the long option name:

program.parse();

const options = program.opts();
console.log('Options detected:');

if (options.alpha) console.log('alpha');
 
const beta = !options.beta ? 'no' : options.beta;
console.log('beta is: %s', beta);

Run the application

Try running it with the node command, first with no options:

$ node ./example.js 
Options detected: 
beta is: Foo

The default value for beta gets used in the absence of an override from the user.

Run it again, this time using the options:

$ node ./example.js --beta hello --alpha
Options detected: 
alpha
beta is: hello

This time, the test script successfully detected the option --alpha, and the --beta option with the value provided by the user.

Option parsing

Here's the full demonstration code for your reference:

const { program } = require('commander');

program
  .description('A sample application to parse options')
  .option('-a, --alpha', 'Alpha')
    .option('-b, --beta <VALUE>', 'Specify a VALUE', 'Foo');

program.parse();

const options = program.opts();
console.log('Options detected:');

console.log(typeof options);

if (options.alpha) console.log(' * alpha');
const beta = !options.beta ? 'no' : options.beta;
console.log(' * beta is: %s', beta);

There are further examples in the project's Git repository.

Including options for your users is an important feature for any application, and Commander.js makes it easy to do. There are other libraries aside from Commander.js, but I find this one easy and quick to use. What's your favorite JavaScript command-line builder?

What to read next
Seth Kenlon
Seth Kenlon is a UNIX geek, free culture advocate, independent multimedia artist, and D&D nerd. He has worked in the film and computing industry, often at the same time.
Ramakrishna Pattnaik
Ramakrishna Pattnaik is a web developer from Bhubaneswar currently working as an Associate Software Engineer with the Red Hat Middleware team in Bangalore. An open source enthusiast and contributor. Loves fiction.

Comments are closed.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.