cli.js
1.88 KB
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
/**
* Phantom args parsing utilities.
*
*/
(function(phantom) {
phantom.Casper.Cli = function() {
/**
* Extract current named parameters passed to the phantom script through
* the command line. Named arguments are of the form --foo=bar ou --foo
*
*/
this.named = phantom.args.forEach(function(arg) {
if (arg.indexOf('--') === 0) {
var match = /--(.*)=(.*)\s?/i.exec(arg);
if (match) {
params[match[1]] = match[2];
} else {
var match2 = /--(.*)\s?/i.exec(arg);
if (match2) {
params[match2[1]] = true;
}
}
}
});
/**
* Checks that the specified named arguments have been passed to current
* phantom script. Dies on failure by default, but you can provide an
* onError callback for hooking on fail and do what you want.
*
*/
this.requireNamedArgs = function(list, onError) {
var missing = [], params = phantom.extractNamedArgs();
for (var i = 0; i < list.length; i++) {
var name = list[i];
if (!params.hasOwnProperty(name)) {
missing.push(name);
}
}
if (missing.length > 0) {
if (typeof(onError) === "function") {
onError(missing);
} else {
var s = missing.length > 1 ? 'are' : 'is a';
console.log(JSON.stringify({
status: "error",
message: '"' + (missing.join('", "')) + '" ' + s + ' required named parameters.'
}, null, ' '));
phantom.exit(1);
}
}
};
};
})(phantom);