From b34bf58973c737259d6e53c8e3e88872802d6982 Mon Sep 17 00:00:00 2001 From: dthree Date: Thu, 6 Aug 2015 17:04:19 -0700 Subject: [PATCH] variadic argument support :fire: --- lib/vantage-commons.js | 1 - lib/vantage.js | 63 +++++++++++++++++++++++++++++------------- test/integration.js | 21 ++++++++++++-- test/util/server.js | 15 +++++++--- 4 files changed, 73 insertions(+), 27 deletions(-) diff --git a/lib/vantage-commons.js b/lib/vantage-commons.js index 82f3dc8..65c214e 100755 --- a/lib/vantage-commons.js +++ b/lib/vantage-commons.js @@ -25,7 +25,6 @@ module.exports = function(vantage) { .command("help [command]") .description("Provides help for a given command.") .action(function(args, cb) { - if (args.command) { var name = _.findWhere(this.parent.commands, { _name: String(args.command).toLowerCase().trim() }); if (name && !name._hidden) { diff --git a/lib/vantage.js b/lib/vantage.js index 1ec551d..1083536 100755 --- a/lib/vantage.js +++ b/lib/vantage.js @@ -304,16 +304,18 @@ vantage.command = function(name, desc, opts) { opts = opts || {}; name = String(name); - var args = - (name.indexOf("[") > -1) ? name.split("[") : - (name.indexOf("<") > -1) ? name.split("<") : [name]; + var argsRegExp = /(\[[^\]]*\]|\<[^\>]*\>)/g; + var args = []; + var arg; - if (args[1]) { - args[1] = (String(args[1]).indexOf("]") > -1) ? "[" + args[1] : args[1]; - args[1] = (String(args[1]).indexOf(">") > -1) ? "<" + args[1] : args[1]; + while ((arg = argsRegExp.exec(name)) != null) { + args.push(arg[1]); } - var cmd = new Command(String(args.shift()).trim(), exports); + var cmdNameRegExp = /^([^\[\<]*)/; + var cmdName = cmdNameRegExp.exec(name)[0].trim(); + var cmd = new Command(cmdName, exports); + if (desc) { cmd.description(desc); this.executables = true; @@ -732,11 +734,6 @@ vantage._exec = function(item) { } } - // This basically makes the arguments human readable. - var parsedArgs = minimist(VantageUtil.parseArgs(args)); - parsedArgs._ = parsedArgs._ || []; - args = {}; - // Match means we found a suitable command. if (match) { @@ -746,30 +743,58 @@ vantage._exec = function(item) { , delimiter = match._delimiter || String(item.command).toLowerCase() + ":" , origArgs = match._args , origOptions = match.options + , variadic = _.findWhere(origArgs, { variadic: true }) ; + // This basically makes the arguments human readable. + var parsedArgs = minimist(VantageUtil.parseArgs(args)); + + if (parsedArgs._.length > 1) { + //if (!variadic) { + //item.session.log(match.helpInformation()); + //item.callback(); return; + //} + } + + parsedArgs._ = parsedArgs._ || []; + args = {}; args.options = {}; // Looks for a help arg and throws help if any. if (parsedArgs.help || parsedArgs._.indexOf("/?") > -1) { - if (!match._hidden) { - item.session.log(match.helpInformation()); - } + item.session.log(match.helpInformation()); item.callback(); return; } // looks for ommitted required args // and throws help. - for (var l = 0; l < origArgs.length; ++l) { - var exists = parsedArgs._[l]; - if (!exists && origArgs[l].required === true) { + if (origArgs[0]) { + var exists = parsedArgs._[0]; + if (!exists && origArgs[0].required === true) { item.session.log(" "); item.session.log(" Missing required argument. Showing Help:"); item.session.log(match.helpInformation()); item.callback(); return; } if (exists) { - args[origArgs[l].name] = exists; + args[origArgs[0].name] = exists; + } + } + + // looks for ommitted required variadic args + // and throws help. + if (origArgs[1]) { + var exists = parsedArgs._[1]; + if (!exists && origArgs[1].required === true) { + item.session.log(" "); + item.session.log(" Missing required argument. Showing Help:"); + item.session.log(match.helpInformation()); + item.callback(); return; + } + if (exists && origArgs[1].variadic === true) { + var variadicList = _.clone(parsedArgs._); + variadicList.shift(); + args[origArgs[1].name] = variadicList; } } diff --git a/test/integration.js b/test/integration.js index 02cb036..e92711e 100755 --- a/test/integration.js +++ b/test/integration.js @@ -112,8 +112,8 @@ describe("integration tests:", function() { }); var exec = function(cmd, done, cb) { - vantage.exec(cmd).then(function(){ - cb(); + vantage.exec(cmd).then(function(data){ + cb(void 0, data); }).catch(function(err){ console.log(err); done(err); @@ -251,7 +251,6 @@ describe("integration tests:", function() { vantage.exec("count " + i).then(hnFn).catch(cFn); } }); - }); describe("command validation", function() { @@ -270,6 +269,22 @@ describe("integration tests:", function() { }); }); + it("should ignore variadic arguments when not warranted", function(done) { + exec("required something with extra something", done, function(err, data) { + data.arg.should.equal("something"); + done(); + }); + }); + + it("should receive variadic arguments as array", function(done) { + exec("variadic pepperoni olives pineapple anchovies", done, function(err, data) { + data.pizza.should.equal("pepperoni"); + data.ingredients[0].should.equal("olives"); + data.ingredients[1].should.equal("pineapple"); + data.ingredients[2].should.equal("anchovies"); + done(); + }); + }); it("should show help when not passed a required variable", function(done) { exec("required", done, function() { diff --git a/test/util/server.js b/test/util/server.js index 0e68114..50db5da 100755 --- a/test/util/server.js +++ b/test/util/server.js @@ -41,6 +41,15 @@ module.exports = function(vantage) { }); }); + vantage + .command('variadic [pizza] [ingredients...]') + .description('Should optionally return an arg.') + .option('-e, --extra', 'Extra complexity on the place.') + .action(function(args, cb){ + var self = this; + cb(void 0, args); + }); + vantage .command('port') .description('Returns port.') @@ -63,10 +72,8 @@ module.exports = function(vantage) { .description('Must return an arg.') .action(function(args, cb){ var self = this; - return new Promise(function(resolve, reject){ - self.log(args.arg); - resolve(); - }); + self.log(args.arg); + cb(void 0, args); });