Version 1
[yaffs-website] / node_modules / coffee-script / lib / coffee-script / optparse.js
1 // Generated by CoffeeScript 1.10.0
2 (function() {
3   var LONG_FLAG, MULTI_FLAG, OPTIONAL, OptionParser, SHORT_FLAG, buildRule, buildRules, normalizeArguments, repeat;
4
5   repeat = require('./helpers').repeat;
6
7   exports.OptionParser = OptionParser = (function() {
8     function OptionParser(rules, banner) {
9       this.banner = banner;
10       this.rules = buildRules(rules);
11     }
12
13     OptionParser.prototype.parse = function(args) {
14       var arg, i, isOption, j, k, len, len1, matchedRule, options, originalArgs, pos, ref, rule, seenNonOptionArg, skippingArgument, value;
15       options = {
16         "arguments": []
17       };
18       skippingArgument = false;
19       originalArgs = args;
20       args = normalizeArguments(args);
21       for (i = j = 0, len = args.length; j < len; i = ++j) {
22         arg = args[i];
23         if (skippingArgument) {
24           skippingArgument = false;
25           continue;
26         }
27         if (arg === '--') {
28           pos = originalArgs.indexOf('--');
29           options["arguments"] = options["arguments"].concat(originalArgs.slice(pos + 1));
30           break;
31         }
32         isOption = !!(arg.match(LONG_FLAG) || arg.match(SHORT_FLAG));
33         seenNonOptionArg = options["arguments"].length > 0;
34         if (!seenNonOptionArg) {
35           matchedRule = false;
36           ref = this.rules;
37           for (k = 0, len1 = ref.length; k < len1; k++) {
38             rule = ref[k];
39             if (rule.shortFlag === arg || rule.longFlag === arg) {
40               value = true;
41               if (rule.hasArgument) {
42                 skippingArgument = true;
43                 value = args[i + 1];
44               }
45               options[rule.name] = rule.isList ? (options[rule.name] || []).concat(value) : value;
46               matchedRule = true;
47               break;
48             }
49           }
50           if (isOption && !matchedRule) {
51             throw new Error("unrecognized option: " + arg);
52           }
53         }
54         if (seenNonOptionArg || !isOption) {
55           options["arguments"].push(arg);
56         }
57       }
58       return options;
59     };
60
61     OptionParser.prototype.help = function() {
62       var j, len, letPart, lines, ref, rule, spaces;
63       lines = [];
64       if (this.banner) {
65         lines.unshift(this.banner + "\n");
66       }
67       ref = this.rules;
68       for (j = 0, len = ref.length; j < len; j++) {
69         rule = ref[j];
70         spaces = 15 - rule.longFlag.length;
71         spaces = spaces > 0 ? repeat(' ', spaces) : '';
72         letPart = rule.shortFlag ? rule.shortFlag + ', ' : '    ';
73         lines.push('  ' + letPart + rule.longFlag + spaces + rule.description);
74       }
75       return "\n" + (lines.join('\n')) + "\n";
76     };
77
78     return OptionParser;
79
80   })();
81
82   LONG_FLAG = /^(--\w[\w\-]*)/;
83
84   SHORT_FLAG = /^(-\w)$/;
85
86   MULTI_FLAG = /^-(\w{2,})/;
87
88   OPTIONAL = /\[(\w+(\*?))\]/;
89
90   buildRules = function(rules) {
91     var j, len, results, tuple;
92     results = [];
93     for (j = 0, len = rules.length; j < len; j++) {
94       tuple = rules[j];
95       if (tuple.length < 3) {
96         tuple.unshift(null);
97       }
98       results.push(buildRule.apply(null, tuple));
99     }
100     return results;
101   };
102
103   buildRule = function(shortFlag, longFlag, description, options) {
104     var match;
105     if (options == null) {
106       options = {};
107     }
108     match = longFlag.match(OPTIONAL);
109     longFlag = longFlag.match(LONG_FLAG)[1];
110     return {
111       name: longFlag.substr(2),
112       shortFlag: shortFlag,
113       longFlag: longFlag,
114       description: description,
115       hasArgument: !!(match && match[1]),
116       isList: !!(match && match[2])
117     };
118   };
119
120   normalizeArguments = function(args) {
121     var arg, j, k, l, len, len1, match, ref, result;
122     args = args.slice(0);
123     result = [];
124     for (j = 0, len = args.length; j < len; j++) {
125       arg = args[j];
126       if (match = arg.match(MULTI_FLAG)) {
127         ref = match[1].split('');
128         for (k = 0, len1 = ref.length; k < len1; k++) {
129           l = ref[k];
130           result.push('-' + l);
131         }
132       } else {
133         result.push(arg);
134       }
135     }
136     return result;
137   };
138
139 }).call(this);