1. #!/usr/bin/env node
  2. const process = require('process');
  3. /**
  4. * Aggregates values of command-line options.
  5. * @returns {object} An object containing the aggregated options.
  6. */
  7. function aggregateOptions() {
  8. const options = {}; // Initialize an empty object to store options
  9. for (let i = 0; i < process.argv.length - 2; i += 2) {
  10. const optionName = process.argv[i];
  11. const optionValue = process.argv[i + 1];
  12. if (optionName === '--debug') {
  13. options.debug = true; // Set debug flag if --debug is present
  14. } else if (optionName === '--value') {
  15. options.value = optionValue; // Store the value of --value
  16. } else if (optionName === '--add') {
  17. if (options.add) {
  18. options.add = parseInt(optionValue, 10) + parseInt(options.add, 10); // Add to existing value
  19. } else {
  20. options.add = parseInt(optionValue, 10); // Initialize if --add not present
  21. }
  22. } else if (optionName === '--multiply') {
  23. if (options.multiply) {
  24. options.multiply = parseInt(optionValue, 10) * parseInt(options.multiply, 10);
  25. } else {
  26. options.multiply = parseInt(optionValue, 10);
  27. }
  28. }
  29. else if (optionName === '--append') {
  30. if (options.append) {
  31. options.append += optionValue; // Append to existing value
  32. } else {
  33. options.append = optionValue; // Initialize if --append not present
  34. }
  35. }
  36. }
  37. return options;
  38. }
  39. const aggregatedOptions = aggregateOptions();
  40. console.log(JSON.stringify(aggregatedOptions, null, 2)); // Output the aggregated options as JSON

Add your comment