File tree Expand file tree Collapse file tree 2 files changed +43
-3
lines changed Expand file tree Collapse file tree 2 files changed +43
-3
lines changed Original file line number Diff line number Diff line change @@ -455,9 +455,16 @@ function parse (args, opts) {
455
455
var prefix = typeof envPrefix === 'string' ? envPrefix : ''
456
456
Object . keys ( process . env ) . forEach ( function ( envVar ) {
457
457
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 ] )
461
468
}
462
469
}
463
470
} )
Original file line number Diff line number Diff line change @@ -1641,6 +1641,39 @@ describe('yargs-parser', function () {
1641
1641
result . should . have . property ( 'truthy' )
1642
1642
result . z . should . equal ( 55 )
1643
1643
} )
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
+ } )
1644
1677
} )
1645
1678
1646
1679
describe ( 'configuration' , function ( ) {
You can’t perform that action at this time.
0 commit comments