Skip to content

Commit

Permalink
fix: we should apply validation prior to executing a command's handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Coe committed Feb 18, 2017
1 parent c18722f commit 5801b79
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/command.js
Expand Up @@ -178,14 +178,14 @@ module.exports = function (yargs, usage, validation) {
positionalMap = populatePositionals(commandHandler, innerArgv, currentContext, yargs)
}

if (commandHandler.handler && !yargs._hasOutput()) {
commandHandler.handler(innerArgv)
}

// we apply validation post-hoc, so that custom
// checks get passed populated positional arguments.
yargs._runValidation(innerArgv, aliases, positionalMap)

if (commandHandler.handler && !yargs._hasOutput()) {
commandHandler.handler(innerArgv)
}

currentContext.commands.pop()
numFiles = currentContext.files.length - numFiles
if (numFiles > 0) currentContext.files.splice(numFiles * -1, numFiles)
Expand Down
14 changes: 14 additions & 0 deletions test/command.js
Expand Up @@ -997,6 +997,20 @@ describe('Command', function () {
return done()
})
})

it('does not fire command if validation fails', function (done) {
var commandRun = false
yargs()
.strict()
.command('hi <name>', 'The hi command', function () {}, function (argv) {
commandRun = true
})
.parse('hi ben --hello=world', function (err, argv, output) {
commandRun.should.equal(false)
err.message.should.equal('Unknown argument: hello')
return done()
})
})
})

describe('types', function () {
Expand Down

0 comments on commit 5801b79

Please sign in to comment.