Building command line applications with JavaScript

User 8965210 2022-06-23 17:55:01 阅读数:262

buildingcommandlineapplicationsjavascript

JavaScript It's for Web Developed language , But its usefulness has gone far beyond the scope of the Internet . because Node.js and Electron Such a project ,JavaScript It is a general scripting language , It is also a browser component . There are specially designed JavaScript Library to build the command line interface . Yes , You can run... In your terminal JavaScript.

install node

To use Commander.js library , You have to install Node.js. stay Linux On , You can use your package manager to install Node. for example , stay Fedora、CentOS、Mageia And other systems :

$ sudo dnf install nodejs

stay Windows and macOS On , You can   from nodejs.org Download the installer from the website .

install Commander.js

To install Commander.js, Please use  npm  command :

$ npm install commander

In your JavaScript Add a library to the code

stay JavaScript in , You can use  require  Keywords include... In your code ( Or import , If you are used to Python) A library . Create a file called  example.js  The file of , And open it in your favorite text editor . Add this line at the top , To include Commander.js library :

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

JavaScript Option resolution in

Options to resolve , The first thing you have to do is define valid options that your application can accept .Commander.js Library allows you to define short options and long options , There is also useful 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');

First option , I call it  --alpha( Abbreviation  -a), Is a Boolean switch : It either exists , Or it doesn't exist . It doesn't need any parameters . The second option , I call it  --beta( Abbreviation  -b), Take a parameter , Even if you don't provide any parameters, specify a default value .

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

Run the application

Try to use  node  Command to run it , First, do not use the option :

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

When the user does not overwrite ,beta  The default value of is used .

Run it again , Use this option :

$ node ./example.js --beta hello --alphaOptions detected: alphabeta is: hello

This time, , The test script successfully detected the option  --alpha, And user provided  --beta  The value of the option .

Option resolution

The following is the complete demonstration 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);

At the end of the project  Git Warehouse   There are more examples in .

For any application , Including user options is an important function , and Commander.js Make it easy to do . except Commander.js, There are other libraries , But I think this library is very convenient and fast to use . Your favorite JavaScript What is the command line builder ?

版权声明:本文为[User 8965210]所创,转载请带上原文链接,感谢。 https://qdmana.com/2022/01/202201041953251848.html