Skip to content

Commit 0780900

Browse files
elas7bcoe
authored andcommitted
feat(configuration): Allow to pass configuration objects to yargs-parser
1 parent 69941a6 commit 0780900

2 files changed

Lines changed: 109 additions & 2 deletions

File tree

index.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ function parse (args, opts) {
1919
'boolean-negation': true
2020
}, opts.configuration)
2121
var defaults = opts.default || {}
22+
var configObjects = opts.configObjects || []
2223
var envPrefix = opts.envPrefix
2324
var newAliases = {}
2425
// allow a i18n handler to be passed in, default to a fake one (util.format).
@@ -263,10 +264,12 @@ function parse (args, opts) {
263264
// order of precedence:
264265
// 1. command line arg
265266
// 2. value from config file
266-
// 3. value from env var
267-
// 4. configured default value
267+
// 3. value from config objects
268+
// 4. value from env var
269+
// 5. configured default value
268270
applyEnvVars(argv, true) // special case: check env vars that point to config file
269271
setConfig(argv)
272+
setConfigObjects()
270273
applyEnvVars(argv, false)
271274
applyDefaultsAndAliases(argv, flags.aliases, defaults)
272275

@@ -438,6 +441,14 @@ function parse (args, opts) {
438441
})
439442
}
440443

444+
// set all config objects passed in opts
445+
function setConfigObjects () {
446+
if (typeof configObjects === 'undefined') return
447+
configObjects.forEach(function (configObject) {
448+
setConfigObject(configObject)
449+
})
450+
}
451+
441452
function applyEnvVars (argv, configOnly) {
442453
if (typeof envPrefix === 'undefined') return
443454

test/yargs-parser.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,102 @@ describe('yargs-parser', function () {
605605
})
606606
})
607607

608+
describe('config objects', function () {
609+
it('should load options from config object', function () {
610+
var argv = parser([ '--foo', 'bar' ], {
611+
configObjects: [{
612+
apple: 'apple',
613+
banana: 42,
614+
foo: 'baz'
615+
}]
616+
})
617+
618+
argv.should.have.property('apple', 'apple')
619+
argv.should.have.property('banana', 42)
620+
argv.should.have.property('foo', 'bar')
621+
})
622+
623+
it('should use value from config object, if argv value is using default value', function () {
624+
var argv = parser([], {
625+
configObjects: [{
626+
apple: 'apple',
627+
banana: 42,
628+
foo: 'baz'
629+
}],
630+
default: {
631+
foo: 'banana'
632+
}
633+
})
634+
635+
argv.should.have.property('apple', 'apple')
636+
argv.should.have.property('banana', 42)
637+
argv.should.have.property('foo', 'baz')
638+
})
639+
640+
it('should use value from config object to all aliases', function () {
641+
var argv = parser([], {
642+
configObjects: [{
643+
apple: 'apple',
644+
banana: 42,
645+
foo: 'baz'
646+
}],
647+
alias: {
648+
a: ['apple'],
649+
banana: ['b']
650+
}
651+
})
652+
653+
argv.should.have.property('apple', 'apple')
654+
argv.should.have.property('a', 'apple')
655+
argv.should.have.property('banana', 42)
656+
argv.should.have.property('b', 42)
657+
argv.should.have.property('foo', 'baz')
658+
})
659+
660+
it('should load nested options from config object', function () {
661+
var argv = parser(['--nested.foo', 'bar'], {
662+
configObjects: [{
663+
a: 'a',
664+
nested: {
665+
foo: 'baz',
666+
bar: 'bar'
667+
},
668+
b: 'b'
669+
}]
670+
})
671+
672+
argv.should.have.property('a', 'a')
673+
argv.should.have.property('b', 'b')
674+
argv.should.have.property('nested').and.deep.equal({
675+
foo: 'bar',
676+
bar: 'bar'
677+
})
678+
})
679+
680+
it('should use nested value from config object, if argv value is using default value', function () {
681+
var argv = parser([], {
682+
configObjects: [{
683+
a: 'a',
684+
nested: {
685+
foo: 'baz',
686+
bar: 'bar'
687+
},
688+
b: 'b'
689+
}],
690+
default: {
691+
'nested.foo': 'banana'
692+
}
693+
})
694+
695+
argv.should.have.property('a', 'a')
696+
argv.should.have.property('b', 'b')
697+
argv.should.have.property('nested').and.deep.equal({
698+
foo: 'baz',
699+
bar: 'bar'
700+
})
701+
})
702+
})
703+
608704
describe('dot notation', function () {
609705
it('should allow object graph traversal via dot notation', function () {
610706
var argv = parser([

0 commit comments

Comments
 (0)