1.1.4 • Published 1 year ago

parametermapper v1.1.4

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

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

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
}

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
}
1.1.4

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.0

1 year ago