-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
421 lines (392 loc) · 13.7 KB
/
index.js
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
/* cmdparse - Node.js command line argument parser.
*
* Copyright (c) 2020-2021 Elizabeth M. <63152708+blueblots@users.noreply.github.com>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
const path = require('path');
const {Positional, Option, Compound} = require('./lib/class.js');
const helperlib = require('./lib/helper.js');
class cmdparse {
constructor(name = `${path.basename(__filename)}`, description = '') {
this.__name = name;
this.__description = description;
}
addPositional({name, value=null, required=true, help='', position=this.positionals.length}) {
if (name === undefined || name === null) {
throw 'cmdparse: addPositional: ERROR: name must not be null or undefined';
}
let newPositional = new Positional(name, value, required, help, position);
this[name] = newPositional;
}
addOption({name, value=null, required=false, help='', flag=false, nargs=false}) {
if (name === undefined || name === null) {
throw 'cmdparse: addOption: ERROR: name must not be null or undefined';
}
let newOption = new Option(name, value, required, help, flag, nargs);
this[helperlib.getOptionName(name)] = newOption;
}
addCompound({name, commands=new cmdparse(), required=true, help=''}) {
if (name === undefined || name === null) {
throw 'cmdparse: addCompound: ERROR: name must not be null or undefined';
}
let newCompound = new Compound(name, commands, required, help);
newCompound.commands.__name = helperlib.getOptionName(name);
newCompound.commands.__description = help;
this[helperlib.getOptionName(name)] = newCompound;
}
delArg(name) {
delete this[name];
}
get compounds() {
return helperlib.getPropertiesByType(this, Compound);
}
get positionals() {
return helperlib.getPropertiesByType(this, Positional);
}
get options() {
return helperlib.getPropertiesByType(this, Option);
}
get required() {
return helperlib.getPropertiesByProperty(this, 'required');
}
get flags() {
return helperlib.getPropertiesByProperty(this, 'flag');
}
getShortHelp(prefix = '\nUSAGE: ') {
let result = `${prefix}${this.__name} `;
this.positionals.forEach(pos => {
if (pos.required === false) {
result += `[ ${pos.name} ] `;
}
else {
result += `<${pos.name}> `;
}
});
this.options.forEach(opt => {
if (opt.required === false) {
result += `[ ${opt.name} ] `;
}
else {
result += `<${opt.name}> `;
}
});
this.compounds.forEach(com => {
if (com.required === false) {
result += `[ ${com.commands.getShortHelp('')} ] `;
}
else {
result += `${com.commands.getShortHelp('')} `;
}
});
return result;
}
getLongHelp(length=40, prefix='USAGE: ') {
let result = `\n${this.__name} - ${this.__description}\n${this.getShortHelp(prefix)}\n\n`;
if (this.positionals.length > 0) {
result += helperlib.formatHelp(helperlib.extractPropertiesFromObjects(this.positionals, 'name'),
helperlib.extractPropertiesFromObjects(this.positionals, 'help'), ' ', length);
}
if (this.options.length > 0) {
result += '\nOPTIONS:\n';
result += helperlib.formatHelp(helperlib.extractPropertiesFromObjects(this.options, 'name'),
helperlib.extractPropertiesFromObjects(this.options, 'help'), ' ', length);
}
if (this.compounds.length > 0) {
result += '\nCOMMANDS:\n';
result += helperlib.formatHelp(helperlib.extractPropertiesFromObjects(this.compounds, 'name'),
helperlib.extractPropertiesFromObjects(this.compounds, 'help'), ' ', length);
}
return result;
}
toObject() {
let result = Object.create(null);
this.positionals.forEach(pos => {
result[helperlib.getOptionName(pos.name)] = pos.value;
});
this.options.forEach(opt => {
result[helperlib.getOptionName(opt.name)] = opt.value;
});
this.compounds.forEach(com => {
result[helperlib.getOptionName(com.name)] = com.commands.toObject();
});
return result;
}
toMap() {
let result = new Map();
this.positionals.forEach(pos => {
result.set(helperlib.getOptionName(pos.name), pos.value);
});
this.options.forEach(opt => {
result.set(helperlib.getOptionName(opt.name), opt.value);
});
this.compounds.forEach(com => {
result.set(helperlib.getOptionName(com.name), com.commands.toMap());
});
return result;
}
parsePositional(currentInput, positional, result) {
/* Parse a positional argument, return the argument if its actually a positional, otherwise false. */
if (this.verifyOption(currentInput) === false) {
if (this.isOptionValue(result, currentInput)) {
return false;
}
return {name: positional.name, value: currentInput, required: positional.required, position: positional.position};
}
else {
return false;
}
throw 'ERROR: Positional parse failed';
}
parseOption(currentInput, inputList, count) {
/* Parse currentInput, return an option if input is an option, otherwise false. */
if (currentInput.flag === true) {
/* if arg is a flag */
return {name: currentInput.name, value: currentInput.value, required: currentInput.required, flag: currentInput.flag};
}
if (inputList[count + 1] === undefined) {
/* if there's nothing in front of the arg */
if (currentInput.value === null) {
/* if arg has no default */
return false;
}
else {
/* use the default value */
return {name: currentInput.name, value: currentInput.value, required: currentInput.required, flag: currentInput.flag, nargs: currentInput.nargs};
}
}
else {
/* something is in front of the arg */
if (this.verifyOption(inputList[count + 1]) === false) {
let argValue;
if (currentInput.nargs !== false) {
/* process multiple values */
argValue = [];
let remainingInput = inputList.slice(count + 1);
if (currentInput.nargs === '+') {
/* one or more values are accepted */
for (let i = 0; i < remainingInput.length; i++) {
if (this.verifyOption(remainingInput[i]) === false) {
argValue.push(remainingInput[i]);
}
else {
break;
}
}
if (argValue.length === 0) {
return false;
}
}
else if (currentInput.nargs === '*') {
/* zero or more values are accepted */
for (let i = 0; i < remainingInput.length; i++) {
if (this.verifyOption(remainingInput[i]) === false) {
argValue.push(remainingInput[i]);
}
else {
break;
}
}
}
else if (Number.isInteger(parseInt(currentInput.nargs)) !== false) {
/* a certain number of values is accepted */
for (let i = 0; i < parseInt(currentInput.nargs); i++) {
if (this.verifyOption(remainingInput[i]) === false) {
argValue.push(remainingInput[i]);
}
else {
break;
}
}
}
}
else {
/* if something in front of the arg isn't an option */
argValue = inputList[count + 1];
}
return {name: currentInput.name, value: argValue, required: currentInput.required, flag: currentInput.flag, nargs: currentInput.nargs};
}
else {
/* the thing in front is an option */
return false;
}
}
throw 'ERROR: Option parse failed';
}
parse(inputList) {
/* Parse a command string (must be an array), return a cmdparse object with values matching the command string. */
let arg;
let currentInput;
let detectedPositional;
let detectedOption;
let detectedCompound;
let result = new cmdparse();
for (let i = 0; i < inputList.length; i++) {
currentInput = inputList[i];
/* Handle options */
arg = this.verifyOption(currentInput);
if (arg !== false) {
detectedOption = this.parseOption(arg, inputList, i);
if (detectedOption !== false) {
result.addOption(detectedOption);
} else {
continue;
}
}
/* Handle positionals */
arg = this.verifyPositional(inputList.indexOf(currentInput));
if (arg !== false) {
detectedPositional = this.parsePositional(currentInput, arg, result);
if (detectedPositional !== false) {
result.addPositional(detectedPositional);
} else {
continue;
}
}
/* Handle compound arguments */
arg = this.verifyCompound(currentInput);
if (arg !== false) {
detectedCompound = arg.commands.parse(inputList.slice(i + 1));
if (detectedCompound !== false) {
result.addCompound({name: arg.name, required: arg.required, commands: detectedCompound, help: arg.help});
}
else {
continue;
}
}
}
/* Check that all required arguments are included */
if (this.verifyRequired(result) === false) {
return false;
}
/* Set any default values */
this.setDefaultValues(result);
return result;
}
parseArgs(input) {
/* Shortcut for parse.toObject */
let result = this.parse(input);
if (result !== false) {
return result.toObject();
}
else {
return false;
}
}
parseArgsMap(input) {
/* Shortcut for parse.toMap */
let result = this.parse(input);
if (result !== false) {
return result.toMap();
}
else {
return false;
}
}
verifyCompound(input) {
/* Return compound argument if input matches a compound name, otherwise false. */
for (let i = 0; i < this.compounds.length; i++) {
if (this.compounds[i].name instanceof Array) {
if (this.compounds[i].name.includes(input)) {
return this.compounds[i];
}
} else if (this.compounds[i].name === input) {
return this.compounds[i];
}
}
return false;
}
verifyOption(input) {
/* Return option argument if input matches an option name, otherwise false. */
for (let i = 0; i < this.options.length; i++) {
if (this.options[i].name instanceof Array) {
if (this.options[i].name.includes(input)) {
return this.options[i];
}
} else if (this.options[i].name === input) {
return this.options[i];
}
}
return false;
}
verifyPositional(inputPosition) {
/* Return positional argument if inputPosition matches a positional's position, otherwise false. */
for (let i = 0; i < this.positionals.length; i++) {
if (this.positionals[i].position === inputPosition) {
return this.positionals[i];
}
}
return false;
}
verifyRequired(input) {
/* Check that required arguments are available. */
let count = 0;
for (let i = 0; i < this.required.length; i++) {
/* If the new cmdparse has a property with the same name
* as one in this.required, increase the count of required arguments. */
if (Object.keys(input).includes(this.required[i].name) || Object.keys(input).includes(helperlib.getOptionName(this.required[i].name))) {
count += 1;
}
}
if (count !== this.required.length) {
return false;
} else {
return true;
}
}
setDefaultValues(input) {
/* Set any default values that are needed in input cmdparse. */
/* Check positional defaults. */
for (let i = 0; i < this.positionals.length; i++) {
if (this.positionals[i].value !== null) {
if (input.positionals.includes(this.positionals[i]) === false) {
input.addPositional({...this.positionals[i]});
}
}
else {
continue;
}
}
/* Check option defaults. */
for (let i = 0; i < this.options.length; i++) {
if (this.options[i].value !== null && this.options[i].flag === false) {
if (input[helperlib.getOptionName(this.options[i].name)] === undefined) {
input.addOption({...this.options[i]});
}
}
else {
continue;
}
}
}
isOptionValue(input, value) {
/* Check that a positional argument's value is not identical to an option's value. */
let optionName;
for (let i = 0; i < this.options.length; i++) {
optionName = helperlib.getOptionName(this.options[i].name);
if (Object.keys(input).includes(optionName)) {
if (input[optionName].value === value) {
return true;
}
}
}
return false;
}
}
module.exports = cmdparse;