Preface:
JavaScript
It is a language developed for the Web, but its uses have gone far beyond the scope of the Internet. because
andElectron
Such a project,JavaScript
It is both a common scripting language and a browser component. Specially designedJavaScript
library to build command line interface. Yes, you can run it in your terminalJavaScript
。
Now, when you enter a command in the terminal, there are usually options, also called switches or flags, which you can use to modify the way the commands run. 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 is useful to use libraries designed to simplify building command line interfaces. My favorite thing is
. It's simple, flexible, and intuitive.
1. Install node
To use
Library, you must install
. On Linux, you can install it using your package managerNode
. For example,Fedora
、CentOS
、Mageia
And on other systems:
$ sudo dnf install nodejs
existWindows
andmacOS
On, we canwebsiteDownload the installer.
2. Installation
To install , use the npm command:
$ npm install commander
3. Add a library to JavaScript code
existJavaScrip
In t, you can userequire
Keywords are included (or imported, if you are used to Python) a library in your code. Create a name calledand open it in your favorite text editor. Add this line at the top to include
Library:
const { program } = require('commander');
4. Option analysis in JavaScript
To parse options, the first thing you have to do is define valid options that your app can accept.
The library allows you to define short and long options, while also having a 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');
The first option, I call it --alpha
(Abbreviation -a), is a Boolean switch: it either exists or does not exist. It requires no parameters. The second option, I call it --beta
(Abbreviation -b), accepts a parameter, and even specifies a default value without providing any parameters.
5. Access command line data
When you define valid options, you can refer to these values using long option names:
(); const options = (); ('Options detected:'); if () ('alpha'); const beta = ! ? 'no' : ; ('beta is: %s', beta);
6. Run the application
Try running it with the node command, first without using the options:
$ node ./ Options detected: beta is: Foo
In the event that the user does not have coverage,beta
The default value of .
Run it again, this time using options:
$ node ./ --beta hello --alpha Options detected: alpha beta is: hello
This time, the test script successfully detected the option --alpha
, and user-provided --beta
The value of the option.
7. Option analysis
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'); (); const options = (); ('Options detected:'); (typeof options); if () (' * alpha'); const beta = ! ? 'no' : ; (' * beta is: %s', beta);
In the projectGit RepositoryThere are more examples in this.
For any application, including user options is an important feature, and
Make it easy to do. Apart from
, and there are other libraries, but I think this library is very convenient and fast to use. Your favoriteJavaScript
What is a command line builder?
This is the end of this article about using JavaScript to build command line applications. For more related contents of JavaScript to build command line applications, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!