-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathcommargs.js
64 lines (60 loc) · 1.51 KB
/
commargs.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
"use strict"
module.exports = function parse_args( args ) {
var
Flags = {}, Options = {}, Params = [],
optionname = '', argumentforoption = false,
arg, index, i, len, i0
;
if ( args )
{
i0 = 0;
}
else
{
args = process.argv;
// remove firt 2 args ('node' and 'this filename')
//args = args.slice(2);
i0 = 2;
}
for (i=i0,len=args.length; i<len; ++i)
{
arg = args[i];
if (arg.length > 1 && '-' == arg[0] && '-' != arg[1])
{
arg.slice(1).split('').forEach(function(c){
Flags[c] = true;
});
argumentforoption = false;
}
/*/^--/.test(arg)*/
else if (/^--/.test(arg))
{
index = arg.indexOf('=');
if (~index)
{
optionname = arg.slice(2, index);
Options[optionname] = arg.slice(index + 1);
argumentforoption = false;
}
else
{
optionname = arg.slice(2);
Options[optionname] = true;
argumentforoption = true;
}
}
else
{
if (argumentforoption)
{
Options[optionname] = arg;
}
else
{
Params.push(arg);
}
argumentforoption = false;
}
}
return {flags: Flags, options: Options, params: Params};
};