Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: positional arguments of sub-command #805

Merged
merged 2 commits into from Feb 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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