diff --git a/CHANGELOG.md b/CHANGELOG.md index 547591dc..3d6d5047 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,17 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. + +# [4.1.0](https://github.com/yargs/yargs-parser/compare/v4.0.2...v4.1.0) (2016-11-07) + + +### Features + +* apply coercions to default options ([#65](https://github.com/yargs/yargs-parser/issues/65)) ([c79052b](https://github.com/yargs/yargs-parser/commit/c79052b)) +* handle dot notation boolean options ([#63](https://github.com/yargs/yargs-parser/issues/63)) ([02c3545](https://github.com/yargs/yargs-parser/commit/02c3545)) + + + ## [4.0.2](https://github.com/yargs/yargs-parser/compare/v4.0.1...v4.0.2) (2016-09-30) diff --git a/index.js b/index.js index 564cb2ed..0d3ed26a 100644 --- a/index.js +++ b/index.js @@ -279,8 +279,8 @@ function parse (args, opts) { setConfig(argv) setConfigObjects() applyEnvVars(argv, false) - applyCoercions(argv) applyDefaultsAndAliases(argv, flags.aliases, defaults) + applyCoercions(argv) // for any counts either not in args or without an explicit default, set to 0 Object.keys(flags.counts).forEach(function (key) { @@ -540,7 +540,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) || checkAllAliases(key, flags.counts)) { + } else if (o[key] === undefined || checkAllAliases(key, flags.bools) || checkAllAliases(keys.join('.'), flags.bools) || checkAllAliases(key, flags.counts)) { o[key] = value } else if (Array.isArray(o[key])) { o[key].push(value) diff --git a/package.json b/package.json index 81c511e5..a2f4baad 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "yargs-parser", - "version": "4.0.2", + "version": "4.1.0", "description": "the mighty option parser used by yargs", "main": "index.js", "scripts": { @@ -31,7 +31,7 @@ "mocha": "^3.0.1", "nyc": "^8.1.0", "standard": "^8.0.0", - "standard-version": "^2.1.2" + "standard-version": "^3.0.0" }, "dependencies": { "camelcase": "^3.0.0" diff --git a/test/yargs-parser.js b/test/yargs-parser.js index dac993c9..8529d859 100644 --- a/test/yargs-parser.js +++ b/test/yargs-parser.js @@ -198,6 +198,18 @@ describe('yargs-parser', function () { parse.should.have.property('_').and.deep.equal(['one', 'two', 'three']) }) + it('should correctly parse dot-notation boolean flags', function () { + var parse = parser(['--nested', '--n.v', '--n.y', 'foo'], { + boolean: ['nested', 'n.v'] + }) + + parse.should.have.property('nested', true).and.be.a('boolean') + parse.should.have.property('n').and.deep.equal({ + v: true, + y: 'foo' + }) + }) + it('should preserve newlines in option values', function () { var args = parser(['-s', 'X\nX']) args.should.have.property('_').with.length(0) @@ -1992,6 +2004,18 @@ describe('yargs-parser', function () { parsed.foo.bar.should.equal('nananana, batman!') }) + it('applies coercion to defaults', function () { + var parsed = parser([], { + default: { foo: 'bar' }, + coerce: { + foo: function (val) { + return val.toUpperCase() + } + } + }) + parsed.foo.should.equal('BAR') + }) + it('applies coercion function to an implicit array', function () { var parsed = parser(['--foo', '99', '-f', '33'], { coerce: {