args-parser.js 962 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. class ArgsParser {
  2. /**
  3. * parse plugin argument strings
  4. *
  5. * @static
  6. * @param {string} str
  7. * @returns {object} { fistArgsKey: 'key', firstArgsValue: 'val', options: {..} }
  8. */
  9. static parse(str) {
  10. let firstArgsKey = null;
  11. let firstArgsValue = null;
  12. const options = {};
  13. if (str != null && str.length > 0) {
  14. const splittedArgs = str.split(',');
  15. splittedArgs.forEach((rawArg, index) => {
  16. const arg = rawArg.trim();
  17. // parse string like 'key1=value1, key2=value2, ...'
  18. // see https://regex101.com/r/pYHcOM/1
  19. const match = arg.match(/([^=]+)=?(.+)?/);
  20. const key = match[1];
  21. const value = match[2] || true;
  22. options[key] = value;
  23. if (index === 0) {
  24. firstArgsKey = key;
  25. firstArgsValue = value;
  26. }
  27. });
  28. }
  29. return {
  30. firstArgsKey,
  31. firstArgsValue,
  32. options,
  33. };
  34. }
  35. }
  36. module.exports = ArgsParser;