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
8 changes: 5 additions & 3 deletions lib/yargs-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,13 +620,13 @@ export class YargsParser {
if (typeof val === 'string') val = val === 'true'
}

let value = Array.isArray(val)
let value: any = Array.isArray(val)
? val.map(function (v) { return maybeCoerceNumber(key, v) })
: maybeCoerceNumber(key, val)

// increment a count given as arg (either no value or value parsed as boolean)
if (checkAllAliases(key, flags.counts) && (isUndefined(value) || typeof value === 'boolean')) {
value = increment()
value = INCREMENT_SENTINEL
}

// Set normalized value when key is in 'normalize' and in 'arrays'
Expand Down Expand Up @@ -850,7 +850,7 @@ export class YargsParser {
}
}

if (value === increment()) {
if (value === INCREMENT_SENTINEL) {
o[key] = increment(o[key])
} else if (Array.isArray(o[key])) {
if (duplicate && isTypeArray && isValueArray) {
Expand Down Expand Up @@ -1098,6 +1098,8 @@ function combineAliases (aliases: Dictionary<string | string[]>): Dictionary<str
return combined
}

const INCREMENT_SENTINEL = Symbol('count-increment')

// this function should only be called when a count is given as an arg
// it is NOT called to set a default value
// thus we can start the count at 1 instead of 0
Expand Down
4 changes: 4 additions & 0 deletions test/yargs-parser.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2581,6 +2581,10 @@ describe('yargs-parser', function () {

parsed.x.should.deep.equal(['o', 'p', 'q'])
})
it('does not sum repeated numeric arguments when one equals 1', function () {
const parsed = parser('-x 3 -x 1')
parsed.x.should.deep.equal([3, 1])
})
})

describe('flatten duplicate arrays', function () {
Expand Down