raising an error on unrecognized options

This commit is contained in:
Jeremy Ashkenas 2010-02-25 18:54:08 -05:00
parent 5f1d3fd775
commit a23dc6b753
2 changed files with 9 additions and 1 deletions

View File

@ -12,7 +12,7 @@
// specified options, and returning it. options.arguments will be an array
// containing the remaning non-option arguments.
op.prototype.parse = function parse(args) {
var _a, _b, arg, is_option, options, rule;
var _a, _b, arg, is_option, matched_rule, options, rule;
arguments = Array.prototype.slice.call(arguments, 0);
options = {
arguments: []
@ -20,14 +20,19 @@
args = args.slice(0);
while (arg = args.shift()) {
is_option = !!(arg.match(LONG_FLAG) || arg.match(SHORT_FLAG));
matched_rule = false;
_a = this.rules;
for (_b = 0; _b < _a.length; _b++) {
rule = _a[_b];
if (rule.letter === arg || rule.flag === arg) {
options[rule.name] = rule.has_argument ? args.shift() : true;
matched_rule = true;
break;
}
}
if (is_option && !matched_rule) {
throw new Error("unrecognized option: " + arg);
}
if (!(is_option)) {
options.arguments.push(arg);
}

View File

@ -14,10 +14,13 @@ op::parse: (args) ->
args: args.slice 0
while arg: args.shift()
is_option: !!(arg.match(LONG_FLAG) or arg.match(SHORT_FLAG))
matched_rule: no
for rule in @rules
if rule.letter is arg or rule.flag is arg
options[rule.name]: if rule.has_argument then args.shift() else true
matched_rule: yes
break
throw new Error "unrecognized option: " + arg if is_option and not matched_rule
options.arguments.push arg unless is_option
options