Preface: 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 1. Install node To use the $ sudo dnf install nodejs On 2. Install Commander.jsTo install Commander.js, use the npm command: $ npm install commander 3. Add a library to your JavaScript code In 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 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 5. Access command line dataOnce 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 applicationTry 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, 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 7. Option analysisHere 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 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 use nginx to configure access to wgcloud
Copy code The code is as follows: <span style=...
We often want to find a file in Linux, but we don...
1. Vector Map Vector graphics use straight lines ...
Two implementations of Vue drop-down list The fir...
Preface add_header is a directive defined in the ...
Last year, the open letter was a huge hit, even a...
By default, the border of the table is 0, and we ...
First of all, you can understand the difference b...
When writing the HTTP module of nginx, it is nece...
This article summarizes the knowledge points of M...
BinLog BinLog is a binary log that records all da...
Preface This article mainly introduces the analys...
1. The vertical-align property achieves the follo...
When developing a website function, the session c...
Preface This tutorial installs the latest version...