Jay Traband 6f6e83722e squashed commits for PR 222 from jtraband
- add warning when bad example tag is encountered.
- shredding for api docs + refactoring of gulpfile to clarify task names.
- added top level gulp tasks + error handling cleanup
- added test-api-builder gulp task
- added warning messages during build-shred-map processing
- mixin signature change
- updated styleguide ... continued.
- added @exampleTabs inlinetag support + refactoring shared services between @example and @exampleTabs
- added new inline tag @linkDevGuide + cleanup
- added angular source watch logic for serve-and-sync and source for examples changed from test -> examples + cleanup
- promisify del
- styleguide explanation of @example, @exampleTabs and @linkDevGuide
2015-09-23 14:04:58 -07:00

55 lines
1.6 KiB
JavaScript

/**
* @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);
// remove leading '-' if it exists.
if (key.substr(0, 1) == '-') {
key = key.substr(1);
}
} else {
unnammedArgs.push(arg)
key = null;
}
}
}
} while (match !== null);
return args;
}
}