@@ -8,7 +8,7 @@ var findUp = require('find-up');
8
8
var readPkgUp = require ( 'read-pkg-up' ) ;
9
9
var chalk = require ( 'chalk' ) ;
10
10
var mkdirp = require ( 'mkdirp' ) ;
11
- var nopt = require ( 'nopt ' ) ;
11
+ var minimist = require ( 'minimist ' ) ;
12
12
var runAsync = require ( 'run-async' ) ;
13
13
var through = require ( 'through2' ) ;
14
14
var userHome = require ( 'user-home' ) ;
@@ -229,14 +229,6 @@ Base.prototype.option = function option(name, config) {
229
229
this . _options [ name ] = config ;
230
230
}
231
231
232
- if ( this . options [ name ] == null && config . alias ) {
233
- this . options [ name ] = this . options [ config . alias ] ;
234
- }
235
-
236
- if ( this . options [ name ] == null ) {
237
- this . options [ name ] = config . default ;
238
- }
239
-
240
232
this . parseOptions ( ) ;
241
233
return this ;
242
234
} ;
@@ -275,47 +267,76 @@ Base.prototype.argument = function argument(name, config) {
275
267
type : String
276
268
} ) ;
277
269
270
+ this . _arguments . push ( config ) ;
278
271
279
- var position = this . _arguments . length ;
280
- this . _arguments . push ( {
281
- name : name ,
282
- config : config
283
- } ) ;
284
-
285
- Object . defineProperty ( this , name , {
286
- configurable : true ,
287
- enumerable : true ,
288
- get : function ( ) {
289
- // a bit of coercion and type handling, to be improved
290
- // just dealing with Array/String, default is assumed to be String
291
- var value = config . type === Array ? this . args . slice ( position ) : this . args [ position ] ;
292
- return position >= this . args . length ? config . default : value ;
293
- } ,
294
- set : function ( value ) {
295
- this . args [ position ] = value ;
296
- }
297
- } ) ;
298
-
299
- this . checkRequiredArgs ( ) ;
272
+ this . parseOptions ( ) ;
300
273
return this ;
301
274
} ;
302
275
303
276
Base . prototype . parseOptions = function ( ) {
304
- var opts = { } ;
305
- var shortOpts = { } ;
277
+ var minimistDef = {
278
+ string : [ ] ,
279
+ boolean : [ ] ,
280
+ alias : { } ,
281
+ default : { }
282
+ } ;
306
283
307
284
_ . each ( this . _options , function ( option ) {
308
- opts [ option . name ] = option . type ;
285
+ if ( option . type === Boolean ) {
286
+ minimistDef . boolean . push ( option . name ) ;
287
+ } else {
288
+ minimistDef . string . push ( option . name ) ;
289
+ }
309
290
310
291
if ( option . alias ) {
311
- shortOpts [ option . alias ] = '--' + option . name ;
292
+ minimistDef . alias [ option . alias ] = option . name ;
312
293
}
294
+
295
+ // Only apply default values if we don't already have a value injected from
296
+ // the runner
297
+ if ( option . name in this . options ) {
298
+ minimistDef . default [ option . name ] = this . options [ option . name ] ;
299
+ } else if ( option . alias && option . alias in this . options ) {
300
+ minimistDef . default [ option . alias ] = this . options [ option . alias ] ;
301
+ } else if ( 'default' in option ) {
302
+ minimistDef . default [ option . name ] = option . default ;
303
+ }
304
+ } . bind ( this ) ) ;
305
+
306
+ var parsedOpts = minimist ( this . _args , minimistDef ) ;
307
+
308
+ // Parse options to the desired type
309
+ _ . each ( parsedOpts , function ( option , name ) {
310
+ if ( this . _options [ name ] && option !== undefined ) {
311
+ parsedOpts [ name ] = this . _options [ name ] . type ( option ) ;
312
+ }
313
+ } . bind ( this ) ) ;
314
+
315
+ // Parse positional arguments to valid options
316
+ this . _arguments . forEach ( function ( config , index ) {
317
+ var value ;
318
+ if ( index >= parsedOpts . _ . length ) {
319
+ if ( 'default' in config ) {
320
+ value = config . default ;
321
+ } else {
322
+ return ;
323
+ }
324
+ } else {
325
+ if ( config . type === Array ) {
326
+ value = parsedOpts . _ . slice ( index , parsedOpts . _ . length ) ;
327
+ } else {
328
+ value = config . type ( parsedOpts . _ [ index ] ) ;
329
+ }
330
+ }
331
+
332
+ parsedOpts [ config . name ] = value ;
313
333
} ) ;
314
334
315
- opts = nopt ( opts , shortOpts , this . _args , 0 ) ;
316
- _ . extend ( this . options , opts ) ;
335
+ // Make the parsed options available to the instance
336
+ _ . extend ( this . options , parsedOpts ) ;
337
+ this . args = this . arguments = parsedOpts . _ ;
317
338
318
- this . args = this . arguments = opts . argv . remain ;
339
+ // Make sure required args are all present
319
340
this . checkRequiredArgs ( ) ;
320
341
} ;
321
342
@@ -331,11 +352,11 @@ Base.prototype.checkRequiredArgs = function () {
331
352
return ;
332
353
}
333
354
334
- this . _arguments . forEach ( function ( arg , position ) {
355
+ this . _arguments . forEach ( function ( config , position ) {
335
356
// If the help option was not provided, check whether the argument was
336
357
// required, and whether a value was provided.
337
- if ( arg . config . required && position >= this . args . length ) {
338
- return this . emit ( 'error' , new Error ( 'Did not provide required argument ' + chalk . bold ( arg . name ) + '!' ) ) ;
358
+ if ( config . required && position >= this . args . length ) {
359
+ return this . emit ( 'error' , new Error ( 'Did not provide required argument ' + chalk . bold ( config . name ) + '!' ) ) ;
339
360
}
340
361
} , this ) ;
341
362
} ;
0 commit comments