File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -455,9 +455,16 @@ function parse (args, opts) {
455455 var prefix = typeof envPrefix === 'string' ? envPrefix : ''
456456 Object . keys ( process . env ) . forEach ( function ( envVar ) {
457457 if ( prefix === '' || envVar . lastIndexOf ( prefix , 0 ) === 0 ) {
458- var key = camelCase ( envVar . substring ( prefix . length ) )
459- if ( ( ( configOnly && flags . configs [ key ] ) || ! configOnly ) && ( ! ( key in argv ) || flags . defaulted [ key ] ) ) {
460- setArg ( key , process . env [ envVar ] )
458+ // get array of nested keys and convert them to camel case
459+ var keys = envVar . split ( '__' ) . map ( function ( key , i ) {
460+ if ( i === 0 ) {
461+ key = key . substring ( prefix . length )
462+ }
463+ return camelCase ( key )
464+ } )
465+
466+ if ( ( ( configOnly && flags . configs [ keys . join ( '.' ) ] ) || ! configOnly ) && ( ! hasKey ( argv , keys ) || flags . defaulted [ keys . join ( '.' ) ] ) ) {
467+ setArg ( keys . join ( '.' ) , process . env [ envVar ] )
461468 }
462469 }
463470 } )
Original file line number Diff line number Diff line change @@ -1641,6 +1641,39 @@ describe('yargs-parser', function () {
16411641 result . should . have . property ( 'truthy' )
16421642 result . z . should . equal ( 55 )
16431643 } )
1644+
1645+ it ( 'should apply all nested env vars' , function ( ) {
1646+ process . env . TEST_A = 'a'
1647+ process . env . TEST_NESTED_OPTION__FOO = 'baz'
1648+ process . env . TEST_NESTED_OPTION__BAR = 'bar'
1649+ var result = parser ( [ '--nestedOption.foo' , 'bar' ] , {
1650+ envPrefix : 'TEST'
1651+ } )
1652+
1653+ result . should . have . property ( 'a' , 'a' )
1654+ result . should . have . property ( 'nestedOption' ) . and . deep . equal ( {
1655+ foo : 'bar' ,
1656+ bar : 'bar'
1657+ } )
1658+ } )
1659+
1660+ it ( 'should apply nested env var if argv value is using default value' , function ( ) {
1661+ process . env . TEST_A = 'a'
1662+ process . env . TEST_NESTED_OPTION__FOO = 'baz'
1663+ process . env . TEST_NESTED_OPTION__BAR = 'bar'
1664+ var result = parser ( [ ] , {
1665+ envPrefix : 'TEST' ,
1666+ default : {
1667+ 'nestedOption.foo' : 'banana'
1668+ }
1669+ } )
1670+
1671+ result . should . have . property ( 'a' , 'a' )
1672+ result . should . have . property ( 'nestedOption' ) . and . deep . equal ( {
1673+ foo : 'baz' ,
1674+ bar : 'bar'
1675+ } )
1676+ } )
16441677 } )
16451678
16461679 describe ( 'configuration' , function ( ) {
You can’t perform that action at this time.
0 commit comments