Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(count): do not increment a default value #39

Merged
merged 1 commit into from
Jul 16, 2016
Merged
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
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 () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lovely, glad to have these additional tests for count.

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