This repository has been archived by the owner on Jun 19, 2020. It is now read-only.
/
cli.html
232 lines (207 loc) · 11.5 KB
/
cli.html
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<!DOCTYPE html><html><head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="robots" content="index, follow">
<link rel="canonical" href="http://casperjs.org/">
<title>API documentation | CasperJS 1.0.0-RC2</title>
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1">
<meta name="keywords" content="phantomjs, phantom, casperjs, casper, browser, javascript, navigation, scripting, test, testing, scrape, scraping">
<meta name="description" content="CasperJS is a browser navigation scripting & testing utility written in Javascript on top of PhantomJS.">
<meta name="google-site-verification" content="SEcGmVv4IsGYP6-xMJquQcrUrYjIZNXyX0VGfly2m1o">
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Asap:400,700,400italic,700italic&subset=latin,latin-ext">
<link rel="stylesheet" href="prettify/prettify.css?1351019514358">
<link rel="stylesheet" href="css/casper.css?1351019514358">
<link rel="shortcut icon" href="favicon.ico">
</head>
<body>
<a href="http://github.com/n1k0/casperjs" class="contribute">
<img src="https://s3.amazonaws.com/github/ribbons/forkme_right_orange_ff7600.png" alt="Fork me on GitHub">
</a>
<div class="navbar navbar-fixed-top" id="topbar">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="./index.html">CasperJS</a>
<div class="nav-collapse">
<ul class="nav">
<li><a href="installation.html">Install</a></li>
<li><a href="quickstart.html">Quickstart</a></li>
<li class="dropdown" id="api-menu">
<a class="dropdown-toggle" data-toggle="dropdown" data-target="#api-menu" href="api.html">
<abbr title="Application Programming Interface">API</abbr>
<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><a href="api.html#intro">Home</a></li>
<li class="divider"></li>
<li><a href="api.html#casper">Casper API</a></li>
<li><a href="api.html#client-utils">Client-side utils</a></li>
<li><a href="api.html#colorizer">Colorizer API</a></li>
<li><a href="api.html#tester">Tester API</a></li>
<li><a href="api.html#utils">Utils API</a></li>
</ul>
</li>
<li class="active"><a href="cli.html"><abbr title="Command Line Interface">CLI</abbr></a></li>
<li><a href="selectors.html">Selectors</a></li>
<li><a href="events-filters.html">Events</a></li>
<li><a href="logging.html">Logging</a></li>
<li><a href="extending.html">Extending</a></li>
<li><a href="testing.html">Testing</a></li>
<li><a href="debugging.html">Debugging</a></li>
<li><a href="faq.html"><abbr title="Frequently Asked Questions">FAQ</abbr></a></li>
<li><a href="https://groups.google.com/forum/#!forum/casperjs">Support</a></li>
</ul>
</div>
</div>
</div>
</div>
<div class="container" id="overview">
<div class="content"><section>
<div class="page-header">
<h1>
<span class="section-title">Command Line Interface</span>
<small class="section-subtitle">positional arguments & options exposed</small>
</h1>
</div>
<div class="section-content"><p>CasperJS ships with a built-in command line parser on top of PhantomJS'
one, located in the <code>cli</code> module; it exposes passed arguments as
<strong>positional ones</strong> and <strong>named options</strong>
</p>
<p>But no worries for manipulating the <code>cli</code> module parsing API, a <code>Casper</code>
instance always contains a ready to use <code>cli</code> property, allowing easy
access of all these parameters.
</p>
<p>Let's consider this simple casper script:
</p>
<pre><code class="javascript">var casper = require("casper").create();
casper.echo("Casper CLI passed args:");
require("utils").dump(casper.cli.args);
casper.echo("Casper CLI passed options:");
require("utils").dump(casper.cli.options);
casper.exit();</code></pre>
<p><span class="label label-info">Note</span> Please note the two <code>casper-path</code>
and <code>cli</code> options; these are passed to the casper script through the <code>casperjs</code>
Python executable.
</p>
<p>Execution results:
</p>
<pre><code>$ casperjs test.js arg1 arg2 arg3 --foo=bar --plop anotherarg
Casper CLI passed args: [
"arg1",
"arg2",
"arg3",
"anotherarg"
]
Casper CLI passed options: {
"casper-path": "/Users/niko/Sites/casperjs",
"cli": true,
"foo": "bar",
"plop": true
}</code></pre>
<p>Getting, checking or dropping parameters:
</p>
<pre><code class="javascript">var casper = require("casper").create();
casper.echo(casper.cli.has(0));
casper.echo(casper.cli.get(0));
casper.echo(casper.cli.has(3));
casper.echo(casper.cli.get(3));
casper.echo(casper.cli.has("foo"));
casper.echo(casper.cli.get("foo"));
casper.cli.drop("foo");
casper.echo(casper.cli.has("foo"));
casper.echo(casper.cli.get("foo"));
casper.exit();</code></pre>
<p>Execution results:
</p>
<pre><code>$ casperjs test.js arg1 arg2 arg3 --foo=bar --plop anotherarg
true
arg1
true
anotherarg
true
bar
false
undefined</code></pre>
<p><span class="label label-info">Hint</span> What if you want to check if any arg
or option has been passed to your script? Here you go:
</p>
<pre><code class="javascript">// removing default options passed by the Python executable
casper.cli.drop("cli");
casper.cli.drop("casper-path");
if (casper.cli.args.length === 0 && Object.keys(casper.cli.options).length === 0) {
casper.echo("No arg nor option passed").exit();
}</code></pre>
<p>Last but not least, you can still use all PhantomJS standard CLI options
as you would do with any other phantomjs script:
</p>
<pre><code>$ casperjs --web-security=no --cookies-file=/tmp/mycookies.txt myscript.js</code></pre>
<p><span class="label label-info">Hint</span> To remember what the native
phantomjs available cli options are, run the <code>phantomjs --help</code> command.
</p>
<h2 id="raw">Accessing raw values of passed parameters</h2>
<p><span class="label label-success">Added in 1.0</span>
By default, the cli object will process every passed argument & cast them to the appropriate
detected type; example script:
</p>
<pre><code class="javascript">var casper = require('casper').create();
var utils = require('utils');
utils.dump(casper.cli.get('foo'));
casper.exit();</code></pre>
<p>If you run this script:
</p>
<pre><code>$ casperjs c.js --foo=01234567
1234567</code></pre>
<p>As you can see, the <code>01234567</code> value has been cast to a <em>Number</em>. Sometimes, you just want
the original string; then you can use the <code>raw</code> property of the <code>cli</code> object, which contains
the raw values of passed parameters:
</p>
<pre><code class="javascript">var casper = require('casper').create();
var utils = require('utils');
utils.dump(casper.cli.get('foo'));
utils.dump(casper.cli.raw.get('foo'));
casper.exit();</code></pre>
<p>Sample usage:
</p>
<pre><code>$ casperjs c.js --foo=01234567
1234567
"01234567"</code></pre>
</div>
</section>
</div>
<footer>
<hr>
<p>
© 2011-2012 <a href="https://nicolas.perriault.net/">Nicolas Perriault</a> — get
the source code on GitHub: <a href="http://github.com/n1k0/casperjs">n1k0/casperjs</a> —
logo by <a href="http://www.forveillejeremy.com/">Jeremy Forveille</a> -
<a href="https://plus.google.com/106641872690063476159" rel="publisher">Google+</a> -
<a href="http://flattr.com/thing/586241/CasperJS-a-navigation-scripting-and-testing-utility-for-PhantomJS" class="flattr" target="_blank"><img src="http://api.flattr.com/button/flattr-badge-large.png" alt="Flattr this" title="Flattr this" border="0"></a>
</p>
</footer>
</div>
<script type="text/javascript" async="" src="http://www.google-analytics.com/ga.js"></script><script type="text/javascript" async="" src="https://apis.google.com/js/plusone.js"></script><script type="text/javascript" async="" src="http://www.google-analytics.com/ga.js"></script><script type="text/javascript" async="" src="https://apis.google.com/js/plusone.js"></script><script type="text/javascript" async="" src="http://www.google-analytics.com/ga.js"></script><script type="text/javascript" async="" src="https://apis.google.com/js/plusone.js"></script><script type="text/javascript" async="" src="http://www.google-analytics.com/ga.js"></script><script type="text/javascript" async="" src="https://apis.google.com/js/plusone.js"></script><script type="text/javascript" async="" src="http://www.google-analytics.com/ga.js"></script><script type="text/javascript" async="" src="https://apis.google.com/js/plusone.js"></script><script type="text/javascript" async="" src="http://www.google-analytics.com/ga.js"></script><script type="text/javascript" async="" src="https://apis.google.com/js/plusone.js"></script><script type="text/javascript" async="" src="http://www.google-analytics.com/ga.js"></script><script type="text/javascript" async="" src="https://apis.google.com/js/plusone.js"></script><script src="https://apis.google.com/_/apps-static/_/js/gapi/plusone/rt=j/ver=3lVApTjvhc8.fr./sv=1/am=!vjVqvImSc8CICbuUGw/d=1/rs=AItRSTMYdzsyHCm7Rbe_R8PDrEqUW9fHWw/cb=gapi.loaded_0" async=""></script><script type="text/javascript" async="" src="http://www.google-analytics.com/ga.js"></script><script type="text/javascript" async="" src="https://apis.google.com/js/plusone.js" gapi_processed="true"></script><script type="text/javascript" src="js/jquery.min.js?1351019514358"></script>
<script type="text/javascript" src="bootstrap/js/bootstrap.min.js?1351019514358"></script>
<script type="text/javascript" src="prettify/prettify.js?1351019514358"></script>
<script type="text/javascript" src="js/application.js?1351019514358"></script>
<script type="text/javascript">
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-71239-7']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</body></html>