Skip to content

Commit b04a189

Browse files
nexdrewbcoe
authored andcommitted
fix(count): do not increment a default value (#39)
1 parent 2418bbc commit b04a189

2 files changed

Lines changed: 63 additions & 4 deletions

File tree

index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,9 @@ function parse (args, opts) {
273273
applyEnvVars(argv, false)
274274
applyDefaultsAndAliases(argv, flags.aliases, defaults)
275275

276+
// for any counts either not in args or without an explicit default, set to 0
276277
Object.keys(flags.counts).forEach(function (key) {
277-
setArg(key, defaults[key])
278+
if (!hasKey(argv, key.split('.'))) setArg(key, 0)
278279
})
279280

280281
notFlags.forEach(function (key) {
@@ -334,7 +335,8 @@ function parse (args, opts) {
334335
if (!isUndefined(val) && !isNumber(val) && checkAllAliases(key, flags.numbers)) value = NaN
335336
}
336337

337-
if (checkAllAliases(key, flags.counts)) {
338+
// increment a count given as arg (either no value or value parsed as boolean)
339+
if (checkAllAliases(key, flags.counts) && (isUndefined(value) || typeof value === 'boolean')) {
338340
value = increment
339341
}
340342

@@ -514,7 +516,7 @@ function parse (args, opts) {
514516
o[key] = increment(o[key])
515517
} else if (o[key] === undefined && checkAllAliases(key, flags.arrays)) {
516518
o[key] = Array.isArray(value) ? value : [value]
517-
} else if (o[key] === undefined || checkAllAliases(key, flags.bools)) {
519+
} else if (o[key] === undefined || checkAllAliases(key, flags.bools) || checkAllAliases(key, flags.counts)) {
518520
o[key] = value
519521
} else if (Array.isArray(o[key])) {
520522
o[key].push(value)
@@ -665,8 +667,11 @@ function combineAliases (aliases) {
665667
return combined
666668
}
667669

670+
// this function should only be called when a count is given as an arg
671+
// it is NOT called to set a default value
672+
// thus we can start the count at 1 instead of 0
668673
function increment (orig) {
669-
return orig !== undefined ? orig + 1 : 0
674+
return orig !== undefined ? orig + 1 : 1
670675
}
671676

672677
function Parser (args, opts) {

test/yargs-parser.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,6 +1263,60 @@ describe('yargs-parser', function () {
12631263
parsed.verbose.should.equal(2)
12641264
parsed.should.have.property('_').and.deep.equal(['moomoo'])
12651265
})
1266+
1267+
it('should use a default value as is when no arg given', function () {
1268+
var parsed = parser([], {
1269+
count: 'v',
1270+
default: { v: 3 }
1271+
})
1272+
parsed.v.should.equal(3)
1273+
1274+
parsed = parser([], {
1275+
count: 'v',
1276+
default: { v: undefined }
1277+
})
1278+
expect(parsed.v).to.be.undefined
1279+
1280+
parsed = parser([], {
1281+
count: 'v',
1282+
default: { v: null }
1283+
})
1284+
expect(parsed.v).to.be.null
1285+
1286+
parsed = parser([], {
1287+
count: 'v',
1288+
default: { v: false }
1289+
})
1290+
parsed.v.should.equal(false)
1291+
1292+
parsed = parser([], {
1293+
count: 'v',
1294+
default: { v: 'hello' }
1295+
})
1296+
parsed.v.should.equal('hello')
1297+
})
1298+
1299+
it('should ignore a default value when arg given', function () {
1300+
var parsed = parser(['-vv', '-v', '-v'], {
1301+
count: 'v',
1302+
default: { v: 1 }
1303+
})
1304+
parsed.v.should.equal(4)
1305+
})
1306+
1307+
it('should increment regardless of arg value', function () {
1308+
var parsed = parser([
1309+
'-v',
1310+
'-v=true',
1311+
'-v', 'true',
1312+
'-v=false',
1313+
'-v', 'false',
1314+
'--no-v',
1315+
'-v=999',
1316+
'-v=foobar'
1317+
], { count: 'v' })
1318+
parsed.v.should.equal(8)
1319+
})
12661320
})
12671321

12681322
describe('array', function () {

0 commit comments

Comments
 (0)