Skip to content

Commit

Permalink
fix: context variables are now recognized in strict() mode (#796)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Feb 26, 2017
1 parent 49a93fc commit 48575cd
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
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 @@ -537,6 +537,10 @@ function Yargs (processArgs, cwd, parentRequire) {
return parsed
}

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

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

0 comments on commit 48575cd

Please sign in to comment.