@@ -2,15 +2,16 @@ var camelCase = require('camelcase')
2
2
var path = require ( 'path' )
3
3
var tokenizeArgString = require ( './lib/tokenize-arg-string' )
4
4
var util = require ( 'util' )
5
+ var _ = require ( 'lodash' )
5
6
6
7
function parse ( args , opts ) {
7
- if ( ! opts ) opts = { }
8
+ if ( ! opts ) opts = _ . create ( null )
8
9
// allow a string argument to be passed in rather
9
10
// than an argv array.
10
11
args = tokenizeArgString ( args )
11
12
// aliases might have transitive relationships, normalize this.
12
- var aliases = combineAliases ( opts . alias || { } )
13
- var configuration = assign ( {
13
+ var aliases = combineAliases ( opts . alias || _ . create ( null ) )
14
+ var configuration = _ . assign ( {
14
15
'short-option-groups' : true ,
15
16
'camel-case-expansion' : true ,
16
17
'dot-notation' : true ,
@@ -19,27 +20,27 @@ function parse (args, opts) {
19
20
'duplicate-arguments-array' : true ,
20
21
'flatten-duplicate-arrays' : true
21
22
} , opts . configuration )
22
- var defaults = opts . default || { }
23
+ var defaults = opts . default || _ . create ( null )
23
24
var configObjects = opts . configObjects || [ ]
24
25
var envPrefix = opts . envPrefix
25
- var newAliases = { }
26
+ var newAliases = _ . create ( null )
26
27
// allow a i18n handler to be passed in, default to a fake one (util.format).
27
28
var __ = opts . __ || function ( str ) {
28
29
return util . format . apply ( util , Array . prototype . slice . call ( arguments ) )
29
30
}
30
31
var error = null
31
32
var flags = {
32
- aliases : { } ,
33
- arrays : { } ,
34
- bools : { } ,
35
- strings : { } ,
36
- numbers : { } ,
37
- counts : { } ,
38
- normalize : { } ,
39
- configs : { } ,
40
- defaulted : { } ,
41
- nargs : { } ,
42
- coercions : { }
33
+ aliases : _ . create ( null ) ,
34
+ arrays : _ . create ( null ) ,
35
+ bools : _ . create ( null ) ,
36
+ strings : _ . create ( null ) ,
37
+ numbers : _ . create ( null ) ,
38
+ counts : _ . create ( null ) ,
39
+ normalize : _ . create ( null ) ,
40
+ configs : _ . create ( null ) ,
41
+ defaulted : _ . create ( null ) ,
42
+ nargs : _ . create ( null ) ,
43
+ coercions : _ . create ( null )
43
44
}
44
45
var negative = / ^ - [ 0 - 9 ] + ( \. [ 0 - 9 ] + ) ? /
45
46
@@ -67,11 +68,11 @@ function parse (args, opts) {
67
68
flags . normalize [ key ] = true
68
69
} )
69
70
70
- Object . keys ( opts . narg || { } ) . forEach ( function ( k ) {
71
+ Object . keys ( opts . narg || _ . create ( null ) ) . forEach ( function ( k ) {
71
72
flags . nargs [ k ] = opts . narg [ k ]
72
73
} )
73
74
74
- Object . keys ( opts . coerce || { } ) . forEach ( function ( k ) {
75
+ Object . keys ( opts . coerce || _ . create ( null ) ) . forEach ( function ( k ) {
75
76
flags . coercions [ k ] = opts . coerce [ k ]
76
77
} )
77
78
@@ -80,7 +81,7 @@ function parse (args, opts) {
80
81
flags . configs [ key ] = true
81
82
} )
82
83
} else {
83
- Object . keys ( opts . config || { } ) . forEach ( function ( k ) {
84
+ Object . keys ( opts . config || _ . create ( null ) ) . forEach ( function ( k ) {
84
85
flags . configs [ k ] = opts . config [ k ]
85
86
} )
86
87
}
@@ -92,7 +93,7 @@ function parse (args, opts) {
92
93
// apply default values to all aliases.
93
94
Object . keys ( defaults ) . forEach ( function ( key ) {
94
95
( flags . aliases [ key ] || [ ] ) . forEach ( function ( alias ) {
95
- defaults [ alias ] = defaults [ key ]
96
+ _ . set ( defaults , alias , defaults [ key ] )
96
97
} )
97
98
} )
98
99
@@ -417,7 +418,7 @@ function parse (args, opts) {
417
418
// set args from config.json file, this should be
418
419
// applied last so that defaults can be applied.
419
420
function setConfig ( argv ) {
420
- var configLookup = { }
421
+ var configLookup = _ . create ( null )
421
422
422
423
// expand defaults/aliases, in-case any happen to reference
423
424
// the config.json file.
@@ -537,7 +538,7 @@ function parse (args, opts) {
537
538
if ( ! configuration [ 'dot-notation' ] ) keys = [ keys . join ( '.' ) ]
538
539
539
540
keys . slice ( 0 , - 1 ) . forEach ( function ( key ) {
540
- o = ( o [ key ] || { } )
541
+ o = ( o [ key ] || _ . create ( null ) )
541
542
} )
542
543
543
544
var key = keys [ keys . length - 1 ]
@@ -547,44 +548,44 @@ function parse (args, opts) {
547
548
}
548
549
549
550
function setKey ( obj , keys , value ) {
550
- var o = obj
551
-
552
- if ( ! configuration [ 'dot-notation' ] ) keys = [ keys . join ( '.' ) ]
551
+ if ( ! configuration [ 'dot-notation' ] ) keys = keys . join ( '/' )
553
552
554
- keys . slice ( 0 , - 1 ) . forEach ( function ( key ) {
555
- if ( o [ key ] === undefined ) o [ key ] = { }
556
- o = o [ key ]
557
- } )
553
+ _ . update ( obj , keys , updater )
558
554
559
- var key = keys [ keys . length - 1 ]
555
+ if ( ! configuration [ 'dot-notation' ] ) {
556
+ obj [ keys . replace ( '/' , '.' ) ] = obj [ keys ]
557
+ delete obj [ keys ]
558
+ }
560
559
561
- var isTypeArray = checkAllAliases ( key , flags . arrays )
562
- var isValueArray = Array . isArray ( value )
563
- var duplicate = configuration [ 'duplicate-arguments-array' ]
564
-
565
- if ( value === increment ) {
566
- o [ key ] = increment ( o [ key ] )
567
- } else if ( Array . isArray ( o [ key ] ) ) {
568
- if ( duplicate && isTypeArray && isValueArray ) {
569
- o [ key ] = configuration [ 'flatten-duplicate-arrays' ] ? o [ key ] . concat ( value ) : [ o [ key ] ] . concat ( [ value ] )
570
- } else if ( ! duplicate && Boolean ( isTypeArray ) === Boolean ( isValueArray ) ) {
571
- o [ key ] = value
560
+ function updater ( existing ) {
561
+ var isTypeArray = checkAllAliases ( key , flags . arrays )
562
+ var isValueArray = Array . isArray ( value )
563
+ var duplicate = configuration [ 'duplicate-arguments-array' ]
564
+
565
+ if ( value === increment ) {
566
+ return increment ( existing )
567
+ } else if ( Array . isArray ( existing ) ) {
568
+ if ( duplicate && isTypeArray && isValueArray ) {
569
+ return configuration [ 'flatten-duplicate-arrays' ] ? existing . concat ( value ) : [ existing ] . concat ( [ value ] )
570
+ } else if ( ! duplicate && Boolean ( isTypeArray ) === Boolean ( isValueArray ) ) {
571
+ return value
572
+ } else {
573
+ return existing . concat ( [ value ] )
574
+ }
575
+ } else if ( existing === undefined && isTypeArray ) {
576
+ return isValueArray ? value : [ value ]
577
+ } else if ( duplicate && ! ( existing === undefined || checkAllAliases ( key , flags . bools ) || checkAllAliases ( keys . join ( '.' ) , flags . bools ) || checkAllAliases ( key , flags . counts ) ) ) {
578
+ return [ existing , value ]
572
579
} else {
573
- o [ key ] = o [ key ] . concat ( [ value ] )
580
+ return value
574
581
}
575
- } else if ( o [ key ] === undefined && isTypeArray ) {
576
- o [ key ] = isValueArray ? value : [ value ]
577
- } else if ( duplicate && ! ( o [ key ] === undefined || checkAllAliases ( key , flags . bools ) || checkAllAliases ( keys . join ( '.' ) , flags . bools ) || checkAllAliases ( key , flags . counts ) ) ) {
578
- o [ key ] = [ o [ key ] , value ]
579
- } else {
580
- o [ key ] = value
581
582
}
582
583
}
583
584
584
585
// extend the aliases list with inferred aliases.
585
586
function extendAliases ( ) {
586
587
Array . prototype . slice . call ( arguments ) . forEach ( function ( obj ) {
587
- Object . keys ( obj || { } ) . forEach ( function ( key ) {
588
+ Object . keys ( obj || _ . create ( null ) ) . forEach ( function ( key ) {
588
589
// short-circuit if we've already added a key
589
590
// to the aliases array, for example it might
590
591
// exist in both 'opts.default' and 'opts.key'.
@@ -681,7 +682,7 @@ function parse (args, opts) {
681
682
function combineAliases ( aliases ) {
682
683
var aliasArrays = [ ]
683
684
var change = true
684
- var combined = { }
685
+ var combined = _ . create ( null )
685
686
686
687
// turn alias lookup hash {key: ['alias1', 'alias2']} into
687
688
// a simple array ['key', 'alias1', 'alias2']
@@ -723,20 +724,6 @@ function combineAliases (aliases) {
723
724
return combined
724
725
}
725
726
726
- function assign ( defaults , configuration ) {
727
- var o = { }
728
- configuration = configuration || { }
729
-
730
- Object . keys ( defaults ) . forEach ( function ( k ) {
731
- o [ k ] = defaults [ k ]
732
- } )
733
- Object . keys ( configuration ) . forEach ( function ( k ) {
734
- o [ k ] = configuration [ k ]
735
- } )
736
-
737
- return o
738
- }
739
-
740
727
// this function should only be called when a count is given as an arg
741
728
// it is NOT called to set a default value
742
729
// thus we can start the count at 1 instead of 0
0 commit comments