Skip to content

Commit

Permalink
Simplified the error messages returned by .check().
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Ford committed Nov 23, 2013
1 parent 661c678 commit 4561ce6
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,11 @@ function Argv (processArgs, cwd) {

checks.forEach(function (f) {
try {
if (f(argv, aliases) === false) {
var result = f(argv, aliases);
if (result === false) {
fail('Argument check failed: ' + f.toString());
} else if (typeof result === 'string') {
fail(result);
}
}
catch (err) {
Expand Down
57 changes: 57 additions & 0 deletions test/usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,63 @@ test('checkFail', function (t) {
t.end();
});

test('checkFailReturn', function (t) {
var r = checkUsage(function () {
return optimist('-x 10 -z 20'.split(' '))
.usage('Usage: $0 -x NUM -y NUM')
.check(function (argv) {
if (!('x' in argv)) return 'You forgot about -x';
if (!('y' in argv)) return 'You forgot about -y';
})
.argv;
});

t.same(
r.result,
{ x : 10, z : 20, _ : [], $0 : './usage' }
);

t.same(
r.errors.join('\n').split(/\n+/),
[
'Usage: ./usage -x NUM -y NUM',
'You forgot about -y'
]
);

t.same(r.logs, []);
t.ok(r.exit);
t.end();
});

exports.checkFailReturn = function () {
var r = checkUsage(function () {
return optimist('-x 10 -z 20'.split(' '))
.usage('Usage: $0 -x NUM -y NUM')
.check(function (argv) {
if (!('x' in argv)) return 'You forgot about -x';
if (!('y' in argv)) return 'You forgot about -y';
})
.argv;
});

assert.deepEqual(
r.result,
{ x : 10, z : 20, _ : [], $0 : './usage' }
);

assert.deepEqual(
r.errors.join('\n').split(/\n+/),
[
'Usage: ./usage -x NUM -y NUM',
'You forgot about -y'
]
);

assert.deepEqual(r.logs, []);
assert.ok(r.exit);
};

test('checkCondPass', function (t) {
function checker (argv) {
return 'x' in argv && 'y' in argv;
Expand Down

0 comments on commit 4561ce6

Please sign in to comment.