Skip to content

Commit 8278bcf

Browse files
author
jklein
committed
Adding a validateOptions function and warning/exiting on invalid options
1 parent 0270c1a commit 8278bcf

File tree

1 file changed

+51
-11
lines changed

1 file changed

+51
-11
lines changed

src/cli/common.js

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@
55
/*global CSSLint*/
66
function cli(api){
77

8+
var globalOptions = {
9+
"help" : { "format" : "", "description" : "Displays this information."},
10+
"format" : { "format" : "<format>", "description" : "Indicate which format to use for output."},
11+
"list-rules" : { "format" : "", "description" : "Outputs all of the rules available."},
12+
"quiet" : { "format" : "", "description" : "Only output when errors are present."},
13+
"errors" : { "format" : "<rule[,rule]+>", "description" : "Indicate which rules to include as errors."},
14+
"warnings" : { "format" : "<rule[,rule]+>", "description" : "Indicate which rules to include as warnings."},
15+
"ignore" : { "format" : "<rule[,rule]+>", "description" : "Indicate which rules to ignore completely."},
16+
"exclude-list": { "format" : "<file|dir[,file|dir]+>", "description" : "Indicate which files/directories to exclude from being linted."},
17+
"version" : { "format" : "", "description" : "Outputs the current version number."}
18+
};
19+
820
//-------------------------------------------------------------------------
921
// Helper functions
1022
//-------------------------------------------------------------------------
@@ -158,20 +170,35 @@ function cli(api){
158170
* @return {void}
159171
*/
160172
function outputHelp(){
173+
var lenToPad = 40,
174+
toPrint = '',
175+
formatString = '';
176+
161177
api.print([
162178
"\nUsage: csslint-rhino.js [options]* [file|dir]*",
163179
" ",
164-
"Global Options",
165-
" --help Displays this information.",
166-
" --format=<format> Indicate which format to use for output.",
167-
" --list-rules Outputs all of the rules available.",
168-
" --quiet Only output when errors are present.",
169-
" --errors=<rule[,rule]+> Indicate which rules to include as errors.",
170-
" --warnings=<rule[,rule]+> Indicate which rules to include as warnings.",
171-
" --ignore=<rule[,rule]+> Indicate which rules to ignore completely.",
172-
" --exclude-list=<file|dir[,file|dir]+> Indicate which files/directories to exclude from being linted.",
173-
" --version Outputs the current version number."
174-
].join("\n") + "\n");
180+
"Global Options"
181+
].join("\n"));
182+
183+
for (var optionName in globalOptions) {
184+
if (globalOptions.hasOwnProperty(optionName)) {
185+
// Print the option name and the format if present
186+
toPrint += " --" + optionName;
187+
if (globalOptions[optionName].format !== "") {
188+
formatString = '=' + globalOptions[optionName].format;
189+
toPrint += formatString;
190+
} else {
191+
formatString = '';
192+
}
193+
194+
// Pad out with the appropriate number of spaces
195+
toPrint += Array(lenToPad - (optionName.length + formatString.length)).join(' ');
196+
197+
// Print the description
198+
toPrint += globalOptions[optionName].description + "\n";
199+
}
200+
}
201+
api.print(toPrint);
175202
}
176203

177204
/**
@@ -255,6 +282,16 @@ function cli(api){
255282
return options;
256283
}
257284

285+
function validateOptions(options) {
286+
for (var option_key in options) {
287+
if (!globalOptions.hasOwnProperty(option_key) && option_key !== 'files') {
288+
api.print(option_key + ' is not a valid option. Exiting...');
289+
outputHelp();
290+
api.quit(0);
291+
}
292+
}
293+
}
294+
258295
function readConfigFile(options) {
259296
var data = api.readFile(api.getFullPath(".csslintrc"));
260297
if (data) {
@@ -285,6 +322,9 @@ function cli(api){
285322
api.quit(0);
286323
}
287324

325+
// Validate options
326+
validateOptions(options);
327+
288328
if (options.version){
289329
api.print("v" + CSSLint.version);
290330
api.quit(0);

0 commit comments

Comments
 (0)