-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.js
More file actions
472 lines (418 loc) · 16.6 KB
/
Copy pathoptions.js
File metadata and controls
472 lines (418 loc) · 16.6 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
function parseOptions(path) {
//////////////////////////////////////////////////////////////////////
// BEGIN parseOptions() options CONFIGURATION
//////////////////////////////////////////////////////////////////////
var default_seperator = ',';
var program_name = 'webLauncher';
var environment_prefix = program_name.toUpperCase() + '_';
// The options list is longer than the code that parses the options.
var options = {
catch_signal: {
type: 'string', argument: 'SIG',
help: 'catch signal SIG to exit. Catchs the signal and then ' +
'cleanly exits. This is handy to use with the ' +
'--kill_children option. Example: ' +
'--catch_signals SIGINT . ' +
'By default termial signals cause the server' +
' to terminate immediately with no catcher function. ' +
'Not all signals are supported in nodejs ' +
'(SIGINT,SIGQUIT,SIGTERM are).',
// TODO: parse with this in mind make this get an array of
// strings:
array: [0, 64] // range 0 to 64 signals may be set
},
// User configuration/setting dir
config_dir: {
type: 'string', dflt: path.join(process.env.HOME, '.' +
program_name),
help: 'set the server configuration and settings directory. ' +
'The default value of CONFIG_DIR is ${HOME}' +
path.sep + '.' + program_name + '.'
},
detach: {
type: 'bool',
help: 'detach the launched programs from the server ' +
'process.'
},
dir_icon: {
type: 'regexp', dflt: '^dir_icon.*\\.(png|jpg|JPG)',
help: 'match this to find directory icon images.'
},
dir_txt: {
type: 'regexp', dflt: 'dir_description\\.txt',
help: 'match this to find directory descriptions in simple text'
},
exit_on_last: {
type: 'number', dflt: -1, // -1 is off
argument: 'MILLI_SECS',
help: 'have server exit after last connection closes.' +
' MILLI_SECS is the time to wait after the last ' +
'connection is closed. MILLI_SECONDS is -1 for not ' +
'exiting on last connection close.'
},
foot: {
type: 'string', dflt: 'foot.htm',
help: '(ADVANCED) set the launch pages suffix HTML text that' +
' is at the bottom of every launcher page. ' +
'If the full path is not given the search path will start' +
' at the installation prefix in the etc sub-directory.'
},
head: {
type: 'string', dflt: 'head.htm',
help: '(ADVANCED) set the launch pages prefix HTML text that' +
' is at the top of every launcher page. ' +
'If the full path is not given the search path will start' +
' at the installation prefix in the etc sub-directory.'
},
heading: {
type: 'string', dflt: 'Demos',
help: 'set <h1> page heading text to HEADING.'
},
help: {
type: 'bool',
help: 'print this help.'
},
hostname: {
type: 'string',
dflt: require('os').hostname(),
help: 'just uses the hostname given to print the URL at startup.' +
' This hostname value is only used to print the URL.'
},
http_port: {
type: 'string', dflt: '8080',
help: 'set the server HTTP port to HTTP_PORT. ' +
'The HTTP (non-secure) service is only available ' +
'to localhost. Setting the HTTP_PORT to "0" will ' +
'disable this HTTP service.'
},
https_port: {
type: 'string', dflt: '8383',
help: 'set the server HTTPS port to HTTPS_PORT.'
},
kill_children: {
type: 'bool', dflt: false,
help: 'kill all launched children on exiting the server.'
},
main_menu: {
type: 'string', dflt: 'main_menu',
help: 'set the name of the main menu directory.'
},
on_exit: {
type: 'string',
help: 'run PROGRAM just before the server exits.',
argument: 'PROGRAM' // as in --on_exit=PROGRAM
},
passcode: {
type: 'string',
help: 'set client passcode to PASSCODE, if set the initial URL' +
' for the service should be appended with something like:' +
' https://example.com/?passcode=PASSCODE' +
' after which additional client requests will be secured' +
' with cookies.'
},
root_dir: {
type: 'string', dflt: process.cwd(),
help: 'set the servers root document directory. The default ' +
'ROOT_DIR is the current working directory this program ' +
'ran from.'
},
run_icon: { // TODO: change this and tests and CAVE files
type: 'regexp', dflt: '^launcherIcon.*\\.(png|jpg|JPG)',
help: 'match this to find launch image icons.'
},
run_script: {
type: 'regexp', dflt: '(.*_|)run',
help: 'match this to find the files to be launched.'
},
run_txt: {
type: 'string', dflt: 'description.txt',
help: 'match this to find launch descriptions in simple text.'
},
signal: {
type: 'string', seperator: ',',
default_print: false, // Do no print "The default value is bla bla"
// in the help.
len: 2, // consumes 2 arguments making 2 strings
// example: "--signal=USR1 2314"
// separator: ' ' is the default. It separates USR1 and 2314
argument: 'SIG,PID',
// sample args for help as in "--signal=USR1,PID"
// or -signal USR1 2314 or --signal USR1,2314
// or --signal=USR1,2314 all would work
help: 'signal the process with PID with signal SIG just after the' +
' listening sockets are open. Example: --signal USR1,2354.'
},
title: {
type: 'string', dflt: 'Demos',
help: 'set the page title text to TITLE.'
}
};
//////////////////////////////////////////////////////////////////////
// END parseOptions() options CONFIGURATION
//////////////////////////////////////////////////////////////////////
var error = '';
function addError(str) {
if(error.length > 0)
error += ' ' + str;
else
error = 'bad option(s): ' + str;
}
var spaces = ' ' +
' ' +
' ',
right0 = 4, // chars left margin
right1 = 31, // position to left side of help
min_width = 50, // minimum width
width = 78; // width of text
function write(str) {
process.stdout.write(str);
}
function print(str, right, next_right) {
var i;
var out = '';
while(str.length > 0 && str.substr(0,1) == ' ') str = str.substr(1);
out = spaces.substr(0,right) + str;
if(arguments.length > 2)
right = next_right;
while(out.length > width) {
for(i=width; i>min_width && out.substr(i,1) != ' '; --i);
if(i == min_width)
// We failed to find a space so show it all.
i = width;
write(out.substr(0,i) + "\n");
out = out.substr(i);
while(out.length > 0 && out.substr(0,1) == ' ') out = out.substr(1);
if(out.length > 0)
out = spaces.substr(0,right) + out;
}
write(out + "\n");
}
function usage() {
write(' Usage: ' + program_name + " [OPTIONS]\n\n");
print('Run a ' + program_name + ' HTTP/webSocket server. ' +
program_name +
' is a nodejs web server that provides a program launcher' +
' service using HTTP, HTTPS, Web Sockets and Web Sockets' +
' over TLS.', 2);
write("\n");
print(
'The following options may be set using the command line ' +
'or via an environment variable with a with the name being ' +
'prefixed with ' + environment_prefix + ' and in all caps ' +
'like for example (bash shell):', 2);
write("\n " + environment_prefix + 'TITLE="Cool Examples" ' +
program_name + "\n\n");
print('will set the --title option to "Cool Examples".' +
' These options may also be set using a single dash ' +
' and with or without \'=\' as in for example:' +
' -title="Cool Examples" or -title "Cool Examples".', 2);
write("\n\n --------- OPTIONS --------\n\n");
var keys = Object.keys(options);
for(var i=0; i<keys.length; ++i) {
var pre = '--' + keys[i];
var opt = options[keys[i]];
if(opt.type === 'string' ||
opt.type === 'regexp' ||
opt.type === 'number')
pre += ' ' + opt.argument;
if(pre.length < right1 - right0)
pre += spaces.substr(0, right1 - right0 - pre.length);
else
pre += ' ';
if(typeof(opt.default_print) === 'undefined' && opt.dflt) {
if(opt.type === 'string')
var dflt = ' The default value of ' +
opt.argument + ' is "' + opt.dflt + '".';
else if(opt.type === 'number')
var dflt = ' The default value of ' +
opt.argument + ' is ' + opt.dflt + '.';
else if(opt.type === 'regexp')
var dflt = ' The default value of ' +
opt.argument + ' is /' + opt.dflt + '/.';
else // if(opt.type === 'bool')
var dflt = ' This is not set by default.';
} else {
var dflt = '';
}
print(pre + options[keys[i]].help + dflt, right0, right1);
write("\n");
}
process.exit(1);
}
var keys = Object.keys(options);
var alen = process.argv.length;
// initialize all options values
for(var j=0; j<keys.length; ++j) {
var name = keys[j];
var opt = options[name];
var type = opt.type;
if(type === 'bool') {
opt.value = false;
continue;
}
if(type === 'string' || type === 'regexp' || type === 'number') {
if(opt.dflt)
opt.value = opt.dflt;
else
opt.value = '';
if(opt.len && opt.len > 1) {
if(typeof(opt.seperator) === 'undefined')
opt.seperator = default_seperator;
if(typeof(opt.dflt) != 'array')
opt.value = [];
else
opt.value = opt.dflt;
} else
opt.len = 1;
if(!opt.argument)
opt.argument = name.toUpperCase();
continue;
}
error = 'bad option parsing opbject with name: ' + name;
process.exit(1);
}
///////////////// TO REMOVE ///////////////////////////
console.log('============ default option values =============');
for(var j=0; j<keys.length; ++j)
console.log(' ' + j + ' ' + keys[j] + '=' + options[keys[j]].value);
console.log('================================================');
for(var i=2; i < alen; ++i) {
var arg = process.argv[i];
for(var k=0; k<keys.length; ++k) {
var name = keys[k];
var opt = options[name];
var type = opt.type;
//console.log(name);
if(type === 'string' || type === 'regexp' || type === 'number') {
if(('--'+name === arg || '-'+name === arg) && alen > i+1) {
// --option val -option val
arg = process.argv[++i];
if(opt.len === 1)
opt.value = arg;
else {
// Getting multiple values into an array of values
// --option val0 --option val1 --option val2 ...
// or
// --option "val0 val1 val2"
var a = arg.split(opt.seperator)
opt.value = opt.value.concat(a);
}
break; // got it
}
var optlen = arg.indexOf('=') + 1;
if(optlen > 0 && ('--'+name+'=' === arg.substr(0,optlen) ||
'-'+name+'=' === arg.substr(0, optlen)) &&
arg.length > optlen) {
if(opt.len === 1) {
opt.value = arg.substr(optlen);
} else {
// Getting multiple values into an array of values
// --option=val0 --option=val1 --option=val2 ...
// or
// --option="val0 val1 val2"
// or
// "--option=val0 val1 val2"
var a = arg.substr(optlen).split(opt.seperator);
opt.value = opt.value.concat(a);
}
break;
}
// TODO: add short options like the list command 'ls -al'
}
if(type && type == 'bool') {
if('--'+name === arg || '-'+name === arg) {
// --option -option
opt.value = true;
break;
}
}
}
if(k === keys.length)
addError(arg);
}
// Check that array values are either 0 length or the len length
for(var j=0; j<keys.length; ++j) {
var key = keys[j];
var opt = options[key];
if(opt.len > 1 &&
(opt.value.length != 0 && opt.value.length != opt.len))
addError(key + ' got ' + opt.value.length +
' values, needed ' + opt.len);
}
// now parse the environment
for(var j=0; j<keys.length; ++j) {
var key = keys[j];
var name = environment_prefix + key.toUpperCase();
var env = process.env[name];
if(env) {
var opt = options[key];
if(opt.type === 'string' || opt.type === 'regexp'
|| opt.type === 'number') {
if(opt.len === 1)
opt.value = env;
else /* if(len > 1) */ {
opt.value = env.split(opt.seperator);
if(opt.value.length < opt.len)
addError('env: ' + name + '=' + env);
}
}
if(opt.type === 'bool') {
// Default to true unless it's a false thing like '0' or
// 'no' etc...
opt.value = true;
if(/(f|F|n|N|0).*/.test(env))
opt.value = false;
}
}
}
// Now convert the options that are numbers from strings to numbers
// and the regexp from strings to regexp.
for(var j=0; j<keys.length; ++j) {
var key = keys[j];
var opt = options[key];
if(opt.type === 'number') {
if(opt.len === 1) {
opt.value = parseInt(opt.value);
} else {
for(var i=0; i<opt.value.length; ++i)
opt.value[i] = parseInt(opt.value[i]);
}
} else if(opt.type === 'regexp') {
if(opt.value.substr(0,1) == '/')
opt.value = opt.value.substr(1);
if(opt.value.substr(opt.value.length-1,1) == '/')
opt.value = opt.value.substr(0, opt.value.length-1);
opt.value = new RegExp(opt.value);
}
}
if(error.length > 0) {
console.log(error + "\n\n");
options.help.value = true;
}
if(options.help.value)
usage();
var ret = {}; // returned option values
// trash all the options data leaving just the values
for(var j=0; j<keys.length; ++j)
ret[keys[j]] = options[keys[j]].value;
ret.program_name = program_name;
return ret;
}
var opt = parseOptions(require('path'));
module.exports.opt = opt;
//////////////////////////////////////////
// BEGIN TO REMOVE later
//////////////////////////////////////////
// test print
function testPrint() {
var keys = Object.keys(opt);
console.log('============== ' + keys.length +
' options =====================');
for(var j=0; j<keys.length; ++j)
console.log(' ' + j + ' ' + keys[j] + '=' + opt[keys[j]].toString());
console.log('=============================================');
}
testPrint();
//////////////////////////////////////////
// END TO REMOVE later
//////////////////////////////////////////