diff --git a/index.js b/index.js index 42dac4d1..2838f685 100644 --- a/index.js +++ b/index.js @@ -19,6 +19,7 @@ function parse (args, opts) { 'boolean-negation': true }, opts.configuration) var defaults = opts.default || {} + var configObjects = opts.configObjects || [] var envPrefix = opts.envPrefix var newAliases = {} // allow a i18n handler to be passed in, default to a fake one (util.format). @@ -263,10 +264,12 @@ function parse (args, opts) { // order of precedence: // 1. command line arg // 2. value from config file - // 3. value from env var - // 4. configured default value + // 3. value from config objects + // 4. value from env var + // 5. configured default value applyEnvVars(argv, true) // special case: check env vars that point to config file setConfig(argv) + setConfigObjects() applyEnvVars(argv, false) applyDefaultsAndAliases(argv, flags.aliases, defaults) @@ -438,6 +441,14 @@ function parse (args, opts) { }) } + // set all config objects passed in opts + function setConfigObjects () { + if (typeof configObjects === 'undefined') return + configObjects.forEach(function (configObject) { + setConfigObject(configObject) + }) + } + function applyEnvVars (argv, configOnly) { if (typeof envPrefix === 'undefined') return diff --git a/test/yargs-parser.js b/test/yargs-parser.js index 338aea0c..eb14c59e 100644 --- a/test/yargs-parser.js +++ b/test/yargs-parser.js @@ -605,6 +605,102 @@ describe('yargs-parser', function () { }) }) + describe('config objects', function () { + it('should load options from config object', function () { + var argv = parser([ '--foo', 'bar' ], { + configObjects: [{ + apple: 'apple', + banana: 42, + foo: 'baz' + }] + }) + + argv.should.have.property('apple', 'apple') + argv.should.have.property('banana', 42) + argv.should.have.property('foo', 'bar') + }) + + it('should use value from config object, if argv value is using default value', function () { + var argv = parser([], { + configObjects: [{ + apple: 'apple', + banana: 42, + foo: 'baz' + }], + default: { + foo: 'banana' + } + }) + + argv.should.have.property('apple', 'apple') + argv.should.have.property('banana', 42) + argv.should.have.property('foo', 'baz') + }) + + it('should use value from config object to all aliases', function () { + var argv = parser([], { + configObjects: [{ + apple: 'apple', + banana: 42, + foo: 'baz' + }], + alias: { + a: ['apple'], + banana: ['b'] + } + }) + + argv.should.have.property('apple', 'apple') + argv.should.have.property('a', 'apple') + argv.should.have.property('banana', 42) + argv.should.have.property('b', 42) + argv.should.have.property('foo', 'baz') + }) + + it('should load nested options from config object', function () { + var argv = parser(['--nested.foo', 'bar'], { + configObjects: [{ + a: 'a', + nested: { + foo: 'baz', + bar: 'bar' + }, + b: 'b' + }] + }) + + argv.should.have.property('a', 'a') + argv.should.have.property('b', 'b') + argv.should.have.property('nested').and.deep.equal({ + foo: 'bar', + bar: 'bar' + }) + }) + + it('should use nested value from config object, if argv value is using default value', function () { + var argv = parser([], { + configObjects: [{ + a: 'a', + nested: { + foo: 'baz', + bar: 'bar' + }, + b: 'b' + }], + default: { + 'nested.foo': 'banana' + } + }) + + argv.should.have.property('a', 'a') + argv.should.have.property('b', 'b') + argv.should.have.property('nested').and.deep.equal({ + foo: 'baz', + bar: 'bar' + }) + }) + }) + describe('dot notation', function () { it('should allow object graph traversal via dot notation', function () { var argv = parser([