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)

Blog    

Recommend

Solve the MySQL login 1045 problem under centos

Since the entire application needs to be deployed...

Analysis of the reasons why MySQL's index system uses B+ tree

Table of contents 1. What is an index? 2. Why do ...

How to use shtml include

By applying it, some public areas of the website c...

An example of implementing a simple finger click animation with CSS3 Animation

This article mainly introduces an example of impl...

How to use dl(dt,dd), ul(li), ol(li) in HTML

HTML <dl> Tag #Definition and Usage The <...

jQuery custom magnifying glass effect

This article example shares the specific code of ...

Summary of 11 common mistakes made by MySQL call novices

Preface You may often receive warning emails from...

SQL statements in Mysql do not use indexes

MySQL query not using index aggregation As we all...

MariaDB-server installation of MySQL series

Table of contents Tutorial Series 1. Install Mari...

Detailed process of using Vscode combined with docker for development

Preface Using Docker and VS Code can optimize the...

Detailed explanation of JavaScript data types

Table of contents 1. Literals 1.1 Numeric literal...

jQuery realizes the scrolling effect of table row data

This article example shares the specific code of ...

URL Rewrite Module 2.1 URL Rewrite Module Rule Writing

Table of contents Prerequisites Setting up a test...

How to query the latest transaction ID in MySQL

Written in front: Sometimes you may need to view ...