Skip to content

Commit

Permalink
feat(configuration): Allow to pass configuration objects to yargs-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
elas7 authored and bcoe committed Apr 9, 2016
1 parent 69941a6 commit 0780900
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 2 deletions.
15 changes: 13 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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

Expand Down
96 changes: 96 additions & 0 deletions test/yargs-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down

0 comments on commit 0780900

Please sign in to comment.