UNPKG

3.34 kBJavaScriptView Raw
1let consolec = require('./unsafe/console.js'),
2 options = [
3 {
4 long: '--debug',
5 short: '-d',
6 description: 'Enables debug level logging.',
7 run: function() {
8 console.warn('Debug mode enabled.');
9 consolec.setDebug(true);
10 }
11 },
12 {
13 long: '--log',
14 short: '-l',
15 description: 'Saves logs to a local file.',
16 run: function() {
17 console.warn('Logging mode enabled.');
18 consolec.setLog(true);
19 }
20 },
21 {
22 long: '--timestamp',
23 short: '-t',
24 description: 'Adds a timestamp to each output log message.',
25 run: function () {
26 console.warn('Console timestamps enabled.');
27 consolec.setTimestamp(true);
28 }
29 },
30 {
31 long: '--language',
32 short: '-i',
33 description: 'Sets the locale that should be used by the bot.',
34 expects: ['LOCALE'],
35 run: function (value) {
36 console.warn(`Locale set to "${value[0]}".`);
37 global.__i18nLocale = value[0];
38 }
39 },
40 {
41 long: '--moduledir',
42 short: '-m',
43 description: 'Sets the search path for modules used by the bot.',
44 expects: ['DIRECTORY'],
45 run: function (value) {
46 let path = require('path');
47 global.__modulesPath = path.resolve(value[0]);
48 console.warn(`Modules directory set to "${value}".`);
49 }
50 },
51 {
52 long: '--help',
53 short: '-h',
54 description: 'Shows this help.',
55 run: function() {
56 console.log('USAGE\n\tnode main.js ' + '<options...>'.cyan + '\nOPTIONS');
57 for (let i = 0; i < options.length; i++) {
58 let infoStr = '\t' + options[i].short + ', ' + options[i].long;
59 if (options[i].expects) {
60 infoStr += ' ';
61 for (let j = 0; j < options[i].expects.length; j++) {
62 infoStr += '{' + options[i].expects[j].yellow + '} ';
63 }
64 }
65 console.info(infoStr);
66 console.log('\t\t' + options[i].description);
67 }
68 process.exit(0);
69 }
70 }
71 ];
72
73exports.runArguments = function (args) {
74 for (let i = 0; i < args.length; i++) {
75 var arg = args[i],
76 pargs = options.filter(function(value) {
77 return value.short === arg || value.long === arg;
78 });
79
80 if (pargs.length === 0) {
81 continue;
82 }
83 else if (pargs.length > 1) {
84 throw new Error('Cannot have overlapping arguments.');
85 }
86
87 let vals = [],
88 count = (pargs[0].expects || {}).length || 0;
89 for (let j = 1; j <= count; j++) {
90 vals.push(args[i + j]);
91 }
92
93 pargs[0].run(vals);
94 let diff = 1 + count;
95 args.splice(i, diff);
96 i -= diff;
97 }
98};