Skip to content

Commit

Permalink
fix(count): do not increment a default value (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
nexdrew authored and bcoe committed Jul 16, 2016
1 parent 2418bbc commit b04a189
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
13 changes: 9 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,9 @@ function parse (args, opts) {
applyEnvVars(argv, false)
applyDefaultsAndAliases(argv, flags.aliases, defaults)

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

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

if (checkAllAliases(key, flags.counts)) {
// 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
}

Expand Down Expand Up @@ -514,7 +516,7 @@ function parse (args, opts) {
o[key] = increment(o[key])
} else if (o[key] === undefined && checkAllAliases(key, flags.arrays)) {
o[key] = Array.isArray(value) ? value : [value]
} else if (o[key] === undefined || checkAllAliases(key, flags.bools)) {
} else if (o[key] === undefined || checkAllAliases(key, flags.bools) || checkAllAliases(key, flags.counts)) {
o[key] = value
} else if (Array.isArray(o[key])) {
o[key].push(value)
Expand Down Expand Up @@ -665,8 +667,11 @@ function combineAliases (aliases) {
return combined
}

// 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
function increment (orig) {
return orig !== undefined ? orig + 1 : 0
return orig !== undefined ? orig + 1 : 1
}

function Parser (args, opts) {
Expand Down
54 changes: 54 additions & 0 deletions test/yargs-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,60 @@ describe('yargs-parser', function () {
parsed.verbose.should.equal(2)
parsed.should.have.property('_').and.deep.equal(['moomoo'])
})

it('should use a default value as is when no arg given', function () {
var parsed = parser([], {
count: 'v',
default: { v: 3 }
})
parsed.v.should.equal(3)

parsed = parser([], {
count: 'v',
default: { v: undefined }
})
expect(parsed.v).to.be.undefined

parsed = parser([], {
count: 'v',
default: { v: null }
})
expect(parsed.v).to.be.null

parsed = parser([], {
count: 'v',
default: { v: false }
})
parsed.v.should.equal(false)

parsed = parser([], {
count: 'v',
default: { v: 'hello' }
})
parsed.v.should.equal('hello')
})

it('should ignore a default value when arg given', function () {
var parsed = parser(['-vv', '-v', '-v'], {
count: 'v',
default: { v: 1 }
})
parsed.v.should.equal(4)
})

it('should increment regardless of arg value', function () {
var parsed = parser([
'-v',
'-v=true',
'-v', 'true',
'-v=false',
'-v', 'false',
'--no-v',
'-v=999',
'-v=foobar'
], { count: 'v' })
parsed.v.should.equal(8)
})
})

describe('array', function () {
Expand Down

0 comments on commit b04a189

Please sign in to comment.