]> git.r.bdr.sh - rbdr/dotfiles/blob
6ec36045237257f3c15633e8e1adb691fca3d996
[rbdr/dotfiles] /
1 // Generated by CoffeeScript 1.8.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, matchedRule, options, originalArgs, pos, rule, seenNonOptionArg, skippingArgument, value, _i, _j, _len, _len1, _ref;
15 options = {
16 "arguments": []
17 };
18 skippingArgument = false;
19 originalArgs = args;
20 args = normalizeArguments(args);
21 for (i = _i = 0, _len = args.length; _i < _len; i = ++_i) {
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 (_j = 0, _len1 = _ref.length; _j < _len1; _j++) {
38 rule = _ref[_j];
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 letPart, lines, rule, spaces, _i, _len, _ref;
63 lines = [];
64 if (this.banner) {
65 lines.unshift("" + this.banner + "\n");
66 }
67 _ref = this.rules;
68 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
69 rule = _ref[_i];
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 tuple, _i, _len, _results;
92 _results = [];
93 for (_i = 0, _len = rules.length; _i < _len; _i++) {
94 tuple = rules[_i];
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, l, match, result, _i, _j, _len, _len1, _ref;
122 args = args.slice(0);
123 result = [];
124 for (_i = 0, _len = args.length; _i < _len; _i++) {
125 arg = args[_i];
126 if (match = arg.match(MULTI_FLAG)) {
127 _ref = match[1].split('');
128 for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) {
129 l = _ref[_j];
130 result.push('-' + l);
131 }
132 } else {
133 result.push(arg);
134 }
135 }
136 return result;
137 };
138
139 }).call(this);