#!/usr/bin/env node
const process = require('process');
/**
* Aggregates values of command-line options.
* @returns {object} An object containing the aggregated options.
*/
function aggregateOptions() {
const options = {}; // Initialize an empty object to store options
for (let i = 0; i < process.argv.length - 2; i += 2) {
const optionName = process.argv[i];
const optionValue = process.argv[i + 1];
if (optionName === '--debug') {
options.debug = true; // Set debug flag if --debug is present
} else if (optionName === '--value') {
options.value = optionValue; // Store the value of --value
} else if (optionName === '--add') {
if (options.add) {
options.add = parseInt(optionValue, 10) + parseInt(options.add, 10); // Add to existing value
} else {
options.add = parseInt(optionValue, 10); // Initialize if --add not present
}
} else if (optionName === '--multiply') {
if (options.multiply) {
options.multiply = parseInt(optionValue, 10) * parseInt(options.multiply, 10);
} else {
options.multiply = parseInt(optionValue, 10);
}
}
else if (optionName === '--append') {
if (options.append) {
options.append += optionValue; // Append to existing value
} else {
options.append = optionValue; // Initialize if --append not present
}
}
}
return options;
}
const aggregatedOptions = aggregateOptions();
console.log(JSON.stringify(aggregatedOptions, null, 2)); // Output the aggregated options as JSON
Add your comment