Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/utils/getopt.js
blob: 6dd651349d90c18b59d4679cd8fabb482d95c512 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273

var GetOpt = function() {
        this.options = {};
        this.add("", null, [], false, false, GetOpt.REQUIRED_ARGUMENT); /* main arguments */
};

/** @constant */ GetOpt.NO_ARGUMENT = 1;
/** @constant */ GetOpt.REQUIRED_ARGUMENT = 2;
/** @constant */ GetOpt.OPTIONAL_ARGUMENT = 3;

/**
 * Adds an option
 * @param {string} id Identifier
 * @param {string} description Textual description
 * @param {?} value Default value
 * @param {character} shortOpt Short option character
 * @param {string} longOpt Long option string
 * @param {number} argument Argument type constant
 */
GetOpt.prototype.add = function(id, description, value, shortOpt, longOpt, argument) {
        this.options[id] = {
                description:description,
                value:value,
                longOpt:longOpt,
                shortOpt:shortOpt,
                argument:argument || GetOpt.NO_ARGUMENT
        };
}

/**
 * Retrieve option value
 * @param {string} id Identifier
 */
GetOpt.prototype.get = function(id) {
        var str = id || "";
        if (str in this.options) {
                return this.options[str].value;
        } else {
                throw new Error("Unknown option '"+id+"'");
        }
}

/**
 * Parses list of options
 * @param {array} array Command-line arguments
 */
GetOpt.prototype.parse = function(array) {
        this._pending = false;
        for (var i=0;i<array.length;i++) {
                var str = array[i];
                if (str == "--") { /* separator */
                        this._finishPending();
                        this._parseRemainder(array, i+1);
                        break;
                } else if (this._isOption(str)) { /* option string */
                        this._finishPending();
                        this._parseOption(str);
                } else { /* value */
                        if (this._pending) { /* we have pending */
                                var item = this.options[this._pending];
                                if (item.argument == GetOpt.OPTIONAL_ARGUMENT && this._onlyValues(array, i+1)) { /* there are only value remaining - this is not a pending optional value */
                                        this._finishPending();
                                        this._addValue("", str);
                                } else { /* safe to finish pending with this value */
                                        this._addValue(this._pending, str);
                                }
                                this._pending = false;
                        } else { /* add as normal argument */
                                this._addValue("", str);
                        }
                }
        }
        this._finishPending();
        this._parseRemainder(array, array.length);
}

/**
 * Formats a help
 * @returns {string}
 */
GetOpt.prototype.help = function() {
        var names = [];
        var values = [];

        for (var id in this.options) {
                if (!id) { continue; } /* do not mention main arguments */
                var item = this.options[id];
                names.push(this._helpName(item));
                values.push(item.description);
        }
        
        var indent = 0;
        for (var i=0;i<names.length;i++) {
                if (names[i].length > indent) { indent = names[i].length; }
        }
        indent++;
        
        var str = "";
        for (var i=0;i<names.length;i++) {
                str += names[i];
                for (var j=names[i].length;j<indent;j++) { str += " "; }
                str += values[i] + "\n";
        }
        return str;
}

/**
 * Are there only values remaining?
 * @param {int} index First string index to test
 * @returns {bool}
 */
GetOpt.prototype._onlyValues = function(array, index) {
        for (var i=index; i<array.length; i++) {
                var item = array[i];
                if (this._isOption(item)) { return false; }
        }
        return true;
}

/**
 * Parse remaining values after '--'
 */
GetOpt.prototype._parseRemainder = function(array, index) {
        for (var i=index; i<array.length;i++) { this._addValue("", array[i]); } /* consume all normal values */
}

/**
 * If there is a pending value, either finish it as empty (optional), or throw an error (required)
 */
GetOpt.prototype._finishPending = function() {
        if (!this._pending) { return; }
        var item = this.options[this._pending];
        if (item.argument == GetOpt.REQUIRED_ARGUMENT) { /* unsatisfied mandatory */
                throw new Error("Missing value for '"+this._pendingString+"'");
        } else { /* unsatisfied optional */
                this._addValue(this._pending, null); 
        }
}

/**
 * Parse an option string
 * @returns {id || false}
 */
GetOpt.prototype._parseOption = function(str) {
        if (str.charAt(1) == "-") {
                this._parseOptionLong(str);
        } else {
                this._parseOptionShort(str);
        }
}

