1. #!/usr/bin/env node
  2. /**
  3. * Strips metadata from CLI arguments for monitoring.
  4. *
  5. * @param {string[]} args The array of command-line arguments.
  6. * @returns {object} An object containing the stripped arguments.
  7. */
  8. function stripMetadata(args) {
  9. const strippedArgs = {};
  10. for (let i = 0; i < args.length; i++) {
  11. const arg = args[i];
  12. const trimmedArg = arg.trim(); // Remove leading/trailing whitespace
  13. if (trimmedArg.startsWith('--')) {
  14. const key = trimmedArg.slice(2); // Remove the '--' prefix
  15. strippedArgs[key] = args[i + 1] || null; // Store value, null if no value provided
  16. i++; // Skip the value
  17. } else {
  18. strippedArgs[trimmedArg] = true; // Store boolean flag
  19. }
  20. }
  21. return strippedArgs;
  22. }
  23. // Example usage (for testing - remove for production)
  24. if (require.main === module) {
  25. const args = process.argv.slice(2); // Get arguments from process.argv
  26. const stripped = stripMetadata(args);
  27. console.log(JSON.stringify(stripped, null, 2));
  28. }
  29. module.exports = stripMetadata;

Add your comment