2017-01-26 09:03:53 -05:00
|
|
|
/**
|
|
|
|
* @dgService parseArgString
|
|
|
|
* @description
|
|
|
|
* processes an arg string in 'almost' the same fashion that the command processor does
|
|
|
|
* and returns an args object in yargs format.
|
|
|
|
* @kind function
|
|
|
|
* @param {String} str The arg string to process
|
|
|
|
* @return {Object} The args parsed into a yargs format.
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = function parseArgString() {
|
|
|
|
|
|
|
|
return function parseArgStringImpl(str) {
|
|
|
|
// regex from npm string-argv
|
|
|
|
//[^\s'"] Match if not a space ' or "
|
|
|
|
|
|
|
|
//+|['] or Match '
|
|
|
|
//([^']*) Match anything that is not '
|
|
|
|
//['] Close match if '
|
|
|
|
|
|
|
|
//+|["] or Match "
|
|
|
|
//([^"]*) Match anything that is not "
|
|
|
|
//["] Close match if "
|
|
|
|
var rx = /[^\s'"]+|[']([^']*?)[']|["]([^"]*?)["]/gi;
|
|
|
|
var value = str;
|
|
|
|
var unnammedArgs = [];
|
|
|
|
var args = {_: unnammedArgs};
|
|
|
|
var match, key;
|
|
|
|
do {
|
|
|
|
// Each call to exec returns the next regex match as an array
|
|
|
|
match = rx.exec(value);
|
|
|
|
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
|
|
|
|
var arg = match[2] ? match[2] : (match[1] ? match[1] : match[0]);
|
|
|
|
if (key) {
|
|
|
|
args[key] = arg;
|
|
|
|
key = null;
|
|
|
|
} else {
|
|
|
|
if (arg.substr(arg.length - 1) === '=') {
|
|
|
|
key = arg.substr(0, arg.length - 1);
|
2017-03-22 16:24:40 -04:00
|
|
|
// remove leading '-' (or '--') if it exists.
|
|
|
|
if (key.substr(0, 1) == '-') {
|
|
|
|
key = key.substr(1);
|
|
|
|
}
|
2017-01-26 09:03:53 -05:00
|
|
|
if (key.substr(0, 1) == '-') {
|
|
|
|
key = key.substr(1);
|
|
|
|
}
|
|
|
|
} else {
|
2017-04-01 14:34:10 -04:00
|
|
|
unnammedArgs.push(arg);
|
2017-01-26 09:03:53 -05:00
|
|
|
key = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (match !== null);
|
|
|
|
return args;
|
2017-04-01 14:34:10 -04:00
|
|
|
};
|
|
|
|
};
|