Using JavaScript to Construct Command Line Application

  • 2021-12-05 05:34:59
  • OfStack

Directory 1, install node2, install Commander. js3, add a library to JavaScript code 4, option analysis in JavaScript 5, access command line data 6, run application 7, option analysis

Foreword:

JavaScript Is a language developed for Web, but its use has gone far beyond the scope of the Internet. Due to Node.js And Electron Such a project, JavaScript It is not only a general scripting language, but also a browser component. There are specially designed ones JavaScript Library to build the command line interface. Yes, you can run it in your terminal JavaScript .

Now, when you enter a command in a terminal, 1 usually has options, also called switches or flags, which you can use to modify how the command runs. This is a useful convention defined by the POSIX specification, so it is helpful as a programmer to know how to detect and resolve these options. To get this functionality from JavaScript, it is useful to use libraries designed to simplify building command-line interfaces. What I like best is Commander.js . It is simple, flexible and intuitive.

1. Install node

To use the Commander.js Library, you must install Node.js . On Linux, you can use your package manager to install Node . For example, in the Node.js 0 , CentOS , Mageia And other systems:


$ sudo dnf install nodejs


In Windows And macOS We can download the installer from the website nodejs. org.

2. Install Commander. js

To install Commander. js, use the npm command:


$ npm install commander

3. Add a library to the JavaScript code

In JavaScrip In t, you can use the require Keyword include (or import, if you are used to Python) 1 library in your code. Create a file named example.js And open it in your favorite text editor. Add this 1 line at the top to include Commander.js Library:


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


4. Option analysis in JavaScript

To parse options, the first thing you must do is define valid options that your application can accept. Commander.js The library allows you to define short and long options, as well as a useful piece of 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 (Abbreviation-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 (Abbreviation-b), accept 1 parameter, and even specify 1 default value if you don't provide any parameters.

5. Access 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);

6. Run the application

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


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

In the case that the user is not covered, beta The default value of is 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 what the user provides-- beta The value of the option.

7. Option resolution

The following 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 more examples in the Git repository of this project.

For any application, including user options is an important function, and Commander.js Make it easy to do. Except for Commander.js There are other libraries, but I think this library is very convenient and quick to use. Your favorite JavaScript What is a command line builder?


Related articles: