argsreader

read commend line agrment

Usage no npm install needed!

<script type="module">
  import argsreader from 'https://cdn.skypack.dev/argsreader';
</script>

README

Args Reader

enter image description here

A wrapper for process.argv, to read and parse args from the command line, also supporting JSON object

Installation

You can install this module using NPM:

  npm install --save argsreader

API

args.Read()

  • Simple

    Read and Parse simple args to an object:

      let argsreader = require "argsreader"
      var args = argsreader.Read();
      console.log(args);
    
      $ node myapplication.js -port 80 -ssl true -path  '~/usr/src/apps'
    

    The Result will be as follow:

    {
      "port": 80, 
      "ssl": true,
      "path": '~/usr/src/app/'
    }
    
  • Complex

    You can also read and parse complex values:

      $ node myapplication.js -DbConnections 'MongoDb/localhost/applicationdb' -AppSettings.GeneralSettings.SSL true -AppSettings.Path  '~/usr/src/path'
    

    The Result will be as follow:

    {
      "DbConnections": 'MongoDB/localhost/applicationdb', 
      "AppSettings" {
        GeneralSettings :{
          SSL: true
        }, 
        "Path": '~/usr/src/app/'
      }
    }
    

args.ReadAndReassign()

You can also Read and Reassign args to custom object for example:

var Settings = {
    Port: 9000,
    SSL: true,
    IsActive: false,
    General: {
        SSLPort: {
            Production: 443,
            Dev: 4433
        }
    }
}

var args = argsReader.Read();
console.log('Settings was:-> ' + JSON.stringify(Settings));
argsReader.ReadAndReassign(Settings);
console.log('Passed args:->' + JSON.stringify(args));
console.log('Settings new values:-> ' + JSON.stringify(Settings));
$ node example.js -Port 80 -SSL false -General.SSLPort.Production 4433 -IsActive true

The result will be:

Settings was:-> {"Port":9000,"SSL":true,"IsActive":false,"General":{"SSLPort":{"Production":443,"Dev":4433}}}
Passed args:->{"Port":80,"SSL":false,"General":{"SSLPort":{"Production":4433}},"IsActive":true}
Settings new values:-> {"Port":80,"SSL":false,"IsActive":true,"General":{"SSLPort":{"Production":4433,"Dev":4433}}}