Skip to content

Commit

Permalink
variadic argument support 🔥
Browse files Browse the repository at this point in the history
  • Loading branch information
dthree committed Aug 7, 2015
1 parent f52f163 commit b34bf58
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 27 deletions.
1 change: 0 additions & 1 deletion lib/vantage-commons.js
Expand Up @@ -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) {
Expand Down
63 changes: 44 additions & 19 deletions lib/vantage.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {

Expand All @@ -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;
}
}

Expand Down
21 changes: 18 additions & 3 deletions test/integration.js
Expand Up @@ -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);
Expand Down Expand Up @@ -251,7 +251,6 @@ describe("integration tests:", function() {
vantage.exec("count " + i).then(hnFn).catch(cFn);
}
});

});

describe("command validation", function() {
Expand All @@ -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() {
Expand Down
15 changes: 11 additions & 4 deletions test/util/server.js
Expand Up @@ -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.')
Expand All @@ -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);
});


Expand Down

0 comments on commit b34bf58

Please sign in to comment.