This repository was archived by the owner on Mar 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetopt.js
More file actions
316 lines (257 loc) · 6.62 KB
/
getopt.js
File metadata and controls
316 lines (257 loc) · 6.62 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
"use strict";
/*
* Copyright (c) 2016 Kelvin W Sherlock <ksherlock@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
var extra_arg = function(dash, opt, arg) {
var e = new Error(`option ${dash}${opt} does not have an argument.`);
e.option = dash + opt;
e.argument = arg;
return e;
}
var missing_arg = function(dash, opt) {
var e = new Error(`option ${dash}${opt} requires an argument.`);
e.option = dash + opt;
return e;
}
var invalid_option = function(dash, opt) {
var e = new Error(`invalid option ${dash}${opt}.`);
e.option = dash + opt;
return e;
}
const BAD_ARG = ':';
const BAD_CH = '?';
const INORDER = 1;
/*
const POSIXLY_CORRECT = false;
const FLAG_ALLARGS = false;
const FLAG_LONGONLY = false;
const FLAG_SHORTONLY = false;
const FLAG_W = false;
*/
var split_opt_string = function(so, config) {
/* convert a short-opt string into a map */
// leading + --> disable permutation (stop at first non-arg)
// leading - --> FLAG_ALLARGS
var options = new Map();
if (!so) return options;
var i = 0;
var l = so.length;
switch (so.charAt(0)) {
case '+':
config.POSIXLY_CORRECT = true;
++i;
break;
case '-':
config.FLAG_ALLARGS = true;
++i;
break;
}
while (i < l) {
var c = so.charAt(i++);
var mod = so.charAt(i);
if (mod == ':') {
options.set(c, mod);
++i;
continue;
}
if (mod == ';' && c == 'W') {
// W; means -W foo == --foo
config.FLAG_W = true;
++i;
continue;
}
options.set(c, true);
}
return options;
}
var split_opt_array = function(optarray, config) {
var options = new Map();
//remap optargs array.
(optarray || []).forEach(function(opt){
var m;
var modifier = true;
if (m = opt.match(/^([^:=!]+)(!|[:=][si])$/)) {
opt = m[1];
modifier = m[2];
if (modifier == '!') {
options.set('no-' + opt, '!!')
}
}
options.set(opt, modifier);
});
return options;
}
module.exports.getopt = function(argv, optstring, callback) {
var config = {
FLAG_SHORTONLY: true
};
var so = split_opt_string(optstring, config);
return getopt_common(argv, so, null, callback, config);
}
module.exports.getopt_long = function(argv, optstring, optarray, callback) {
var config = {};
var so = split_opt_string(optstring, config);
var lo = split_opt_array(optarray, config);
return getopt_common(argv, so, lo, callback, config);
}
module.exports.getopt_long_only = function(argv, optarray, callback) {
var config = {
FLAG_LONGONLY: true
};
var lo = split_opt_array(optarray, config);
return getopt_common(argv, null, lo, callback, config);
}
// returns state.
var parse_long_opt = function (arg, long_opts, callback, config) {
const FLAG_ALLARGS = config.FLAG_ALLARGS;
var m;
var optarg;
var modifier;
arg = arg.substr(2);
if (m = arg.match(/^([^=]+)=(.*)$/)) {
arg = m[1];
optarg = m[2];
// handle it here to simplify argument checks.
modifier = long_opts.get(arg);
switch(modifier) {
case undefined:
if (FLAG_ALLARGS) callback(INORDER, arg)
callback(BAD_CH, invalid_option('--', arg));
return null;
case '!':
case true:
callback(BAD_ARG, extra_arg('--', arg, optarg));
return null;
default:
callback(arg, optarg);
return null;
}
}
modifier = long_opts.get(arg);
switch(modifier) {
case undefined: break; // try again as no- below.
case true:
callback(arg);
return null;
case '!!':
// --no-arg => arg
callback(arg.substr(3), false);
return null;
case '!':
callback(arg, true);
return null;
default:
return ['--', arg, modifier];
}
if (FLAG_ALLARGS) callback(INORDER, arg);
else callback(BAD_CH, invalid_option('--', arg));
return null;
}
var getopt_common = function(argv, short_opts, long_opts, callback, config) {
const FLAG_SHORTONLY = config.FLAG_SHORTONLY || false;
const FLAG_LONGONLY = config.FLAG_LONGONLY || false;
const FLAG_ALLARGS = config.FLAG_ALLARGS || false;
const FLAG_W = config.FLAG_W || false;
const POSIXLY_CORRECT = config.POSIXLY_CORRECT || false;
if (!callback) callback = function(){};
if (!config) config = {};
var state = null;
var __ = false;
if (!argv) argv = process.argv.slice(2);
var rv = argv.filter(function(arg){
var m;
var optarg;
var modifier;
var opt;
if (__) return true;
if (state === '-W') {
state = parse_long_opt('--' + arg, long_opts, callback, config);
return false;
}
if (state) {
// arg for previous flag!
if (state[2].charAt(0) == '=' || arg.charAt(0) != '-') {
callback(state[1], arg);
state = null;
return false;
}
if (state[2].charAt(0) == ':') {
// optional arg w/o an option.
callback(state[1], '');
}
state = null;
}
if (arg === '--') {
__ = true;
return false;
}
if (arg.substr(0,2) == '--' && !FLAG_SHORTONLY) {
state = parse_long_opt(arg, long_opts, callback, config);
return false;
}
if (arg.charAt(0) == '-' && !FLAG_LONGONLY) {
for (var i = 1, l = arg.length; i < l; ++i) {
opt = arg.charAt(i);
modifier = short_opts.get(opt);
switch(modifier) {
case undefined:
// -W flag?
if (opt == 'W' && FLAG_W && !FLAG_SHORTONLY) {
state = '-W';
break;
}
if (FLAG_ALLARGS) callback(INORDER, opt);
else callback(BAD_CH, invalid_option('-', opt));
continue;
case true:
callback(opt);
continue;
case '!':
callback(opt, true);
continue;
default:
state = ['-', opt, modifier];
break;
}
// only here if : or =.
break;
}
if (state && ++i < l) {
optarg = arg.substr(i);
if (state === '-W') {
state = parse_long_opt('--' + optarg, long_opts, callback, config);
}
else {
opt = state[1];
callback(opt, optarg);
state = null;
}
}
return false;
}
// not an option; add it to the list.
if (POSIXLY_CORRECT) __ = true;
return true;
});
if (state){
if (state[2].charAt(0) == ':') {
callback(state[1], '');
}
else {
callback(BAD_ARG, missing_arg(state[0], state[1]));
}
}
return rv;
}