tokenize-arg-string.js 724 B

12345678910111213141516171819202122232425262728293031323334
  1. // take an un-split argv string and tokenize it.
  2. module.exports = function (argString) {
  3. if (Array.isArray(argString)) return argString
  4. var i = 0
  5. var c = null
  6. var opening = null
  7. var args = []
  8. for (var ii = 0; ii < argString.length; ii++) {
  9. c = argString.charAt(ii)
  10. // split on spaces unless we're in quotes.
  11. if (c === ' ' && !opening) {
  12. i++
  13. continue
  14. }
  15. // don't split the string if we're in matching
  16. // opening or closing single and double quotes.
  17. if (c === opening) {
  18. opening = null
  19. continue
  20. } else if ((c === "'" || c === '"') && !opening) {
  21. opening = c
  22. continue
  23. }
  24. if (!args[i]) args[i] = ''
  25. args[i] += c
  26. }
  27. return args
  28. }