Skip to content

Commit

Permalink
Merge pull request #124 from bcoe/demand-msg-null
Browse files Browse the repository at this point in the history
slight tweak to logic, simply echo anything other than `undefined`
  • Loading branch information
bcoe committed Mar 12, 2015
2 parents 870b428 + d28baf1 commit 6411a9e
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 40 deletions.
8 changes: 2 additions & 6 deletions lib/usage.js
Expand Up @@ -34,14 +34,10 @@ module.exports = function (yargs) {
f(msg);
});
} else {
if (showHelpOnFail) {
yargs.showHelp("error");
}
if (showHelpOnFail) yargs.showHelp("error");
if (msg) console.error(msg);
if (failMessage) {
if (msg) {
console.error("");
}
if (msg) console.error("");
console.error(failMessage);
}
if (yargs.getExitProcess()){
Expand Down
2 changes: 1 addition & 1 deletion lib/validation.js
Expand Up @@ -9,7 +9,7 @@ module.exports = function (yargs, usage) {
var demanded = yargs.getDemanded();

if (demanded._ && argv._.length < demanded._.count) {
if (demanded._.msg) {
if (demanded._.msg !== undefined) {
usage.fail(demanded._.msg);
} else {
usage.fail('Not enough non-option arguments: got '
Expand Down
16 changes: 16 additions & 0 deletions test/usage.js
Expand Up @@ -95,6 +95,22 @@ describe('usage tests', function () {
r.should.have.property('logs').with.length(0);
r.should.have.property('exit', false);
});


it('should not show a custom message if msg is null', function() {
var r = checkUsage(function() {
return yargs('')
.usage('Usage: foo')
.demand(1, null)
.argv
;
});

r.errors.join('\n').split(/\n+/).should.deep.equal([
'Usage: foo',
''
]);
});
});

it('should return valid values when check passes', function () {
Expand Down
89 changes: 56 additions & 33 deletions test/validation.js
@@ -1,4 +1,5 @@
var should = require('chai').should(),
expect = require('chai').expect,
checkValidation = require('./helpers/utils').checkOutput,
Hash = require('hashish'),
yargs = require('../');
Expand All @@ -12,51 +13,73 @@ describe('validation tests', function () {
describe('implies', function() {
it("fails if '_' populated, and implied argument not set", function(done) {
var argv = yargs(['cat'])
.implies({
1: 'foo'
})
.fail(function(msg) {
msg.should.match(/Implications failed/);
return done();
})
.argv;
.implies({
1: 'foo'
})
.fail(function(msg) {
msg.should.match(/Implications failed/);
return done();
})
.argv;
});

it("fails if key implies values in '_', but '_' is not populated", function(done) {
var argv = yargs(['--foo'])
.boolean('foo')
.implies({
'foo': 1
})
.fail(function(msg) {
msg.should.match(/Implications failed/);
return done();
})
.argv;
.boolean('foo')
.implies({
'foo': 1
})
.fail(function(msg) {
msg.should.match(/Implications failed/);
return done();
})
.argv;
});

it("fails if --no-foo's implied argument is not set", function(done) {
var argv = yargs([])
.implies({
'--no-bar': 'foo'
})
.fail(function(msg) {
msg.should.match(/Implications failed/);
return done();
})
.argv;
.implies({
'--no-bar': 'foo'
})
.fail(function(msg) {
msg.should.match(/Implications failed/);
return done();
})
.argv;
});

it("fails if a key is set, along with a key that it implies should not be set", function(done) {
var argv = yargs(['--bar', '--foo'])
.implies({
'bar': '--no-foo'
})
.fail(function(msg) {
msg.should.match(/Implications failed/);
return done();
})
.argv;
.implies({
'bar': '--no-foo'
})
.fail(function(msg) {
msg.should.match(/Implications failed/);
return done();
})
.argv;
});
});

describe('demand', function() {
it('fails with standard error message if msg is not defined', function(done) {
var argv = yargs([])
.demand(1)
.fail(function(msg) {
msg.should.equal('Not enough non-option arguments: got 0, need at least 1');
return done();
})
.argv;
});

it('fails without a message if msg is null', function(done) {
var argv = yargs([])
.demand(1, null)
.fail(function(msg) {
expect(msg).to.equal(null);
return done();
})
.argv;
});
});
});

0 comments on commit 6411a9e

Please sign in to comment.