@@ -16,6 +16,7 @@ function cli(api){
16
16
"warnings" : { "format" : "<rule[,rule]+>" , "description" : "Indicate which rules to include as warnings." } ,
17
17
"ignore" : { "format" : "<rule[,rule]+>" , "description" : "Indicate which rules to ignore completely." } ,
18
18
"exclude-list" : { "format" : "<file|dir[,file|dir]+>" , "description" : "Indicate which files/directories to exclude from being linted." } ,
19
+ "config" : { "format" : "<file>" , "description" : "Reads csslint options from specified file." } ,
19
20
"version" : { "format" : "" , "description" : "Outputs the current version number." }
20
21
} ;
21
22
@@ -250,8 +251,9 @@ function cli(api){
250
251
}
251
252
252
253
253
- function processArguments ( args , options ) {
254
+ function processArguments ( args , extend ) {
254
255
var arg = args . shift ( ) ,
256
+ options = extend || { } ,
255
257
argName ,
256
258
parts ,
257
259
files = [ ] ;
@@ -293,9 +295,16 @@ function cli(api){
293
295
}
294
296
}
295
297
296
- function readConfigFile ( options ) {
297
- var data = api . readFile ( api . getFullPath ( ".csslintrc" ) ) ,
298
- json ;
298
+ function readConfigFile ( config ) {
299
+ var csslintrc = config || ".csslintrc" ,
300
+ data = api . readFile ( api . getFullPath ( csslintrc ) ) ;
301
+ return data ;
302
+ }
303
+
304
+ function readConfigData ( config ) {
305
+ var data = readConfigFile ( config ) ,
306
+ json ,
307
+ options = { } ;
299
308
if ( data ) {
300
309
if ( data . charAt ( 0 ) === "{" ) {
301
310
try {
@@ -308,45 +317,70 @@ function cli(api){
308
317
}
309
318
} catch ( e ) { }
310
319
}
311
- options = processArguments ( data . split ( / [ \s \n \r ] + / m) , options ) ;
320
+ options = processArguments ( data . split ( / [ \s \n \r ] + / m) ) ;
312
321
}
313
322
314
323
return options ;
315
324
}
316
325
326
+ function mergeOptions ( /*arguments*/ ) {
327
+ var allOptions = Array . apply ( null , arguments ) . sort ( ) ,
328
+ allOptionsCount = allOptions . length ,
329
+ options = allOptions [ 0 ] ,
330
+ overrideOptions ,
331
+ overrideOptionix ,
332
+ overrideOption ;
333
+
334
+ for ( var i = 1 ; i < allOptionsCount ; i += 1 ) {
335
+ overrideOptions = allOptions [ i ] ;
336
+
337
+ for ( overrideOptionix in overrideOptions ) {
338
+ if ( overrideOptions . hasOwnProperty ( overrideOptionix ) ) {
339
+ overrideOption = overrideOptions [ overrideOptionix ] ;
340
+ options [ overrideOptionix ] = overrideOption ;
341
+ }
342
+ }
343
+ }
317
344
345
+ return options ;
346
+ }
318
347
319
348
//-----------------------------------------------------------------------------
320
349
// Process command line
321
350
//-----------------------------------------------------------------------------
322
351
323
352
var args = api . args ,
324
353
argCount = args . length ,
325
- options = { } ;
354
+ options ,
355
+ rcOptions ,
356
+ cliOptions ;
326
357
327
- // first look for config file .csslintrc
328
- options = readConfigFile ( options ) ;
329
-
330
- // Command line arguments override config file
331
- options = processArguments ( args , options ) ;
358
+ // Preprocess command line arguments
359
+ cliOptions = processArguments ( args ) ;
332
360
333
- if ( options . help || argCount === 0 ) {
361
+ if ( cliOptions . help || argCount === 0 ) {
334
362
outputHelp ( ) ;
335
363
api . quit ( 0 ) ;
336
364
}
337
365
338
- // Validate options
339
- validateOptions ( options ) ;
340
-
341
- if ( options . version ) {
366
+ if ( cliOptions . version ) {
342
367
api . print ( "v" + CSSLint . version ) ;
343
368
api . quit ( 0 ) ;
344
369
}
345
370
346
- if ( options [ "list-rules" ] ) {
371
+ if ( cliOptions [ "list-rules" ] ) {
347
372
printRules ( ) ;
348
373
api . quit ( 0 ) ;
349
374
}
350
375
376
+ // Look for config file
377
+ rcOptions = readConfigData ( cliOptions . config ) ;
378
+
379
+ // Command line arguments override config file
380
+ options = mergeOptions ( rcOptions , cliOptions ) ;
381
+
382
+ // Validate options
383
+ validateOptions ( options ) ;
384
+
351
385
api . quit ( processFiles ( options . files , options ) ) ;
352
386
}
0 commit comments