preloader
post-thumb

Last Update: March 14, 2022 1 min read


BYauthor-thumberic

|Loading...

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.

javascript
npm install --save node-programmer

Usage Example

Second, here is an example of using it.

To acquire parameters from the command line:

javascript
// 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:

bash
node db.js -p 3306 -h 192.168.1.8 -i "./views2"

Also, you can pass the arguments directly from the code

javascript
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:

Comments (0)

Leave a Comment
Your email won't be published. We'll only use it to notify you of replies to your comment.
Loading comments...
Previous Article
post-thumb

Mar 14, 2022

How to Download an Offline Wikipedia Database in Tyokiie (Android)

Step-by-step: set your main Wikipedia language in Tyokiie, download its offline database, and switch offline mode on and off. Includes the five languages available and their real download sizes.

Next Article
post-thumb

Oct 03, 2021

Setting up Ingress for a Web Service in a Kubernetes Cluster with NGINX Ingress Controller

A simple tutorial that helps configure ingress for a web service inside a kubernetes cluster using NGINX Ingress Controller

agico

We transform visions into reality. We specializes in crafting digital experiences that captivate, engage, and innovate. With a fusion of creativity and expertise, we bring your ideas to life, one pixel at a time. Let's build the future together.

Copyright ©  2026  TYO Lab · v0.0.38