@@ -2,7 +2,22 @@ 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, typeDefault } ) {
5
+ const hasOwn = ( o , k ) => Object . prototype . hasOwnProperty . call ( o , k )
6
+
7
+ const getType = ( k , { types, dynamicTypes } ) => {
8
+ let hasType = hasOwn ( types , k )
9
+ let type = types [ k ]
10
+ if ( ! hasType && typeof dynamicTypes === 'function' ) {
11
+ const matchedType = dynamicTypes ( k )
12
+ if ( matchedType !== undefined ) {
13
+ type = matchedType
14
+ hasType = true
15
+ }
16
+ }
17
+ return [ hasType , type ]
18
+ }
19
+
20
+ function nopt ( args , { types, dynamicTypes, shorthands, typeDefs, invalidHandler, typeDefault } ) {
6
21
debug ( types , shorthands , args , typeDefs )
7
22
8
23
var data = { }
@@ -12,10 +27,10 @@ function nopt (args, { types, shorthands, typeDefs, invalidHandler, typeDefault
12
27
original : args . slice ( 0 ) ,
13
28
}
14
29
15
- parse ( args , data , argv . remain , { typeDefs, types, shorthands } )
30
+ parse ( args , data , argv . remain , { typeDefs, types, dynamicTypes , shorthands } )
16
31
17
32
// now data is full
18
- clean ( data , { types, typeDefs, invalidHandler, typeDefault } )
33
+ clean ( data , { types, dynamicTypes , typeDefs, invalidHandler, typeDefault } )
19
34
data . argv = argv
20
35
21
36
Object . defineProperty ( data . argv , 'toString' , {
@@ -28,7 +43,7 @@ function nopt (args, { types, shorthands, typeDefs, invalidHandler, typeDefault
28
43
return data
29
44
}
30
45
31
- function clean ( data , { types, typeDefs, invalidHandler, typeDefault } ) {
46
+ function clean ( data , { types, dynamicTypes , typeDefs, invalidHandler, typeDefault } ) {
32
47
const StringType = typeDefs . String . type
33
48
const NumberType = typeDefs . Number . type
34
49
const ArrayType = typeDefs . Array . type
@@ -48,7 +63,7 @@ function clean (data, { types, typeDefs, invalidHandler, typeDefault }) {
48
63
}
49
64
var val = data [ k ]
50
65
var isArray = Array . isArray ( val )
51
- let rawType = types [ k ]
66
+ let [ hasType , rawType ] = getType ( k , { types , dynamicTypes } )
52
67
var type = rawType
53
68
if ( ! isArray ) {
54
69
val = [ val ]
@@ -86,7 +101,7 @@ function clean (data, { types, typeDefs, invalidHandler, typeDefault }) {
86
101
}
87
102
}
88
103
89
- if ( ! Object . prototype . hasOwnProperty . call ( types , k ) ) {
104
+ if ( ! hasType ) {
90
105
if ( ! hasTypeDefault ) {
91
106
return v
92
107
}
@@ -205,7 +220,7 @@ function validate (data, k, val, type, { typeDefs }) {
205
220
return ok
206
221
}
207
222
208
- function parse ( args , data , remain , { typeDefs, types, shorthands } ) {
223
+ function parse ( args , data , remain , { typeDefs, types, dynamicTypes , shorthands } ) {
209
224
const StringType = typeDefs . String . type
210
225
const NumberType = typeDefs . String . type
211
226
const ArrayType = typeDefs . Array . type
@@ -214,6 +229,7 @@ function parse (args, data, remain, { typeDefs, types, shorthands }) {
214
229
debug ( 'parse' , args , data , remain )
215
230
216
231
var abbrevs = abbrev ( Object . keys ( types ) )
232
+ debug ( 'abbrevs=%j' , abbrevs )
217
233
var shortAbbr = abbrev ( Object . keys ( shorthands ) )
218
234
219
235
for ( var i = 0 ; i < args . length ; i ++ ) {
@@ -260,7 +276,7 @@ function parse (args, data, remain, { typeDefs, types, shorthands }) {
260
276
arg = abbrevs [ arg ]
261
277
}
262
278
263
- var argType = types [ arg ]
279
+ var [ hasType , argType ] = getType ( arg , { types , dynamicTypes } )
264
280
var isTypeArray = Array . isArray ( argType )
265
281
if ( isTypeArray && argType . length === 1 ) {
266
282
isTypeArray = false
@@ -271,10 +287,7 @@ function parse (args, data, remain, { typeDefs, types, shorthands }) {
271
287
isTypeArray && argType . indexOf ( ArrayType ) !== - 1
272
288
273
289
// allow unknown things to be arrays if specified multiple times.
274
- if (
275
- ! Object . prototype . hasOwnProperty . call ( types , arg ) &&
276
- Object . prototype . hasOwnProperty . call ( data , arg )
277
- ) {
290
+ if ( ! hasType && hasOwn ( data , arg ) ) {
278
291
if ( ! Array . isArray ( data [ arg ] ) ) {
279
292
data [ arg ] = [ data [ arg ] ]
280
293
}
0 commit comments