[Translate]CASPERJS usage Instructions-use the command line

Source: Internet
Author: User

Using the command line

Casperjs uses the built-in PHANTOMJS command-line parser, which passes the naming options for parameter locations in the CLI module

But don't worry about not being able to manipulate the API of the CLI module, a Casper instance already contains the CLI attribute, allowing you to easily use his parameters

Let's take a look at this simple Casper script:

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 ();

Attention:

Note that both the Casper-path and CLI parameters are executed by Python when they execute Casperjs scripts through Casperjs

Execution Result:

$ casperjs test.js arg1 arg2 arg3--foo=bar--plop anotherargcasper CLI passed args: [    "Arg1",    "arg2",    "Arg3",    "Anotherarg"] Casper CLI passed options: {    "Casper-path": "/users/niko/sites/casperjs",    "CLI": True,    "foo": "Bar",    "Plop": true}
Get, check, delete parameters:
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 ();
Execution Result:
$ casperjs test.js arg1 arg2 arg3--foo=bar--plop anotherargtruearg1trueanotherargtruebarfalseundefined
Useful advice:
If you want to check for those parameters that are used in your script, look at the following:
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 ();
Casper own parameters:
1.1 Version added:
The CASPERJS command has 3 common parameters:
--direct: Output log messages to the console
--log-level=[debug|info|warning|error] Set Log level
--ENGINE=[PHANTOMJS|SLIMERJS] Select the browser engine you want to use, CASPERJS support PHANTOMJS supported Webkit,slimerjs run WebKit kernel by default
Attention:
Version 1.1 is obsolete
The--direct parameter has been renamed to--verbose. --direct Although still available, his abandonment was considered.
Example:
$ casperjs--verbose--log-level=debug myscript.js
Last but not least, you can also use all the PHANTOMJS and CLI command-line arguments just as you would run other PHANTOMJS scripts
$ casperjs--web-security=no--cookies-file=/tmp/mycookies.txt myscript.js
Useful advice:
Want to know that there are those PHANTOMJS command line arguments available, run the PHANTOMJS--help command, SLIMERJS supported command-line parameters almost identical to Phantomjs
Original parameter value:
1.0 version Added
By default, the CLI object will convert all parameters to the type that he considers most appropriate, such as this example:
var casper = require (' Casper '). Create (); var utils = require (' utils '); Utils.dump (Casper.cli.get (' foo ')); Casper.exit () ;
Run this script:
$ casperjs c.js--foo=012345671234567
As you can see, 01234567 is converted into numbers.
Sometimes you just want the original string, you can use the raw property of the CLI object, and he contains the original value of the parameter
var casper = require (' Casper '). Create (); var utils = require (' utils '); Utils.dump (Casper.cli.get (' foo ')); Utils.dump ( Casper.cli.raw.get (' foo ')); Casper.exit ();
Examples of Use:
$ casperjs c.js--foo=012345671234567 "01234567"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.