parse-command.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.parseCommand = void 0;
  4. function parseCommand(cmd) {
  5. const extraSpacesStrippedCommand = cmd.replace(/\s{2,}/g, ' ');
  6. const splitCommand = extraSpacesStrippedCommand.split(/\s+(?![^[]*]|[^<]*>)/);
  7. const bregex = /\.*[\][<>]/g;
  8. const firstCommand = splitCommand.shift();
  9. if (!firstCommand)
  10. throw new Error(`No command found in: ${cmd}`);
  11. const parsedCommand = {
  12. cmd: firstCommand.replace(bregex, ''),
  13. demanded: [],
  14. optional: []
  15. };
  16. splitCommand.forEach((cmd, i) => {
  17. let variadic = false;
  18. cmd = cmd.replace(/\s/g, '');
  19. if (/\.+[\]>]/.test(cmd) && i === splitCommand.length - 1)
  20. variadic = true;
  21. if (/^\[/.test(cmd)) {
  22. parsedCommand.optional.push({
  23. cmd: cmd.replace(bregex, '').split('|'),
  24. variadic
  25. });
  26. }
  27. else {
  28. parsedCommand.demanded.push({
  29. cmd: cmd.replace(bregex, '').split('|'),
  30. variadic
  31. });
  32. }
  33. });
  34. return parsedCommand;
  35. }
  36. exports.parseCommand = parseCommand;