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: context variables are now recognized in strict() mode #796

Merged
merged 1 commit into from Feb 25, 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
1 change: 1 addition & 0 deletions lib/validation.js
Expand Up @@ -135,6 +135,7 @@ module.exports = function (yargs, usage, y18n) {
!descriptions.hasOwnProperty(key) &&
!demandedOptions.hasOwnProperty(key) &&
!positionalMap.hasOwnProperty(key) &&
!yargs._getParseContext().hasOwnProperty(key) &&
!aliasLookup.hasOwnProperty(key)) {
unknown.push(key)
}
Expand Down
37 changes: 37 additions & 0 deletions test/validation.js
Expand Up @@ -233,6 +233,43 @@ describe('validation tests', function () {
argv._[0].should.equal('koala')
})

// addresses: https://github.com/yargs/yargs/issues/791
it('should recognize context variables in strict mode', function (done) {
yargs()
.command('foo <y>')
.strict()
.parse('foo 99', {x: 33}, function (err, argv, output) {
expect(err).to.equal(null)
expect(output).to.equal('')
argv.y.should.equal(99)
argv.x.should.equal(33)
argv._.should.include('foo')
return done()
})
})

// addresses: https://github.com/yargs/yargs/issues/791
it('should recognize context variables in strict mode, when running sub-commands', function (done) {
yargs()
.command('request', 'request command', function (yargs) {
yargs
.command('get', 'sub-command')
.option('y', {
describe: 'y inner option'
})
})
.strict()
.parse('request get --y=22', {x: 33}, function (err, argv, output) {
expect(err).to.equal(null)
expect(output).to.equal('')
argv.y.should.equal(22)
argv.x.should.equal(33)
argv._.should.include('request')
argv._.should.include('get')
return done()
})
})

it('fails when a required argument is missing', function (done) {
yargs('-w 10 marsupial')
.demand(1, ['w', 'b'])
Expand Down
4 changes: 4 additions & 0 deletions yargs.js
Expand Up @@ -531,6 +531,10 @@ function Yargs (processArgs, cwd, parentRequire) {
return parsed
}

self._getParseContext = function () {
return parseContext || {}
}

self._hasParseCallback = function () {
return !!parseFn
}
Expand Down