Building command line applications with JavaScript

Posted by ubersmuck on Tue, 04 Jan 2022 19:44:00 +0100

JavaScript is a language developed for the Web, but its usefulness has gone far beyond the scope of the Internet. Due to node For projects such as JS and Electron, JavaScript is not only a general scripting language, but also a browser component. There are specially designed JavaScript libraries to build command-line interfaces. Yes, you can run JavaScript in your terminal.

Install node

To use commander JS library, you must install Node js. On Linux, you can install Node with your package manager. For example, on Fedora, CentOS, Mageia, and other systems:

$ sudo dnf install nodejs

On Windows and macOS, you can {from nodejs Download the installer from

Install commander js

To install commander JS, please use the {npm} command:

$ npm install commander

Add a library to your JavaScript code

In JavaScript, you can use the "require" keyword to include (or import, if you are used to Python) a library in your code. Create an example named example JS and open it in your favorite text editor. Add this line at the top to include commander JS Library:

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

Option parsing in JavaScript

To parse options, the first thing you must do is define valid options that your application can accept. Commander.js library allows you to define short options and long options, and there is also a useful information to clarify the purpose of each option.

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 call -- alpha (abbreviated - a), is a Boolean switch: it either exists or does not exist. It does not require any parameters. The second option, which I call -- beta (abbreviated as - b), accepts a parameter and even specifies a default value without providing any parameters.

Accessing command line data

When you define valid options, you can use long option names to refer to these values:

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 application

Try to run it with the node command without using the option:

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

The default value of beta} is used when the user does not overwrite it.

Run it again, this time with the following options:

$ node ./example.js --beta hello --alphaOptions detected: alphabeta is: hello

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

Option resolution

The following is the complete 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 more examples in the # Git warehouse # of the project.

For any application, including user options is an important function, and commander JS makes it easy to do. Except commander JS, there are other libraries, but I think this library is very convenient and fast to use. What is your favorite JavaScript command line builder?