Skip to content

Commit

Permalink
Merge pull request #25 from four43/feature-simpler-sub-commands
Browse files Browse the repository at this point in the history
Added tests for CommandGroup/Command callbacks to verify type, one wa…
  • Loading branch information
four43 committed May 8, 2016
2 parents 79bb04e + 97055d8 commit a1211a1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
6 changes: 3 additions & 3 deletions lib/Cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,13 @@ Cli.prototype.parse = function (args) {
}

//Parse Option
helpers.merge(this.params, Option.parse(argsToParse, this.options));
helpers.merge(this.params, Option.parse.call(this, argsToParse, this.options));

//Parse Command Groups
helpers.merge(this.params, CommandGroup.parse(argsToParse, this.commandGroups));
helpers.merge(this.params, CommandGroup.parse.call(this, argsToParse, this.commandGroups));

//Parse EnvVars
helpers.merge(this.env, EnvVar.parse(this.processEnv, this.envVars));
helpers.merge(this.env, EnvVar.parse.call(this, this.processEnv, this.envVars));

if (!this.allowExtraArgs) {
// parse methods should have removed all entries from argsToParse
Expand Down
52 changes: 50 additions & 2 deletions test/CommandTest.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var assert = require('assert'),
Cli = require('./../lib/Cli'),
CliError = require('./../lib/error/abstract-error'),
Command = require('./../lib/Command');

describe("Commands", function () {
Expand Down Expand Up @@ -73,6 +72,7 @@ describe("Commands", function () {
var cli = new Cli();

var resultCommand = {};
var resultCli = {};
var resultGroup = {};
cli
.commandGroup({
Expand All @@ -81,11 +81,13 @@ describe("Commands", function () {
commands: [
new Command({
name: 'test1', description: 'The first command option', callback: function (cli, command) {
resultCli = cli;
resultCommand = command;
}
}),
new Command({
name: 'test2', description: 'The second command option', callback: function (cli, command) {
resultCli = cli;
resultCommand = 'Hello World';
}
})
Expand All @@ -96,15 +98,61 @@ describe("Commands", function () {
})
.parse(['node', 'cli-test.js', 'test1']);
assert.equal(cli.params.cmd1, 'test1');
assert.ok(resultCli instanceof Cli, 'resultCli was not instance of Cli');
assert.ok(resultCommand instanceof Command, 'resultCommand was not instance of Command');
assert.equal(resultCommand.name, 'test1');
assert.equal(resultGroup.name, 'test1');

cli.parse(['node', 'cli-test.js', 'test2']);
assert.equal(cli.params.cmd1, 'test2');
assert.ok(resultCli instanceof Cli, 'resultCli was not instance of Cli');
assert.equal(resultCommand, 'Hello World');
assert.equal(resultGroup.name, 'test2');
});

it("Should make a tree of commands easily", function () {
var cli = new Cli();

var finalResult;
cli
.commandGroup({
name: 'cmd1',
description: 'main route for the program',
commands: [
new Command({
name: 'test1',
description: 'The first command option',
callback: function(cli, command) {
// Append additional subgroups when this one is chosen.
cli.commandGroup({
name: 'test1Sub',
description: 'the sub command to test1',
commands: [
new Command({
name: 'foo',
description: 'Foo should equal bar',
callback: function(cli, command) {
finalResult = 'bar';
}
})
]
});
}
}),
new Command({
name: 'test2',
description: 'The second command option'
})
],
required: true
});

cli.parse(['node', 'cli-test.js', 'test1', 'foo']);
assert.equal(cli.params.cmd1, 'test1');
assert.equal(cli.params.test1Sub, 'foo');
assert.equal(finalResult, 'bar');
});

it("Should error on extra command", function () {
var cli = new Cli();

Expand All @@ -120,6 +168,6 @@ describe("Commands", function () {

assert.throws(function () {
cli.parse(['node', 'cli-test.js', 'test3']);
});
}, 'Didn\'t throw error for extra params as it should');
});
});

0 comments on commit a1211a1

Please sign in to comment.