-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.argv.js
63 lines (57 loc) · 1.41 KB
/
process.argv.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
var obop = require("obop")();
/**
* light-weight command line arguments parser for cli application
* "--foo-bar=baz" => {foo: {bar: "baz"}}
*
* @param {Array} [args] - command line arguments
* @returns {Function}
*/
function argv(args) {
var end;
var $set = {"--": []};
if (args) Array.prototype.forEach.call(args, parse);
if (!$set["--"].length) delete $set["--"];
var update = obop.update({$set: $set});
return updater;
function parse(arg) {
if (end) {
// rest of arguments
add("--", arg);
} else if (arg === "--") {
// end of options
end = true;
} else if (arg.indexOf("--")) {
// a filename
add("--", arg);
} else {
// an option
var pos = arg.indexOf("=");
var key, val;
if (pos < 0) {
key = arg.substr(2);
val = true;
} else {
key = arg.substr(2, pos - 2);
val = arg.substr(pos + 1);
}
key = key.replace(/-+/g, ".");
add(key, val);
}
}
function add(key, val) {
key = decodeURIComponent(key);
var prev = $set[key];
if (prev instanceof Array) {
prev.push(val);
} else if ("undefined" !== typeof prev) {
$set[key] = [prev, val];
} else {
$set[key] = val;
}
}
function updater(config) {
if (!config) config = {};
return update ? update(config) : config;
}
}
if ("undefined" !== typeof module) module.exports = argv;