improving errors for undefined options, and error messages for compile attempts on nonexistent files

This commit is contained in:
Jeremy Ashkenas
2010-02-25 18:36:43 -05:00
parent 213ae1430e
commit c62f93f930
4 changed files with 14 additions and 9 deletions

View File

@@ -59,8 +59,13 @@
compile_scripts = function compile_scripts() { compile_scripts = function compile_scripts() {
var _a, _b, _c, compile, source; var _a, _b, _c, compile, source;
compile = function compile(source) { compile = function compile(source) {
return fs.readFile(source, function(err, code) { return path.exists(source, function(exists) {
return compile_script(source, code); if (!(exists)) {
throw new Error('File not found: ' + source);
}
return fs.readFile(source, function(err, code) {
return compile_script(source, code);
});
}); });
}; };
_a = []; _b = sources; _a = []; _b = sources;

View File

@@ -19,13 +19,12 @@
}; };
args = args.slice(0); args = args.slice(0);
while (arg = args.shift()) { while (arg = args.shift()) {
is_option = false; is_option = !!(arg.match(LONG_FLAG) || arg.match(SHORT_FLAG));
_a = this.rules; _a = this.rules;
for (_b = 0; _b < _a.length; _b++) { for (_b = 0; _b < _a.length; _b++) {
rule = _a[_b]; rule = _a[_b];
if (rule.letter === arg || rule.flag === arg) { if (rule.letter === arg || rule.flag === arg) {
options[rule.name] = rule.has_argument ? args.shift() : true; options[rule.name] = rule.has_argument ? args.shift() : true;
is_option = true;
break; break;
} }
} }
@@ -56,7 +55,7 @@
return lines.join('\n'); return lines.join('\n');
}; };
// Regex matchers for option flags. // Regex matchers for option flags.
LONG_FLAG = /^(--[\w\-]+)/; LONG_FLAG = /^(--\w[\w\-]+)/;
SHORT_FLAG = /^(-\w+)/; SHORT_FLAG = /^(-\w+)/;
OPTIONAL = /\[(.+)\]/; OPTIONAL = /\[(.+)\]/;
// Build rules from a list of valid switch tuples in the form: // Build rules from a list of valid switch tuples in the form:

View File

@@ -63,7 +63,9 @@ version: ->
# or JSLint results. # or JSLint results.
compile_scripts: -> compile_scripts: ->
compile: (source) -> compile: (source) ->
fs.readFile source, (err, code) -> compile_script(source, code) path.exists source, (exists) ->
throw new Error 'File not found: ' + source unless exists
fs.readFile source, (err, code) -> compile_script(source, code)
compile(source) for source in sources compile(source) for source in sources
# Compile a single source script, containing the given code, according to the # Compile a single source script, containing the given code, according to the

View File

@@ -13,11 +13,10 @@ op::parse: (args) ->
options: {arguments: []} options: {arguments: []}
args: args.slice 0 args: args.slice 0
while arg: args.shift() while arg: args.shift()
is_option: false is_option: !!(arg.match(LONG_FLAG) or arg.match(SHORT_FLAG))
for rule in @rules for rule in @rules
if rule.letter is arg or rule.flag is arg if rule.letter is arg or rule.flag is arg
options[rule.name]: if rule.has_argument then args.shift() else true options[rule.name]: if rule.has_argument then args.shift() else true
is_option: true
break break
options.arguments.push arg unless is_option options.arguments.push arg unless is_option
options options
@@ -33,7 +32,7 @@ op::help: ->
lines.join('\n') lines.join('\n')
# Regex matchers for option flags. # Regex matchers for option flags.
LONG_FLAG: /^(--[\w\-]+)/ LONG_FLAG: /^(--\w[\w\-]+)/
SHORT_FLAG: /^(-\w+)/ SHORT_FLAG: /^(-\w+)/
OPTIONAL: /\[(.+)\]/ OPTIONAL: /\[(.+)\]/