Skip to content

Commit

Permalink
fix: strict mode should not fail for hidden options (#949)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Sep 3, 2017
1 parent c240661 commit 0e0c58d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
14 changes: 2 additions & 12 deletions lib/validation.js
Expand Up @@ -120,26 +120,16 @@ module.exports = function validation (yargs, usage, y18n) {

// check for unknown arguments (strict-mode).
self.unknownArguments = function unknownArguments (argv, aliases, positionalMap) {
const aliasLookup = {}
const descriptions = usage.getDescriptions()
const demandedOptions = yargs.getDemandedOptions()
const commandKeys = yargs.getCommandInstance().getCommands()
const unknown = []
const currentContext = yargs.getContext()

Object.keys(aliases).forEach((key) => {
aliases[key].forEach((alias) => {
aliasLookup[alias] = key
})
})

Object.keys(argv).forEach((key) => {
if (specialKeys.indexOf(key) === -1 &&
!descriptions.hasOwnProperty(key) &&
!demandedOptions.hasOwnProperty(key) &&
!positionalMap.hasOwnProperty(key) &&
!yargs._getParseContext().hasOwnProperty(key) &&
!aliasLookup.hasOwnProperty(key)) {
!aliases.hasOwnProperty(key)
) {
unknown.push(key)
}
})
Expand Down
26 changes: 26 additions & 0 deletions test/validation.js
Expand Up @@ -724,6 +724,32 @@ describe('validation tests', () => {
})
.argv
})

it('does not fail for hidden options', () => {
const args = yargs('--foo hey')
.strict()
.option('foo', {boolean: true, describe: false})
.fail((msg) => {
expect.fail()
})
.argv
args.foo.should.equal(true)
})

it('does not fail if an alias is provided, rather than option itself', () => {
const args = yargs('--cat hey')
.strict()
.option('foo', {boolean: true, describe: false})
.alias('foo', 'bar')
.alias('bar', 'cat')
.fail((msg) => {
expect.fail()
})
.argv
args.cat.should.equal(true)
args.foo.should.equal(true)
args.bar.should.equal(true)
})
})

describe('demandOption', () => {
Expand Down

0 comments on commit 0e0c58d

Please sign in to comment.