Skip to content

Commit 48575cd

Browse files
committed
fix: context variables are now recognized in strict() mode (#796)
1 parent 49a93fc commit 48575cd

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

lib/validation.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ module.exports = function (yargs, usage, y18n) {
135135
!descriptions.hasOwnProperty(key) &&
136136
!demandedOptions.hasOwnProperty(key) &&
137137
!positionalMap.hasOwnProperty(key) &&
138+
!yargs._getParseContext().hasOwnProperty(key) &&
138139
!aliasLookup.hasOwnProperty(key)) {
139140
unknown.push(key)
140141
}

test/validation.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,43 @@ describe('validation tests', function () {
233233
argv._[0].should.equal('koala')
234234
})
235235

236+
// addresses: https://github.com/yargs/yargs/issues/791
237+
it('should recognize context variables in strict mode', function (done) {
238+
yargs()
239+
.command('foo <y>')
240+
.strict()
241+
.parse('foo 99', {x: 33}, function (err, argv, output) {
242+
expect(err).to.equal(null)
243+
expect(output).to.equal('')
244+
argv.y.should.equal(99)
245+
argv.x.should.equal(33)
246+
argv._.should.include('foo')
247+
return done()
248+
})
249+
})
250+
251+
// addresses: https://github.com/yargs/yargs/issues/791
252+
it('should recognize context variables in strict mode, when running sub-commands', function (done) {
253+
yargs()
254+
.command('request', 'request command', function (yargs) {
255+
yargs
256+
.command('get', 'sub-command')
257+
.option('y', {
258+
describe: 'y inner option'
259+
})
260+
})
261+
.strict()
262+
.parse('request get --y=22', {x: 33}, function (err, argv, output) {
263+
expect(err).to.equal(null)
264+
expect(output).to.equal('')
265+
argv.y.should.equal(22)
266+
argv.x.should.equal(33)
267+
argv._.should.include('request')
268+
argv._.should.include('get')
269+
return done()
270+
})
271+
})
272+
236273
it('fails when a required argument is missing', function (done) {
237274
yargs('-w 10 marsupial')
238275
.demand(1, ['w', 'b'])

yargs.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,10 @@ function Yargs (processArgs, cwd, parentRequire) {
537537
return parsed
538538
}
539539

540+
self._getParseContext = function () {
541+
return parseContext || {}
542+
}
543+
540544
self._hasParseCallback = function () {
541545
return !!parseFn
542546
}

0 commit comments

Comments
 (0)