Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Updated dependencies

  • Loading branch information...
commit ccfbbb5424f83c8f3503b28e1df5e3da2d4f8b39 1 parent 82b907d
Jon Brisbin authored December 11, 2010

Showing 2 changed files with 5 additions and 310 deletions. Show diff stats Hide diff stats

  1. 309  optparse.js
  2. 6  package.json
309  optparse.js
... ...
@@ -1,309 +0,0 @@
1  
-//  Optparse.js 1.0.2 - Option Parser for Javascript 
2  
-// 
3  
-//  Copyright (c) 2009 Johan Dahlberg
4  
-// 
5  
-//  See README.md for license.
6  
-//                                                        
7  
-var optparse = {};
8  
-try{ optparse = exports } catch(e) {}; // Try to export the lib for node.js
9  
-(function(self) {
10  
-var VERSION = '1.0.2';
11  
-var LONG_SWITCH_RE = /^--\w/;
12  
-var SHORT_SWITCH_RE = /^-\w/;
13  
-var NUMBER_RE = /^(0x[A-Fa-f0-9]+)|([0-9]+\.[0-9]+)|(\d+)$/;
14  
-var DATE_RE = /^\d{4}-(0[0-9]|1[0,1,2])-([0,1,2][0-9]|3[0,1])$/;
15  
-var EMAIL_RE = /^([0-9a-zA-Z]+([_.-]?[0-9a-zA-Z]+)*@[0-9a-zA-Z]+[0-9,a-z,A-Z,.,-]*(.){1}[a-zA-Z]{2,4})+$/;
16  
-var EXT_RULE_RE = /(\-\-[\w_-]+)\s+([\w\[\]_-]+)|(\-\-[\w_-]+)/;
17  
-var ARG_OPTIONAL_RE = /\[(.+)\]/;
18  
-
19  
-// The default switch argument filter to use, when argument name doesnt match
20  
-// any other names. 
21  
-var DEFAULT_FILTER = '_DEFAULT';
22  
-var PREDEFINED_FILTERS = {};
23  
-
24  
-// The default switch argument filter. Parses the argument as text.
25  
-function filter_text(value) {
26  
-    return value;
27  
-}
28  
-
29  
-// Switch argument filter that expects an integer, HEX or a decimal value. An 
30  
-// exception is throwed if the criteria is not matched. 
31  
-// Valid input formats are: 0xFFFFFFF, 12345 and 1234.1234
32  
-function filter_number(value) {
33  
-    var m = NUMBER_RE(value);
34  
-    if(m == null) throw OptError('Expected a number representative');
35  
-    if(m[1]) {
36  
-        // The number is in HEX format. Convert into a number, then return it
37  
-        return parseInt(m[1], 16);
38  
-    } else {
39  
-        // The number is in regular- or decimal form. Just run in through 
40  
-        // the float caster.
41  
-        return parseFloat(m[2] || m[3]);
42  
-    }
43  
-};
44  
-
45  
-// Switch argument filter that expects a Date expression. The date string MUST be
46  
-// formated as: "yyyy-mm-dd" An exception is throwed if the criteria is not 
47  
-// matched. An DATE object is returned on success. 
48  
-function filter_date(value) {
49  
-    var m = DATE_RE(value);
50  
-    if(m == null) throw OptError('Expected a date representation in the "yyyy-mm-dd" format.');
51  
-    return new Date(parseInt(m[0]), parseInt(m[1]), parseInt(m[2]));
52  
-};
53  
-
54  
-// Switch argument filter that expects an email address. An exception is throwed
55  
-// if the criteria doesn`t match. 
56  
-function filter_email(value) {
57  
-    var m = EMAIL_RE(value);
58  
-    if(m == null) throw OptError('Excpeted an email address.');
59  
-    return m[1];
60  
-}
61  
-
62  
-// Register all predefined filters. This dict is used by each OptionParser 
63  
-// instance, when parsing arguments. Custom filters can be added to the parser 
64  
-// instance by calling the "add_filter" -method. 
65  
-PREDEFINED_FILTERS[DEFAULT_FILTER] = filter_text;
66  
-PREDEFINED_FILTERS['TEXT'] = filter_text;
67  
-PREDEFINED_FILTERS['NUMBER'] = filter_number;
68  
-PREDEFINED_FILTERS['DATE'] = filter_date;
69  
-PREDEFINED_FILTERS['EMAIL'] = filter_email;
70  
-
71  
-//  Buildes rules from a switches collection. The switches collection is defined
72  
-//  when constructing a new OptionParser object. 
73  
-function build_rules(filters, arr) {
74  
-    var rules = [];
75  
-    for(var i=0; i<arr.length; i++) {
76  
-        var r = arr[i], rule
77  
-        if(!contains_expr(r)) throw OptError('Rule MUST contain an option.');
78  
-        switch(r.length) {
79  
-            case 1:
80  
-                rule = build_rule(filters, r[0]);
81  
-                break;
82  
-            case 2:
83  
-                var expr = LONG_SWITCH_RE(r[0]) ? 0 : 1;
84  
-                var alias = expr == 0 ? -1 : 0;
85  
-                var desc = alias == -1 ? 1 : -1;
86  
-                rule = build_rule(filters, r[alias], r[expr], r[desc]);
87  
-                break;
88  
-            case 3:
89  
-                rule = build_rule(filters, r[0], r[1], r[2]);
90  
-                break;
91  
-            default:
92  
-            case 0:
93  
-                continue;
94  
-        }
95  
-        rules.push(rule)
96  
-    }
97  
-    return rules;
98  
-}
99  
-
100  
-//  Builds a rule with specified expression, short style switch and help. This 
101  
-//  function expects a dict with filters to work correctly. 
102  
-//
103  
-//  Return format:
104  
-//      name               The name of the switch.
105  
-//      short              The short style switch
106  
-//      long               The long style switch
107  
-//      decl               The declaration expression (the input expression)
108  
-//      desc               The optional help section for the switch
109  
-//      optional_arg       Indicates that switch argument is optional
110  
-//      filter             The filter to use when parsing the arg. An 
111  
-//                         <<undefined>> value means that the switch does 
112  
-//                         not take anargument.
113  
-function build_rule(filters, short, expr, desc) {
114  
-    var optional, filter;
115  
-    var m = expr.match(EXT_RULE_RE);
116  
-    if(m == null) throw OptError('The switch is not well-formed.');
117  
-    var long = m[1] || m[3];
118  
-    if(m[2] != undefined) {
119  
-        // A switch argument is expected. Check if the argument is optional,
120  
-        // then find a filter that suites.
121  
-        var optional_match = ARG_OPTIONAL_RE(m[2]);
122  
-        var filter_name = optional_match === null ? m[2] : optional_match[1];
123  
-        optional = optional_match !== null;
124  
-        filter = filters[filter_name];
125  
-        if(filter === undefined) filter = filters[DEFAULT_FILTER];
126  
-    }
127  
-    return {
128  
-        name: long.substr(2),       
129  
-        short: short,               
130  
-        long: long,
131  
-        decl: expr,
132  
-        desc: desc,                 
133  
-        optional_arg: optional,
134  
-        filter: filter              
135  
-    }
136  
-}
137  
-
138  
-// Loop's trough all elements of an array and check if there is valid
139  
-// options expression within. An valid option is a token that starts 
140  
-// double dashes. E.G. --my_option
141  
-function contains_expr(arr) {
142  
-    if(!arr || !arr.length) return false;
143  
-    var l = arr.length;
144  
-    while(l-- > 0) if(LONG_SWITCH_RE(arr[l])) return true;
145  
-    return false;
146  
-}
147  
-
148  
-// Extends destination object with members of source object
149  
-function extend(dest, src) {
150  
-    var result = dest;
151  
-    for(var n in src) {
152  
-        result[n] = src[n];
153  
-    }
154  
-    return result;
155  
-}
156  
-
157  
-// Appends spaces to match specified number of chars
158  
-function spaces(arg1, arg2) {
159  
-    var l, builder = [];
160  
-    if(arg1.constructor === Number) {
161  
-        l = arg1;  
162  
-    } else {
163  
-        if(arg1.length == arg2) return arg1;
164  
-        l = arg2 - arg1.length;
165  
-        builder.push(arg1);
166  
-    }
167  
-    while(l-- > 0) builder.push(' ');
168  
-    return builder.join('');
169  
-}
170  
-
171  
-//  Create a new Parser object that can be used to parse command line arguments.
172  
-//
173  
-//
174  
-function Parser(rules) {
175  
-    return new OptionParser(rules);
176  
-}
177  
-
178  
-// Creates an error object with specified error message.
179  
-function OptError(msg) {
180  
-    return new function() {
181  
-        this.msg = msg;
182  
-        this.toString = function() {
183  
-            return this.msg;
184  
-        }
185  
-    }
186  
-}
187  
-
188  
-function OptionParser(rules) {
189  
-    this.banner = 'Usage: [Options]';
190  
-    this.options_title = 'Available options:'
191  
-    this._rules = rules;
192  
-    this._halt = false;
193  
-    this.filters = extend({}, PREDEFINED_FILTERS);
194  
-    this.on_args = {};
195  
-    this.on_switches = {};
196  
-    this.on_halt = function() {};
197  
-    this.default_handler = function() {};
198  
-}
199  
-
200  
-OptionParser.prototype = {
201  
-    
202  
-    // Adds args and switchs handler.
203  
-    on: function(value, fn) {
204  
-        if(value.constructor === Function ) {
205  
-            this.default_handler = value;
206  
-        } else if(value.constructor === Number) {
207  
-            this.on_args[value] = fn;
208  
-        } else {
209  
-            this.on_switches[value] = fn;
210  
-        }
211  
-    },
212  
-    
213  
-    // Adds a custom filter to the parser. It's possible to override the
214  
-    // default filter by passing the value "_DEFAULT" to the ´´name´´
215  
-    // argument. The name of the filter is automatically transformed into 
216  
-    // upper case. 
217  
-    filter: function(name, fn) {
218  
-        this.filters[name.toUpperCase()] = fn;
219  
-    },
220  
-    
221  
-    // Parses specified args. Returns remaining arguments. 
222  
-    parse: function(args) {
223  
-        var result = [], callback;
224  
-        var rules = build_rules(this.filters, this._rules);
225  
-        var tokens = args.concat([]);
226  
-        while((token = tokens.shift()) && this._halt == false) {
227  
-            if(LONG_SWITCH_RE(token) || SHORT_SWITCH_RE(token)) {
228  
-                var arg = undefined;
229  
-                // The token is a long or a short switch. Get the corresponding 
230  
-                // rule, filter and handle it. Pass the switch to the default 
231  
-                // handler if no rule matched.
232  
-                for(var i = 0; i < rules.length; i++) {
233  
-                    var rule = rules[i];
234  
-                    if(rule.long == token || rule.short == token) {
235  
-                        if(rule.filter !== undefined) {
236  
-                            arg = tokens.shift();
237  
-                            if(!LONG_SWITCH_RE(arg) && !SHORT_SWITCH_RE(arg)) {
238  
-                                try {
239  
-                                    arg = rule.filter(arg);
240  
-                                } catch(e) {
241  
-                                    throw OptError(token + ': ' + e.toString());
242  
-                                }
243  
-                            } else if(rule.optional_arg) {
244  
-                                tokens.unshift(arg);
245  
-                            } else {
246  
-                                throw OptError('Expected switch argument.');
247  
-                            }
248  
-                        } 
249  
-                        callback = this.on_switches[rule.name];
250  
-                        if (!callback) callback = this.on_switches['*'];
251  
-                        if(callback) callback.apply(this, [rule.name, arg]);
252  
-                        break;
253  
-                    } 
254  
-                }
255  
-                if(i == rules.length) this.default_handler.apply(this, [token]);
256  
-            } else {
257  
-                // Did not match long or short switch. Parse the token as a 
258  
-                // normal argument.
259  
-                callback = this.on_args[result.length];
260  
-                result.push(token);
261  
-                if(callback) callback.apply(this, [token]);
262  
-            }
263  
-        }
264  
-        return this._halt ? this.on_halt.apply(this, []) : result;
265  
-    },
266  
-    
267  
-    // Returns an Array with all defined option rules 
268  
-    options: function() {
269  
-        return build_rules(this.filters, this._rules);
270  
-    },
271  
-
272  
-    // Add an on_halt callback if argument ´´fn´´ is specified. on_switch handlers can 
273  
-    // call instance.halt to abort the argument parsing. This can be useful when
274  
-    // displaying help or version information.
275  
-    halt: function(fn) {
276  
-        this._halt = fn === undefined
277  
-        if(fn) this.on_halt = fn;
278  
-    },
279  
-    
280  
-    // Returns a string representation of this OptionParser instance.
281  
-    toString: function() {
282  
-        var builder = [this.banner, '', this.options_title], 
283  
-            shorts = false, longest = 0, rule;
284  
-        var rules = build_rules(this.filters, this._rules);
285  
-        for(var i = 0; i < rules.length; i++) {
286  
-            rule = rules[i];
287  
-            // Quick-analyze the options. 
288  
-            if(rule.short) shorts = true;
289  
-            if(rule.decl.length > longest) longest = rule.decl.length;
290  
-        }
291  
-        for(var i = 0; i < rules.length; i++) {
292  
-            var text; 
293  
-            rule = rules[i];
294  
-            if(shorts) {
295  
-                if(rule.short) text = spaces(2) + rule.short + ', ';
296  
-                else text = spaces(6);
297  
-            }
298  
-            text += spaces(rule.decl, longest) + spaces(3);
299  
-            text += rule.desc;
300  
-            builder.push(text);
301  
-        }
302  
-        return builder.join('\n');
303  
-    }
304  
-}
305  
-
306  
-self.VERSION = VERSION;
307  
-self.OptionParser = OptionParser;
308  
-
309  
-})(optparse);
6  package.json
@@ -4,5 +4,9 @@
4 4
   "version": "0.1.0",
5 5
   "author": "J. Brisbin <jon@jbrisbin.com>",
6 6
   "main": "./index.js",
7  
-  "bin": { "noderlog": "./viewer" }
  7
+  "bin": { "noderlog": "./viewer" },
  8
+  "dependencies": {
  9
+    "riak-js": ">= 0.3.0beta4",
  10
+    "optparse": ">= 1.0.1"
  11
+  }
8 12
 }

0 notes on commit ccfbbb5

Please sign in to comment.
Something went wrong with that request. Please try again.