Skip to content

Commit

Permalink
Added .strict() method, to report error if unknown arguments are given
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisn committed Jun 16, 2014
1 parent b61c69e commit eb16369
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,12 @@ is loaded and parsed, and its properties are set as arguments.

Format usage output to wrap at `columns` many columns.

.strict()
---------

Any command-line argument given that is not demanded, or does not have a
corresponding description, will be reported as an error.

.help()
-------

Expand Down
19 changes: 19 additions & 0 deletions example/strict.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var yargs = require('yargs');

var argv = yargs.usage('This is my awesome program', {
'about': {
description: 'Provide some details about the author of this program',
boolean: true,
short: 'a',
},
'info': {
description: 'Provide some information about this program',
boolean: true,
short: 'i'
}
}).strict().argv;

yargs.showHelp();

console.log('\n\nInspecting options');
console.dir(argv);
36 changes: 35 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ function Argv (processArgs, cwd) {
wrap = cols;
return self;
};

var strict = false;
self.strict = function () {
strict = true;
return self;
};

self.showHelp = function (fn) {
if (!fn) fn = console.error.bind(console);
Expand Down Expand Up @@ -406,7 +412,35 @@ function Argv (processArgs, cwd) {

fail('Missing required arguments: ' + Object.keys(missing).join(', ') + customMsg);
}


if (strict) {
var unknown = [];

var aliases = {};

Object.keys(options.alias).forEach(function (key) {
options.alias[key].forEach(function (alias) {
aliases[alias] = key;
});
});

Object.keys(argv).forEach(function (key) {
if (key !== "$0" && key !== "_" &&
!descriptions.hasOwnProperty(key) &&
!demanded.hasOwnProperty(key) &&
!aliases.hasOwnProperty(key)) {
unknown.push(key);
}
});

if (unknown.length == 1) {
fail("Unknown argument: " + unknown[0]);
}
else if (unknown.length > 1) {
fail("Unknown arguments: " + unknown.join(", "));
}
}

checks.forEach(function (f) {
try {
var result = f(argv, aliases);
Expand Down
121 changes: 121 additions & 0 deletions test/usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,127 @@ describe('usage', function () {
r.result.should.have.property('quux', 30);
});

context("with strict() option set", function () {
it('should fail given an option argument that is not demanded', function () {
var r = checkUsage(function () {
opts = {
foo: { demand: 'foo option', alias: 'f' },
bar: { demand: 'bar option', alias: 'b' }
};

return yargs('-f 10 --bar 20 --baz 30'.split(' '))
.usage('Usage: $0 [options]', opts)
.strict()
.argv;
});

r.should.have.property('result');
r.result.should.have.property('_').with.length(0);
r.result.should.have.property('f', 10);
r.result.should.have.property('foo', 10);
r.result.should.have.property('b', 20);
r.result.should.have.property('bar', 20);
r.result.should.have.property('baz', 30);
r.should.have.property('errors');
r.errors.join('\n').split(/\n+/).should.deep.equal([
'Usage: ./usage [options]',
'Options:',
' --foo, -f [required]',
' --bar, -b [required]',
'Unknown argument: baz',
]);
r.should.have.property('logs').with.length(0);
r.should.have.property('exit').and.be.ok;
});

it('should fail given an option argument without a corresponding description', function () {
var r = checkUsage(function () {
opts = {
foo: { description: 'foo option', alias: 'f' },
bar: { description: 'bar option', alias: 'b' }
};

return yargs('-f 10 --bar 20 --baz 30'.split(' '))
.usage('Usage: $0 [options]', opts)
.strict()
.argv;
});

r.should.have.property('result');
r.result.should.have.property('_').with.length(0);
r.result.should.have.property('f', 10);
r.result.should.have.property('foo', 10);
r.result.should.have.property('b', 20);
r.result.should.have.property('bar', 20);
r.result.should.have.property('baz', 30);
r.should.have.property('errors');
r.errors.join('\n').split(/\n+/).should.deep.equal([
'Usage: ./usage [options]',
'Options:',
' --foo, -f foo option',
' --bar, -b bar option',
'Unknown argument: baz',
]);
r.should.have.property('logs').with.length(0);
r.should.have.property('exit').and.be.ok;
});

it('should fail given multiple option arguments without corresponding descriptions', function () {
var r = checkUsage(function () {
opts = {
foo: { description: 'foo option', alias: 'f' },
bar: { description: 'bar option', alias: 'b' }
};

return yargs('-f 10 --bar 20 --baz 30 -q 40'.split(' '))
.usage('Usage: $0 [options]', opts)
.strict()
.argv;
});

r.should.have.property('result');
r.result.should.have.property('_').with.length(0);
r.result.should.have.property('f', 10);
r.result.should.have.property('foo', 10);
r.result.should.have.property('b', 20);
r.result.should.have.property('bar', 20);
r.result.should.have.property('baz', 30);
r.result.should.have.property('q', 40);
r.should.have.property('errors');
r.errors.join('\n').split(/\n+/).should.deep.equal([
'Usage: ./usage [options]',
'Options:',
' --foo, -f foo option',
' --bar, -b bar option',
'Unknown arguments: baz, q',
]);
r.should.have.property('logs').with.length(0);
r.should.have.property('exit').and.be.ok;
});

it('should pass given option arguments with corresponding descriptions', function () {
var r = checkUsage(function () {
opts = {
foo: { description: 'foo option' },
bar: { description: 'bar option' }
};

return yargs('--foo 10 --bar 20'.split(' '))
.usage('Usage: $0 [options]', opts)
.strict()
.argv;
});

r.should.have.property('result');
r.result.should.have.property('foo', 10);
r.result.should.have.property('bar', 20)
r.result.should.have.property('_').with.length(0);
r.should.have.property('errors').with.length(0);
r.should.have.property('logs').with.length(0);
r.should.have.property('exit', false);
});
});

it('should display example on fail', function () {
var r = checkUsage(function () {
return yargs('')
Expand Down

0 comments on commit eb16369

Please sign in to comment.