-
Notifications
You must be signed in to change notification settings - Fork 0
/
args.js
74 lines (60 loc) · 1.66 KB
/
args.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
/**
* @typedef { Object } argFormat
* @property { string } name
* @property { 'string'|'number'|'boolean'|'path' } type
* @property { string } short
* @property { string } long
* @property { boolean } isPositional
* @property { boolean } multipe
* @property { boolean } required
* @property { string } enum
*/
/**
* @param { argFormat } arg
*/
const validateArg = (arg)=>{
// TODO
return arg;
}
/**
*
* @param { argFormat[] } formats
* @param { string[] } args
*/
const main = function(formats, args = process.argv.slice(2), verbose = false) {
formats = formats.map(validateArg);
let posList = formats.filter(e => e.isPositional === true);
let result = {};
for (var i=0; i<args.length; i++){
// Long flag
if (args[i].startsWith('-')) {
let flag = args[i].replace(/^\-\-?/, ''), k, val;
if (flag.includes('=')){
[k, val] = flag.split('=');
} else {
k = flag; val = args[i+1] && !args[i+1].startsWith('-') ? args[++i] : null;
}
let fmt = formats.find(e => e[args[i].startsWith('--') ? 'long' : 'short'] == k);
if (!fmt) { verbose && console.warn(`Argument "${k}" not expected`); continue; }
if (!val && fmt.type != 'boolean') continue;
if (fmt.type === 'boolean'){
val = !val ? true : val === 'true';
}
if (fmt.multipe === true){
result[ fmt.name ] = result[ fmt.name ] || [];
result[ fmt.name ].push( val );
} else {
result[ fmt.name ] = val;
}
}
// Posiional
else {
let fmt = posList.shift();
if (!fmt) { verbose && console.warn(`No remaining positional argument for "${k}"`); continue; }
result[ fmt.name ] = args[i];
}
}
return result;
}
main()
module.exports = main;