phantomjs-binaries 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/modules/cli.js ADDED
@@ -0,0 +1,138 @@
1
+ /*!
2
+ * Casper is a navigation utility for PhantomJS.
3
+ *
4
+ * Documentation: http://casperjs.org/
5
+ * Repository: http://github.com/n1k0/casperjs
6
+ *
7
+ * Copyright (c) 2011-2012 Nicolas Perriault
8
+ *
9
+ * Part of source code is Copyright Joyent, Inc. and other Node contributors.
10
+ *
11
+ * Permission is hereby granted, free of charge, to any person obtaining a
12
+ * copy of this software and associated documentation files (the "Software"),
13
+ * to deal in the Software without restriction, including without limitation
14
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15
+ * and/or sell copies of the Software, and to permit persons to whom the
16
+ * Software is furnished to do so, subject to the following conditions:
17
+ *
18
+ * The above copyright notice and this permission notice shall be included
19
+ * in all copies or substantial portions of the Software.
20
+ *
21
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27
+ * DEALINGS IN THE SOFTWARE.
28
+ *
29
+ */
30
+
31
+ /*global CasperError console exports phantom require*/
32
+
33
+ var system = require('system');
34
+ var utils = require('utils');
35
+
36
+ /**
37
+ * Extracts, normalize ad organize PhantomJS CLI arguments in a dedicated
38
+ * Object.
39
+ *
40
+ * @param array phantomArgs system.args value
41
+ * @return Object
42
+ */
43
+ exports.parse = function parse(phantomArgs) {
44
+ "use strict";
45
+ var extract = {
46
+ args: [],
47
+ options: {},
48
+ raw: {
49
+ args: [],
50
+ options: {}
51
+ },
52
+ drop: function drop(what) {
53
+ if (utils.isNumber(what)) {
54
+ // deleting an arg by its position
55
+ this.args = this.args.filter(function _filter(arg, index) {
56
+ return index !== what;
57
+ });
58
+ } else if (utils.isString(what)) {
59
+ // deleting an arg by its value
60
+ this.args = this.args.filter(function _filter(arg) {
61
+ return arg !== what;
62
+ });
63
+ // deleting an option by its name (key)
64
+ var self = this;
65
+ Object.keys(this.options).forEach(function _forEach(option) {
66
+ if (option === what) {
67
+ delete self.options[what];
68
+ }
69
+ });
70
+ } else {
71
+ throw new CasperError("cannot drop argument of type " + typeof what);
72
+ }
73
+ },
74
+ has: function has(what) {
75
+ if (utils.isNumber(what)) {
76
+ return what in this.args;
77
+ } else if (utils.isString(what)) {
78
+ return what in this.options;
79
+ } else {
80
+ throw new CasperError("Unsupported cli arg tester " + typeof what);
81
+ }
82
+ },
83
+ get: function get(what) {
84
+ if (utils.isNumber(what)) {
85
+ return this.args[what];
86
+ } else if (utils.isString(what)) {
87
+ return this.options[what];
88
+ } else {
89
+ throw new CasperError("Unsupported cli arg getter " + typeof what);
90
+ }
91
+ }
92
+ };
93
+ phantomArgs.forEach(function _forEach(arg) {
94
+ if (arg.indexOf('--') === 0) {
95
+ // named option
96
+ var optionMatch = arg.match(/^--(.*?)=(.*)/i);
97
+ if (optionMatch) {
98
+ extract.options[optionMatch[1]] = castArgument(optionMatch[2]);
99
+ extract.raw.options[optionMatch[1]] = optionMatch[2];
100
+ } else {
101
+ // flag
102
+ var flagMatch = arg.match(/^--(.*)/);
103
+ if (flagMatch) {
104
+ extract.options[flagMatch[1]] = extract.raw.options[flagMatch[1]] = true;
105
+ }
106
+ }
107
+ } else {
108
+ // positional arg
109
+ extract.args.push(castArgument(arg));
110
+ extract.raw.args.push(castArgument(arg));
111
+ }
112
+ });
113
+ extract.raw = utils.mergeObjects(extract.raw, {
114
+ drop: extract.drop,
115
+ has: extract.has,
116
+ get: extract.get
117
+ });
118
+ return extract;
119
+ };
120
+
121
+ /**
122
+ * Cast a string argument to its typed equivalent.
123
+ *
124
+ * @param String arg
125
+ * @return Mixed
126
+ */
127
+ function castArgument(arg) {
128
+ "use strict";
129
+ if (arg.match(/^-?\d+$/)) {
130
+ return parseInt(arg, 10);
131
+ } else if (arg.match(/^-?\d+\.\d+$/)) {
132
+ return parseFloat(arg);
133
+ } else if (arg.match(/^(true|false)$/i)) {
134
+ return arg.trim().toLowerCase() === "true" ? true : false;
135
+ } else {
136
+ return arg;
137
+ }
138
+ }