Building command line applications with JavaScript

Building command line applications with JavaScript

Preface:

JavaScript is a language developed for the Web, but its usefulness extends far beyond the Internet. JavaScript is both a general-purpose scripting language and a browser component, thanks to projects like Node.js and Electron . There are JavaScript libraries specifically designed for building command-line interfaces. Yes, you can run JavaScript in your terminal.

Now, when you type a command into the terminal, there are generally options, also called switches or flags, that you can use to modify the way the command runs. This is a useful convention defined by the POSIX specification, so as a programmer it is helpful to know how to detect and parse these 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 simple, flexible, and intuitive.

1. Install 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 other systems:

$ sudo dnf install nodejs


On Windows and macOS , we can download the installer from the nodejs.org website.

2. Install Commander.js

To install Commander.js, use the npm command:

$ npm install commander

3. Add a library to your JavaScript code

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

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


4. Option parsing in JavaScript

To parse options, the first thing you must do is 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 clarifying 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 ( -a for short), is a boolean switch: it is either present or it is not. It does not require any parameters. The second option, which I call beta ( -b for short), accepts a single argument and even specifies a default value if you don't provide any.

5. Access command line data

Once you have defined valid options, you can refer to the values ​​using the long option names:

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);

6. Run the application

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

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

In the absence of an override by the user, beta defaults are used.

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 value provided by the user for the beta option.

7. Option analysis

Here is the complete demo 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 many more examples in the project's Git repository.

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

This is the end of this article about using JavaScript to build command line applications. For more relevant JavaScript building command line applications content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to collect and parse command line arguments in Node.js
  • How to use nodejs to implement command line games
  • Node.js uses yargs to process command line parameters
  • How to parse and format JSON output using Linux command line tools
  • Node.js command line tutorial with pictures and text
  • Node.JS checks whether the Chrome browser is installed and opens the specified URL in the command line

<<:  How to use nginx to configure access to wgcloud

>>:  CSS commonly used font style to set the font of a variety of changes (example detailed explanation)

Recommend

Specific use of Linux which command

We often want to find a file in Linux, but we don...

Solve the problem of OpenLayers 3 loading vector map source

1. Vector Map Vector graphics use straight lines ...

Comparison of two implementation methods of Vue drop-down list

Two implementations of Vue drop-down list The fir...

A designer complains about Hammer's official website again

Last year, the open letter was a huge hit, even a...

HTML table markup tutorial (2): table border attributes BORDER

By default, the border of the table is 0, and we ...

How to build SFTP server and image server on Linux cloud server

First of all, you can understand the difference b...

Detailed analysis of each stage of nginx's http request processing

When writing the HTTP module of nginx, it is nece...

Summary of MySql index, lock, and transaction knowledge points

This article summarizes the knowledge points of M...

Analysis and Solution of ERROR:2002 Reported When MySQL Starts

Preface This article mainly introduces the analys...

CSS to achieve horizontal lines on both sides of the middle text

1. The vertical-align property achieves the follo...

UrlRewriter caching issues and a series of related explorations

When developing a website function, the session c...

How to configure NAS on Windows Server 2019

Preface This tutorial installs the latest version...