From a23dc6b753a9f092a9818a4410128c35a9597098 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Thu, 25 Feb 2010 18:54:08 -0500 Subject: [PATCH] raising an error on unrecognized options --- lib/optparse.js | 7 ++++++- src/optparse.coffee | 3 +++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/optparse.js b/lib/optparse.js index 14d4027d..f7af671e 100755 --- a/lib/optparse.js +++ b/lib/optparse.js @@ -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); } diff --git a/src/optparse.coffee b/src/optparse.coffee index 3e2c588e..46f09ec9 100644 --- a/src/optparse.coffee +++ b/src/optparse.coffee @@ -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