GetOpt.prototype._parseOptionLong = function(str) {
        var re = str.match(/^--([a-zA-Z0-9]+)(=(.*))?$/);
        if (!re) { throw new Error("Cannot parse option '"+str+"'"); }
        
        var id = this._findOptionLong(re[1]);
        if (!id) { throw new Error("Unknown option '--"+re[1]+"'"); }
        
        var item = this.options[id];
        if (re[2]) { /* with a value */
                var value = re[3];
                this._addValue(id, value);
        } else { /* without a value */
                if (item.argument == GetOpt.NO_ARGUMENT) {
                        this._addValue(id);
                } else {
                        this._pending = id;
                        this._pendingString = "--"+re[1];
                }
        }
}

GetOpt.prototype._parseOptionShort = function(str) {
        var re = str.match(/^-([a-zA-Z0-9]+)(=(.*))?$/);
        if (!re) { throw new Error("Cannot parse option '"+str+"'"); }
        
        var list = re[1];
        for (var i=0;i<list.length;i++) {
                var character = list[i];
                var id = this._findOptionShort(character);
                if (!id) { throw new Error("Unknown option '-"+character+"'"); }
                
                var item = this.options[id];
                if (item.argument == GetOpt.NO_ARGUMENT) { /* no argument */
                        this._addValue(id);
                } else if (i+1 == list.length) { /* last character */
                        if (re[2]) {
                                this._addValue(id, re[3]);
                        } else {
                                this._pending = id;
                                this._pendingString = "-"+character;
                        }
                } else { /* middle character, argument optional/mandatory */
                        if (item.argument == GetOpt.REQUIRED_ARGUMENT) { /* mandatory -> error */
                                throw new Error("Missing value for '-"+character+"'");
                        } else { /* unsatisfied optional */
                                this._addValue(id, null);
                        }
                }
        }
}

GetOpt.prototype._isOption = function(str) {
        return (str.length > 1 && str.charAt(0) == "-");
}

GetOpt.prototype._findOptionShort = function(str) {
        for (var id in this.options) {
                if (this.options[id].shortOpt == str) { return id; }
        }
        return false;
}

GetOpt.prototype._findOptionLong = function(str) {
        for (var id in this.options) {
                if (this.options[id].longOpt == str) { return id; }
        }
        return false;
}

/**
 * Set/add a value to option
 */
GetOpt.prototype._addValue = function(id, value) {
        var item = this.options[id];
        if (item.argument == GetOpt.NO_ARGUMENT) { /* no argument -> switch to true */
                item.value = true;
                return;
        }
        
        if (item.value instanceof Array) { /* _add_ a new value */
                var val = value;
                if (value === null) { val = true; }
                item.value.push(val);
        } else { /* _set_ a new value */
                if (item.argument == GetOpt.OPTIONAL_ARGUMENT) { /* optional */
                        if (typeof(item.value) == "number" && value === null) { /* numeric - increment */
                                item.value++;
                        } else if (typeof(item.value) == "number") { /* numeric value */
                                item.value = parseFloat(value);
                        } else { /* other or null value */
                                item.value = (value === null ? true : value);
                        }
                } else { /* required */
                        item.value = value;
                }
        }
}

GetOpt.prototype._helpName = function(item) {
        var str = "";
        if (item.shortOpt) {
                str += "-"+item.shortOpt;
                if (item.argument != GetOpt.NO_ARGUMENT) { str += " "; }
                if (item.argument == GetOpt.OPTIONAL_ARGUMENT) { str += "["; }
                if (item.argument != GetOpt.NO_ARGUMENT) { str += "value"; }
                if (item.argument == GetOpt.OPTIONAL_ARGUMENT) { str += "]"; }
        }
        
        if (item.shortOpt && item.longOpt) { str += ", "; }
        
        if (item.longOpt) {
                str += "--"+item.longOpt;
                if (item.argument == GetOpt.OPTIONAL_ARGUMENT) { str += "["; }
                if (item.argument != GetOpt.NO_ARGUMENT) { str += "="; }
                if (item.argument != GetOpt.NO_ARGUMENT) { str += "value"; }
                if (item.argument == GetOpt.OPTIONAL_ARGUMENT) { str += "]"; }
        }

        return str;
}

exports.GetOpt = GetOpt;