showandtell
Version:
A Javascript library providing debugger-like command-line interactivity for program state inspection and modification
36 lines (30 loc) • 1.06 kB
JavaScript
/**
* This code is copied, with slight modification, from
* https://github.com/mccormicka/string-argv/blob/master/index.js
*
* Thanks to Github user mccormicka for their awesome work!
*/
function parseArgsStringToArgv (value) {
// ([^\s'"]+(['"])([^\2]*?)\2) Match `text"quotes text"`
// [^\s'"] or Match if not a space ' or "
// (['"])([^\4]*?)\4 or Match "quoted text" without quotes
// `\2` and `\4` are a backreference to the quote style (' or ") captured
let myRegexp = /([^\s'"]+(['"])([^\2]*?)\2)|[^\s'"]+|(['"])([^\4]*?)\4/gi
let myString = value
let myArray = []
let match = null
do {
// Each call to exec returns the next regex match as an array
match = myRegexp.exec(myString)
if (match !== null) {
// Index 1 in the array is the captured group if it exists
// Index 0 is the matched text, which we use if no captured group exists
myArray.push(match[1] || match[5] || match[0])
}
} while (match !== null)
return myArray
}
module.exports = {
parse: parseArgsStringToArgv
}