Skip to content
This repository has been archived by the owner on Mar 30, 2022. It is now read-only.

Fixes to get wsbench working with node.js 0.6 #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 53 additions & 53 deletions lib/optparse.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Optparse.js 1.0.2 - Option Parser for Javascript
//
// Optparse.js 1.0.3 - Option Parser for Javascript
//
// Copyright (c) 2009 Johan Dahlberg
//
//
// See README.md for license.
//
//
var optparse = {};
try{ optparse = exports } catch(e) {}; // Try to export the lib for node.js
(function(self) {
var VERSION = '1.0.2';
var VERSION = '1.0.3';
var LONG_SWITCH_RE = /^--\w/;
var SHORT_SWITCH_RE = /^-\w/;
var NUMBER_RE = /^(0x[A-Fa-f0-9]+)|([0-9]+\.[0-9]+)|(\d+)$/;
Expand All @@ -17,7 +17,7 @@ var EXT_RULE_RE = /(\-\-[\w_-]+)\s+([\w\[\]_-]+)|(\-\-[\w_-]+)/;
var ARG_OPTIONAL_RE = /\[(.+)\]/;

// The default switch argument filter to use, when argument name doesnt match
// any other names.
// any other names.
var DEFAULT_FILTER = '_DEFAULT';
var PREDEFINED_FILTERS = {};

Expand All @@ -26,50 +26,50 @@ function filter_text(value) {
return value;
}

// Switch argument filter that expects an integer, HEX or a decimal value. An
// exception is throwed if the criteria is not matched.
// Switch argument filter that expects an integer, HEX or a decimal value. An
// exception is throwed if the criteria is not matched.
// Valid input formats are: 0xFFFFFFF, 12345 and 1234.1234
function filter_number(value) {
var m = NUMBER_RE(value);
var m = NUMBER_RE.exec(value);
if(m == null) throw OptError('Expected a number representative');
if(m[1]) {
// The number is in HEX format. Convert into a number, then return it
return parseInt(m[1], 16);
} else {
// The number is in regular- or decimal form. Just run in through
// The number is in regular- or decimal form. Just run in through
// the float caster.
return parseFloat(m[2] || m[3]);
}
};

// Switch argument filter that expects a Date expression. The date string MUST be
// formated as: "yyyy-mm-dd" An exception is throwed if the criteria is not
// matched. An DATE object is returned on success.
// formated as: "yyyy-mm-dd" An exception is throwed if the criteria is not
// matched. An DATE object is returned on success.
function filter_date(value) {
var m = DATE_RE(value);
var m = DATE_RE.exec(value);
if(m == null) throw OptError('Expected a date representation in the "yyyy-mm-dd" format.');
return new Date(parseInt(m[0]), parseInt(m[1]), parseInt(m[2]));
};

// Switch argument filter that expects an email address. An exception is throwed
// if the criteria doesn`t match.
// if the criteria doesn`t match.
function filter_email(value) {
var m = EMAIL_RE(value);
var m = EMAIL_RE.exec(value);
if(m == null) throw OptError('Excpeted an email address.');
return m[1];
}

// Register all predefined filters. This dict is used by each OptionParser
// instance, when parsing arguments. Custom filters can be added to the parser
// instance by calling the "add_filter" -method.
// Register all predefined filters. This dict is used by each OptionParser
// instance, when parsing arguments. Custom filters can be added to the parser
// instance by calling the "add_filter" -method.
PREDEFINED_FILTERS[DEFAULT_FILTER] = filter_text;
PREDEFINED_FILTERS['TEXT'] = filter_text;
PREDEFINED_FILTERS['NUMBER'] = filter_number;
PREDEFINED_FILTERS['DATE'] = filter_date;
PREDEFINED_FILTERS['EMAIL'] = filter_email;

// Buildes rules from a switches collection. The switches collection is defined
// when constructing a new OptionParser object.
// when constructing a new OptionParser object.
function build_rules(filters, arr) {
var rules = [];
for(var i=0; i<arr.length; i++) {
Expand All @@ -80,7 +80,7 @@ function build_rules(filters, arr) {
rule = build_rule(filters, r[0]);
break;
case 2:
var expr = LONG_SWITCH_RE(r[0]) ? 0 : 1;
var expr = LONG_SWITCH_RE.test(r[0]) ? 0 : 1;
var alias = expr == 0 ? -1 : 0;
var desc = alias == -1 ? 1 : -1;
rule = build_rule(filters, r[alias], r[expr], r[desc]);
Expand All @@ -97,8 +97,8 @@ function build_rules(filters, arr) {
return rules;
}

// Builds a rule with specified expression, short style switch and help. This
// function expects a dict with filters to work correctly.
// Builds a rule with specified expression, short style switch and help. This
// function expects a dict with filters to work correctly.
//
// Return format:
// name The name of the switch.
Expand All @@ -107,8 +107,8 @@ function build_rules(filters, arr) {
// decl The declaration expression (the input expression)
// desc The optional help section for the switch
// optional_arg Indicates that switch argument is optional
// filter The filter to use when parsing the arg. An
// <<undefined>> value means that the switch does
// filter The filter to use when parsing the arg. An
// <<undefined>> value means that the switch does
// not take anargument.
function build_rule(filters, short, expr, desc) {
var optional, filter;
Expand All @@ -118,30 +118,30 @@ function build_rule(filters, short, expr, desc) {
if(m[2] != undefined) {
// A switch argument is expected. Check if the argument is optional,
// then find a filter that suites.
var optional_match = ARG_OPTIONAL_RE(m[2]);
var optional_match = ARG_OPTIONAL_RE.test(m[2]);
var filter_name = optional_match === null ? m[2] : optional_match[1];
optional = optional_match !== null;
filter = filters[filter_name];
if(filter === undefined) filter = filters[DEFAULT_FILTER];
}
return {
name: long.substr(2),
short: short,
name: long.substr(2),
short: short,
long: long,
decl: expr,
desc: desc,
desc: desc,
optional_arg: optional,
filter: filter
filter: filter
}
}

// Loop's trough all elements of an array and check if there is valid
// options expression within. An valid option is a token that starts
// options expression within. An valid option is a token that starts
// double dashes. E.G. --my_option
function contains_expr(arr) {
if(!arr || !arr.length) return false;
var l = arr.length;
while(l-- > 0) if(LONG_SWITCH_RE(arr[l])) return true;
while(l-- > 0) if(LONG_SWITCH_RE.test(arr[l])) return true;
return false;
}

Expand All @@ -158,7 +158,7 @@ function extend(dest, src) {
function spaces(arg1, arg2) {
var l, builder = [];
if(arg1.constructor === Number) {
l = arg1;
l = arg1;
} else {
if(arg1.length == arg2) return arg1;
l = arg2 - arg1.length;
Expand Down Expand Up @@ -198,7 +198,7 @@ function OptionParser(rules) {
}

OptionParser.prototype = {

// Adds args and switchs handler.
on: function(value, fn) {
if(value.constructor === Function ) {
Expand All @@ -209,32 +209,32 @@ OptionParser.prototype = {
this.on_switches[value] = fn;
}
},

// Adds a custom filter to the parser. It's possible to override the
// default filter by passing the value "_DEFAULT" to the ´´name´´
// argument. The name of the filter is automatically transformed into
// upper case.
// argument. The name of the filter is automatically transformed into
// upper case.
filter: function(name, fn) {
this.filters[name.toUpperCase()] = fn;
},
// Parses specified args. Returns remaining arguments.

// Parses specified args. Returns remaining arguments.
parse: function(args) {
var result = [], callback;
var rules = build_rules(this.filters, this._rules);
var tokens = args.concat([]);
while((token = tokens.shift()) && this._halt == false) {
if(LONG_SWITCH_RE(token) || SHORT_SWITCH_RE(token)) {
if(LONG_SWITCH_RE.test(token) || SHORT_SWITCH_RE.test(token)) {
var arg = undefined;
// The token is a long or a short switch. Get the corresponding
// rule, filter and handle it. Pass the switch to the default
// The token is a long or a short switch. Get the corresponding
// rule, filter and handle it. Pass the switch to the default
// handler if no rule matched.
for(var i = 0; i < rules.length; i++) {
var rule = rules[i];
if(rule.long == token || rule.short == token) {
if(rule.filter !== undefined) {
arg = tokens.shift();
if(!LONG_SWITCH_RE(arg) && !SHORT_SWITCH_RE(arg)) {
if(!LONG_SWITCH_RE.test(arg) && !SHORT_SWITCH_RE.test(arg)) {
try {
arg = rule.filter(arg);
} catch(e) {
Expand All @@ -245,16 +245,16 @@ OptionParser.prototype = {
} else {
throw OptError('Expected switch argument.');
}
}
}
callback = this.on_switches[rule.name];
if (!callback) callback = this.on_switches['*'];
if(callback) callback.apply(this, [rule.name, arg]);
break;
}
}
}
if(i == rules.length) this.default_handler.apply(this, [token]);
} else {
// Did not match long or short switch. Parse the token as a
// Did not match long or short switch. Parse the token as a
// normal argument.
callback = this.on_args[result.length];
result.push(token);
Expand All @@ -263,33 +263,33 @@ OptionParser.prototype = {
}
return this._halt ? this.on_halt.apply(this, []) : result;
},
// Returns an Array with all defined option rules

// Returns an Array with all defined option rules
options: function() {
return build_rules(this.filters, this._rules);
},

// Add an on_halt callback if argument ´´fn´´ is specified. on_switch handlers can
// Add an on_halt callback if argument ´´fn´´ is specified. on_switch handlers can
// call instance.halt to abort the argument parsing. This can be useful when
// displaying help or version information.
halt: function(fn) {
this._halt = fn === undefined
if(fn) this.on_halt = fn;
},

// Returns a string representation of this OptionParser instance.
toString: function() {
var builder = [this.banner, '', this.options_title],
var builder = [this.banner, '', this.options_title],
shorts = false, longest = 0, rule;
var rules = build_rules(this.filters, this._rules);
for(var i = 0; i < rules.length; i++) {
rule = rules[i];
// Quick-analyze the options.
// Quick-analyze the options.
if(rule.short) shorts = true;
if(rule.decl.length > longest) longest = rule.decl.length;
}
for(var i = 0; i < rules.length; i++) {
var text;
var text;
rule = rules[i];
if(shorts) {
if(rule.short) text = spaces(2) + rule.short + ', ';
Expand All @@ -306,4 +306,4 @@ OptionParser.prototype = {
self.VERSION = VERSION;
self.OptionParser = OptionParser;

})(optparse);
})(optparse);
Loading