Skip to content

AsP3X/parametermapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

paraMap

The paraMap object contains a method convertArgs2Map that converts command line arguments into a map.

Usage

const paraMap = require('parametermapper');

const args = process.argv.slice(2);
const parameterMap = paraMap.convertArgs2Map(args);


convertArgs2Map

The convertArgs2Map method takes in an array of command line arguments and returns an object with the arguments as keys and their values as the corresponding properties.

$ node script.js -a value1 -b value2 -c

This would result in the following object:

{
  '-a': 'value1',
  '-b': 'value2',
  '-c': true
}

Note that if a command line argument does not have a value, its value will be set to `true`.


convertArgs2Array

The convertArgs2Array function is used to convert command line arguments into an array. It takes in an array of command line arguments (args) and returns an array of arguments and their values. The function first filters out all parameters that include the string '-', and then checks if the following parameter is a value or another parameter. If it is a value, it is added to the array. If it is another parameter, only that parameter is added to the array. For example, running the following command:

$ node script.js -a value1 -b value2 -c

This would result in the following array:

[ '-a', 'value1', '-b', 'value2', '-c' ]

Note that if a command line argument does not have a value, it is still added to the array as a single element.


Another Example

const paraMap = require('parametermapper');

const args = process.argv.slice(2);
const parameterMap = paraMap.convertArgs2Map(args);

if (parameterMap['-h']) {
  console.log('Usage: node script.js [-a value] [-b value] [-c]');
}

if (parameterMap['-a']) {
  console.log(`Value for -a: ${parameterMap['-a']}`);
}

if (parameterMap['-b']) {
  console.log(`Value for -b: ${parameterMap['-b']}`);
}

if (parameterMap['-c']) {
  console.log('Option -c was specified');
}

This script can be run with the following command:

$ node script.js -a value1 -b value2 -c

This will output the following:

Value for -a: value1
Value for -b: value2
Option -c was specified

The parameterMap object will contain the following key-value pairs:

{
  "-a": "value1",
  "-b": "value2",
  "-c": true
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published