Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions lib/yargs-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,17 @@ export class YargsParser {
const key = typeof opt === 'object' ? opt.key : opt

// assign to flags[bools|strings|numbers]
const assignment: ArrayFlagsKey | undefined = Object.keys(opt).map(function (key) {
const arrayFlagKeys: Record<string, ArrayFlagsKey> = {
boolean: 'bools',
string: 'strings',
number: 'numbers'
}
return arrayFlagKeys[key]
}).filter(Boolean).pop()
const arrayFlagKeys: Record<string, ArrayFlagsKey> = {
boolean: 'bools',
string: 'strings',
number: 'numbers'
}
const assignment: ArrayFlagsKey | undefined = typeof opt === 'object'
? Object.keys(arrayFlagKeys)
.filter((flagKey) => (opt as Record<string, unknown>)[flagKey] === true)
.map((flagKey) => arrayFlagKeys[flagKey])
.pop()
: undefined

// assign key to be coerced
if (assignment) {
Expand Down
31 changes: 31 additions & 0 deletions test/yargs-parser.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1877,6 +1877,37 @@ describe('yargs-parser', function () {
result.should.have.property('x').that.is.an('array').and.to.deep.equal(['5', '2'])
})

// see https://github.com/yargs/yargs-parser/issues/415
// a falsy type flag should be treated as if it were absent, rather than
// enabling the corresponding coercion.
it('should not coerce array values to number when `number` is false', function () {
const result = parser(['--foo', 'dog', 'cat'], {
array: [{ key: 'foo', number: false }]
})
result.should.have.property('foo').that.is.an('array').and.to.deep.equal(['dog', 'cat'])
})

it('should not coerce array values to number when `number` is undefined', function () {
const result = parser(['--foo', 'dog', 'cat'], {
array: [{ key: 'foo', number: undefined }]
})
result.should.have.property('foo').that.is.an('array').and.to.deep.equal(['dog', 'cat'])
})

it('should not coerce array values to boolean when `boolean` is false', function () {
const result = parser(['--foo', 'dog', 'cat'], {
array: [{ key: 'foo', boolean: false }]
})
result.should.have.property('foo').that.is.an('array').and.to.deep.equal(['dog', 'cat'])
})

it('should not coerce array values to string when `string` is false', function () {
const result = parser(['--foo', '1', '2'], {
array: [{ key: 'foo', string: false }]
})
result.should.have.property('foo').that.is.an('array').and.to.deep.equal([1, 2])
})

it('should eat non-hyphenated arguments until hyphenated option is hit - combined with coercion', function () {
const result = parser([
'-a=hello', 'world',
Expand Down
Loading