Last Update: March 14, 2022
BY eric
Keywords
node-programmer
In this post, we introduce an easy-to-use and super-flexible arguments parsing library - 'node-prorgammer' for nodejs.
Parsing arguments is a simple task, it can be done in a few lines of code. However, there are a few things to consider in order to properly parse the arguments:
- The order of the arguments (e.g. arg1 arg2 arg3 VS arg3 arg2 arg1) or (e.g. --arg1 argv1 input1 VS input --arg1 argv1)
- The separation between an argument input (e.g. --width 50) and a normal input (e.g. /a/path/to/the/input/file)
- Boolean argument input (e.g. --take-action true/false or yes/no or just take no value at all)
- How to handle default value(s).
Sometimes we only need tuning a couple of parameters and use the default values for the rest
- Output usage, when there is no input(s), we print out the usage
So things can get complicated if all the above need considerations.
For that we made 'node-prorgammer' package to make arguments parsing easy and flexible.
Installation
First, we need to install the package for your project.
npm install --save node-programmer
Usage Example
Second, here is an example of using it.
To acquire parameters from the command line:
// import the Params class
var Params = require('node-programmer/params');
var params = new Params({
"long": "long",
"port": "8080",
"host": "localhost",
"static": "./static",
"views": {default: "./views", short: "i"},
"forward": false
});
/**
* All the necessary arguments are included in the "opts" object
*/
var opts = params.getOpts();
/**
* Opitons (Parameters / Arguments) count equals to the number of specified arguments,
* not the total count of all parameters you pass them to program from the command line
* And the empty parameters will be ignored, and not included in the option count
*/
var optCount = params.getOptCount();
// do something if you don't get the right number of arguments
if (optCount < 1) {
console.error("Not enough parameters provide");
// print out the usage before exiting the program
process.exit();
}
// getting all the inputs, which is an array type even only one input is provided
var inputs = opts['---'];
// For example connect to a db
// var db_connection = new DBConnection(opts.host, opts.port);
From the command line, you can pass the parameters to your program like the following:
node db.js -p 3306 -h 192.168.1.8 -i "./views2"
Also, you can pass the arguments directly from the code
var params = new Params({
"long": "long",
"port": "8080",
"host": "localhost",
"static": "./static",
"views": {default: "./views", short: "i"},
"forward": false
});
var opts = params.getOpts(["-p", "8080", "-h", "127.0.0.1", "input1", "input2", "-i", "./views2", "-f"]);
var optCount = params.getOptCount();
var inputs = opts['---'];
TODO
- make the library be able to read input after an equal sign (e.g. --width=50)
- add a "description" value in the default input and processing for usage output.
Contact
For suggestions, errors or any others.
I can be reach via:
- twitter: Eric Tang
- email: [email protected]