Skip to content

Commit

Permalink
fix: strict() should not ignore hyphenated arguments (#1414)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewdotn authored and bcoe committed Aug 30, 2019
1 parent 434def5 commit b774b5e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
17 changes: 16 additions & 1 deletion lib/validation.js
Expand Up @@ -96,7 +96,7 @@ module.exports = function validation (yargs, usage, y18n) {
if (specialKeys.indexOf(key) === -1 &&
!positionalMap.hasOwnProperty(key) &&
!yargs._getParseContext().hasOwnProperty(key) &&
!aliases.hasOwnProperty(key)
!self.isValidAndSomeAliasIsNotNew(key, aliases)
) {
unknown.push(key)
}
Expand All @@ -120,6 +120,21 @@ module.exports = function validation (yargs, usage, y18n) {
}
}

// check for a key that is not an alias, or for which every alias is new,
// implying that it was invented by the parser, e.g., during camelization
self.isValidAndSomeAliasIsNotNew = function isValidAndSomeAliasIsNotNew (key, aliases) {
if (!aliases.hasOwnProperty(key)) {
return false
}
const newAliases = yargs.parsed.newAliases
for (let a of [key, ...aliases[key]]) {
if (!newAliases.hasOwnProperty(a) || !newAliases[key]) {
return true
}
}
return false
}

// validate arguments limited to enumerated choices
self.limitedChoices = function limitedChoices (argv) {
const options = yargs.getOptions()
Expand Down
69 changes: 69 additions & 0 deletions test/usage.js
Expand Up @@ -810,6 +810,75 @@ describe('usage tests', () => {
r.should.have.property('exit').and.equal(true)
})

describe('with hyphens in options', () => {
it('fails when an invalid argument is provided', (done) => {
return yargs('--foo-bar')
.strict()
.fail((msg) => {
return done()
})
.argv
})

it('accepts valid options', () => {
const r = checkUsage(() => {
const opts = {
'--foo-bar': { description: 'foo bar option' },
'--bar-baz': { description: 'bar baz option' }
}

return yargs('--foo-bar --bar-baz')
.options(opts)
.strict()
.parse()
})

r.result.should.have.property('foo-bar', true)
r.result.should.have.property('fooBar', true)
r.result.should.have.property('bar-baz', true)
r.result.should.have.property('barBaz', true)
})

it('works with aliases', () => {
const r = checkUsage(() => {
const opts = {
'--foo-bar': { description: 'foo bar option', alias: 'f' },
'--bar-baz': { description: 'bar baz option', alias: 'b' }
}

return yargs('--foo-bar -b')
.options(opts)
.strict()
.parse()
})

r.result.should.have.property('foo-bar', true)
r.result.should.have.property('fooBar', true)
r.result.should.have.property('f', true)
r.result.should.have.property('bar-baz', true)
r.result.should.have.property('barBaz', true)
r.result.should.have.property('b', true)
})

it('accepts mixed options with values', () => {
const r = checkUsage(() => {
const opts = {
'--foo-bar': { description: 'foo bar option', demand: true },
'--baz': { description: 'baz option', demand: true }
}

return yargs('--foo-bar 150 --baz')
.options(opts)
.strict()
.parse()
})

r.result.should.have.property('foo-bar', 150)
r.result.should.have.property('fooBar', 150)
r.result.should.have.property('baz', true)
})
})

it('should fail given an option argument without a corresponding description', () => {
const r = checkUsage(() => {
const opts = {
Expand Down

0 comments on commit b774b5e

Please sign in to comment.