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
Since the entire application needs to be deployed...
The performance of your website or service depend...
Table of contents 1. What is an index? 2. Why do ...
By applying it, some public areas of the website c...
This article mainly introduces an example of impl...
HTML <dl> Tag #Definition and Usage The <...
This article example shares the specific code of ...
Preface You may often receive warning emails from...
MySQL query not using index aggregation As we all...
Table of contents Tutorial Series 1. Install Mari...
Preface Using Docker and VS Code can optimize the...
Table of contents 1. Literals 1.1 Numeric literal...
This article example shares the specific code of ...
Table of contents Prerequisites Setting up a test...
Written in front: Sometimes you may need to view ...