Skip to content

Commit

Permalink
fix: positional arguments of sub-commands threw strict() exception (#805
Browse files Browse the repository at this point in the history
)
  • Loading branch information
bcoe committed Feb 26, 2017
1 parent 738f9c2 commit 221e5d9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
7 changes: 0 additions & 7 deletions .editorconfig

This file was deleted.

3 changes: 2 additions & 1 deletion lib/command.js
Expand Up @@ -224,9 +224,10 @@ module.exports = function (yargs, usage, validation) {

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

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

Expand Down
13 changes: 13 additions & 0 deletions test/command.js
Expand Up @@ -1024,6 +1024,19 @@ describe('Command', function () {
})
})

// address https://github.com/yargs/yargs/issues/795
it('does not fail strict check due to postional command arguments in nested commands', function (done) {
yargs()
.strict()
.command('hi', 'The hi command', function (yargs) {
yargs.command('ben <age>', 'ben command', function () {}, function () {})
})
.parse('hi ben 99', function (err, argv, output) {
expect(err).to.equal(null)
return done()
})
})

it('does not fire command if validation fails', function (done) {
var commandRun = false
yargs()
Expand Down
4 changes: 4 additions & 0 deletions yargs.js
Expand Up @@ -897,6 +897,10 @@ function Yargs (processArgs, cwd, parentRequire) {
return hasOutput
}

self._setHasOutput = function () {
hasOutput = true
}

var recommendCommands
self.recommendCommands = function (recommend) {
argsert('[boolean]', [recommend], arguments.length)
Expand Down

0 comments on commit 221e5d9

Please sign in to comment.