@@ -2,7 +2,7 @@ var abbrev = require('abbrev')
2
2
const debug = require ( './debug' )
3
3
const defaultTypeDefs = require ( './type-defs' )
4
4
5
- function nopt ( args , { types, shorthands, typeDefs, invalidHandler } ) {
5
+ function nopt ( args , { types, shorthands, typeDefs, invalidHandler, typeDefault } ) {
6
6
debug ( types , shorthands , args , typeDefs )
7
7
8
8
var data = { }
@@ -15,7 +15,7 @@ function nopt (args, { types, shorthands, typeDefs, invalidHandler }) {
15
15
parse ( args , data , argv . remain , { typeDefs, types, shorthands } )
16
16
17
17
// now data is full
18
- clean ( data , { types, typeDefs, invalidHandler } )
18
+ clean ( data , { types, typeDefs, invalidHandler, typeDefault } )
19
19
data . argv = argv
20
20
21
21
Object . defineProperty ( data . argv , 'toString' , {
@@ -28,23 +28,28 @@ function nopt (args, { types, shorthands, typeDefs, invalidHandler }) {
28
28
return data
29
29
}
30
30
31
- function clean ( data , { types, typeDefs, invalidHandler } ) {
31
+ function clean ( data , { types, typeDefs, invalidHandler, typeDefault } ) {
32
32
const StringType = typeDefs . String . type
33
33
const NumberType = typeDefs . Number . type
34
34
const ArrayType = typeDefs . Array . type
35
35
const BooleanType = typeDefs . Boolean . type
36
36
const DateType = typeDefs . Date . type
37
37
38
+ const hasTypeDefault = typeof typeDefault !== 'undefined'
39
+ if ( ! hasTypeDefault ) {
40
+ typeDefault = [ false , true , null , StringType , ArrayType ]
41
+ }
42
+
38
43
var remove = { }
39
- var typeDefault = [ false , true , null , StringType , ArrayType ]
40
44
41
45
Object . keys ( data ) . forEach ( function ( k ) {
42
46
if ( k === 'argv' ) {
43
47
return
44
48
}
45
49
var val = data [ k ]
46
50
var isArray = Array . isArray ( val )
47
- var type = types [ k ]
51
+ let rawType = types [ k ]
52
+ var type = rawType
48
53
if ( ! isArray ) {
49
54
val = [ val ]
50
55
}
@@ -82,7 +87,14 @@ function clean (data, { types, typeDefs, invalidHandler }) {
82
87
}
83
88
84
89
if ( ! Object . prototype . hasOwnProperty . call ( types , k ) ) {
85
- return v
90
+ if ( ! hasTypeDefault ) {
91
+ return v
92
+ }
93
+ // if the default type has been passed in then we want to validate the
94
+ // unknown data key instead of bailing out earlier. we also set the raw
95
+ // type which is passed to the invalid handler so that it can be
96
+ // determined if during validation if it is unknown vs invalid
97
+ rawType = typeDefault
86
98
}
87
99
88
100
// allow `--no-blah` to set 'blah' to null if null is allowed
@@ -93,16 +105,16 @@ function clean (data, { types, typeDefs, invalidHandler }) {
93
105
94
106
var d = { }
95
107
d [ k ] = v
96
- debug ( 'prevalidated val' , d , v , types [ k ] )
97
- if ( ! validate ( d , k , v , types [ k ] , { typeDefs } ) ) {
108
+ debug ( 'prevalidated val' , d , v , rawType )
109
+ if ( ! validate ( d , k , v , rawType , { typeDefs } ) ) {
98
110
if ( invalidHandler ) {
99
- invalidHandler ( k , v , types [ k ] , data )
111
+ invalidHandler ( k , v , rawType , data )
100
112
} else if ( invalidHandler !== false ) {
101
- debug ( 'invalid: ' + k + '=' + v , types [ k ] )
113
+ debug ( 'invalid: ' + k + '=' + v , rawType )
102
114
}
103
115
return remove
104
116
}
105
- debug ( 'validated v' , d , v , types [ k ] )
117
+ debug ( 'validated v' , d , v , rawType )
106
118
return d [ k ]
107
119
} ) . filter ( function ( v ) {
108
120
return v !== remove
0 commit comments