Comparing version 0.1.2 to 0.1.3
@@ -12,5 +12,5 @@ var assert = require('assert'); | ||
var matchOption = function(args,option){ | ||
var optionMatches = function(args,option){ | ||
var bothFlags = option.flag && option.longFlag; | ||
var pattern = bothFlags ? '(?:' : ''; | ||
var pattern = '('; | ||
if(option.flag){ | ||
@@ -23,8 +23,11 @@ pattern += ('\\s-' + option.flag); | ||
} | ||
if(bothFlags){ | ||
pattern += ')'; | ||
} | ||
pattern += '(?:\\s|$)([^-]+)?'; | ||
pattern += ')'; | ||
pattern += '(?:\\s|$)([^-\\s]+)?'; | ||
var regex = new RegExp(pattern); | ||
var matches = args.join(' ').match(regex); | ||
return matches && matches[0] ? matches.splice(1) : matches; | ||
} | ||
var matchOption = function(args,option){ | ||
var matches = optionMatches(args,option); | ||
if(matches){ | ||
@@ -39,4 +42,5 @@ return matches[1] ? matches[1].trim() : true; | ||
if(command.name){ | ||
var regex = new RegExp('(\\s|^)' + command.name + '(\\s|$)'); | ||
return regex.test(args.join(' ')); | ||
var regex = new RegExp('(?:\\s|^)(' + command.name + ')(?:\\s|$)'); | ||
var matches = args.join(' ').match(regex); | ||
return matches && matches[1] ? matches[1].trim() : false; | ||
} else { | ||
@@ -140,2 +144,18 @@ return false; | ||
}; | ||
self.unmatchedArgs = function(args){ | ||
var argsString = Array.prototype.slice.call(args,2).join('##'); | ||
this._commands.forEach(function(command){ | ||
var match = matchCommand(args,command); | ||
if(match){ | ||
argsString = argsString.replace(match,''); | ||
(command.options || []).forEach(function(option){ | ||
var optionMatch = optionMatches(args,option); | ||
if(optionMatch){ | ||
argsString = argsString.replace(optionMatch.join('##').trim(),''); | ||
} | ||
}); | ||
} | ||
}); | ||
return argsString.split('##').filter(function(v){return v;}); | ||
} | ||
@@ -146,4 +166,5 @@ // methods | ||
self.parse = function(args,callback){ | ||
args = Array.prototype.slice.call(args,2); | ||
var matchedCommands = 0; | ||
async.forEach(self._commands,function(command,done){ | ||
async.forEachSeries(self._commands,function(command,done){ | ||
var matched = matchCommand(args,command); | ||
@@ -165,3 +186,3 @@ if(matched){ | ||
if(command.stop) return done(new Error('complete')); | ||
done(err); | ||
done(); | ||
}; | ||
@@ -168,0 +189,0 @@ command.run.apply(self,[optionMap].concat(self._baseArgs,complete)); |
{ | ||
"name": "cuisinart", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "A node js command line parser", | ||
@@ -5,0 +5,0 @@ "main": "cuisinart.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38296
21
1034