Skip to content

Commit

Permalink
Merge pull request #144 from drewbrokke/dev
Browse files Browse the repository at this point in the history
#143 - Allow for unknown options to be passed into commands
  • Loading branch information
dthree authored Jun 25, 2016
2 parents ced2267 + 4abd458 commit ff19d36
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 17 deletions.
18 changes: 18 additions & 0 deletions dist/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function Command(name, parent) {
this._help = undefined;
this._init = undefined;
this._after = undefined;
this._allowUnknownOptions = false;
}

/**
Expand Down Expand Up @@ -366,6 +367,23 @@ command.hidden = function () {
return this;
};

/**
* Allows undeclared options to be passed in with the command.
*
* @param {Boolean} [allowUnknownOptions=true]
* @return {Command}
* @api public
*/

command.allowUnknownOptions = function () {
var allowUnknownOptions = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0];

allowUnknownOptions = allowUnknownOptions === "false" ? false : allowUnknownOptions;

this._allowUnknownOptions = !!allowUnknownOptions;
return this;
};

/**
* Returns the command usage string for help.
*
Expand Down
14 changes: 10 additions & 4 deletions dist/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,9 @@ var util = {
}

// Looks for supplied options that don't
// exist in the options list and throws help
// exist in the options list.
// If the command allows unknown options,
// adds it, otherwise throws help.
var passedOpts = _.chain(parsedArgs).keys().pull('_').pull('help').value();

var _loop = function _loop(key) {
Expand All @@ -316,9 +318,13 @@ var util = {
return false;
});
if (optionFound === undefined) {
return {
v: '\n Invalid option: \'' + opt + '\'. Showing Help:'
};
if (cmd._allowUnknownOptions) {
args.options[opt] = parsedArgs[opt];
} else {
return {
v: '\n Invalid option: \'' + opt + '\'. Showing Help:'
};
}
}
};

Expand Down
16 changes: 16 additions & 0 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function Command(name, parent) {
this._help = undefined;
this._init = undefined;
this._after = undefined;
this._allowUnknownOptions = false;
}

/**
Expand Down Expand Up @@ -385,6 +386,21 @@ command.hidden = function () {
return this;
};

/**
* Allows undeclared options to be passed in with the command.
*
* @param {Boolean} [allowUnknownOptions=true]
* @return {Command}
* @api public
*/

command.allowUnknownOptions = function (allowUnknownOptions = true) {
allowUnknownOptions = allowUnknownOptions === "false" ? false : allowUnknownOptions;

this._allowUnknownOptions = !!allowUnknownOptions;
return this;
};

/**
* Returns the command usage string for help.
*
Expand Down
10 changes: 8 additions & 2 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,9 @@ const util = {
}

// Looks for supplied options that don't
// exist in the options list and throws help
// exist in the options list.
// If the command allows unknown options,
// adds it, otherwise throws help.
const passedOpts = _.chain(parsedArgs)
.keys()
.pull('_')
Expand All @@ -317,7 +319,11 @@ const util = {
return false;
});
if (optionFound === undefined) {
return `\n Invalid option: '${opt}'. Showing Help:`;
if (cmd._allowUnknownOptions) {
args.options[opt] = parsedArgs[opt];
} else {
return `\n Invalid option: '${opt}'. Showing Help:`;
}
}
}

Expand Down
52 changes: 41 additions & 11 deletions test/vorpal.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ vorpal
return args;
});

vorpal
.command('bar')
.allowUnknownOptions(true)
.action(function (args, cb) {
return args;
});

vorpal
.command('baz')
.allowUnknownOptions(true)
.allowUnknownOptions(false)
.action(function (args, cb) {
return args;
});

vorpal
.command('optional [str]')
.action(function (args, cb) {
Expand Down Expand Up @@ -84,41 +99,41 @@ describe('argument parsing', function () {

it('should execute a command with an optional arg', function () {
var fixture = obj({ options: {}, str: 'bar' });
obj(vorpal.execSync('optional bar')).should.equal(fixture);
obj(vorpal.execSync('optional bar')).should.equal(fixture);
});

it('should execute a command with a required arg', function () {
var fixture = obj({ options: {}, str: 'bar' });
obj(vorpal.execSync('required bar')).should.equal(fixture);
obj(vorpal.execSync('required bar')).should.equal(fixture);
});

it('should throw help when not passed a required arg', function () {
mute();
var fixture = '\n Missing required argument. Showing Help:';
vorpal.execSync('required').should.equal(fixture);
vorpal.execSync('required').should.equal(fixture);
unmute();
});

it('should execute a command with multiple arg types', function () {
var fixture = obj({ options: {}, req: 'foo', opt: 'bar', variadic: ['joe', 'smith'] });
obj(vorpal.execSync('multiple foo bar joe smith')).should.equal(fixture);
obj(vorpal.execSync('multiple foo bar joe smith')).should.equal(fixture);
});

it('should correct a command with wrong arg sequences declared', function () {
var fixture = obj({ options: {}, req: 'foo', opt: 'bar', variadic: ['joe', 'smith'] });
obj(vorpal.execSync('multiple foo bar joe smith')).should.equal(fixture);
obj(vorpal.execSync('multiple foo bar joe smith')).should.equal(fixture);
});

it('should normalize key=value pairs', function () {
var fixture = obj({ options: {},
req: "a='b'",
opt: "c='d and e'",
var fixture = obj({ options: {},
req: "a='b'",
opt: "c='d and e'",
variadic: ["wombat='true'","a","fizz='buzz'","hello='goodbye'"] });
obj(vorpal.execSync('multiple a=\'b\' c="d and e" wombat=true a fizz=\'buzz\' "hello=\'goodbye\'"')).should.equal(fixture);
obj(vorpal.execSync('multiple a=\'b\' c="d and e" wombat=true a fizz=\'buzz\' "hello=\'goodbye\'"')).should.equal(fixture);
});

it('should NOT normalize key=value pairs when isCommandArgKeyPairNormalized is false', function () {
var fixture = obj({ options: {},
var fixture = obj({ options: {},
req: "hello=world",
opt: 'hello="world"',
variadic: ['hello=`world`']
Expand All @@ -130,7 +145,7 @@ describe('argument parsing', function () {

it('should execute multi-word command with arguments', function () {
var fixture = obj({ options: {}, variadic: ['and', 'so', 'on'] });
obj(vorpal.execSync('multi word command and so on')).should.equal(fixture);
obj(vorpal.execSync('multi word command and so on')).should.equal(fixture);
});
});

Expand Down Expand Up @@ -292,6 +307,21 @@ describe('option parsing', function () {
vorpal.execSync('foo --no-required cows').should.equal(fixture);
unmute();
});

it('should throw help on an unknown option', function() {
var fixture = "\n Invalid option: 'unknown'. Showing Help:";
vorpal.execSync('foo --unknown').should.equal(fixture);
});

it('should allow unknown options when allowUnknownOptions is set to true', function() {
var fixture = obj({ options: { unknown: true }});
obj(vorpal.execSync('bar --unknown')).should.equal(fixture);
});

it('should allow the allowUnknownOptions state to be set with a boolean', function() {
var fixture = "\n Invalid option: 'unknown'. Showing Help:";
vorpal.execSync('baz --unknown').should.equal(fixture);
});
});
});

Expand Down

0 comments on commit ff19d36

Please sign in to comment.