# Using the command line

CasperJS ships with a built-in command line parser on top of PhantomJS’ parser, located in the `cli` module. It exposes passed arguments as positional ones and named options

A `Casper` instance always contains a ready-to-use `cli` property for easy access to these parameters, so you don’t have to worry about manipulating the `cli` module parsing API.

Let’s consider 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();

Note

Please note the two `casper-path` and `cli` options; these are passed to the casper script through the `casperjs` Python executable.

Execution results:

    $ 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
    }

Getting, checking or dropping 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 results:

    $ casperjs test.js arg1 arg2 arg3 --foo=bar --plop anotherarg
    true
    arg1
    true
    anotherarg
    true
    bar
    false
    undefined

Hint

What if you want to check if any arg or option has been passed to your script? Here you go:

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

## casperjs native options

New in version 1.1.

The casperjs command has three available options:

- `--direct`: to print out log messages to the console
- `--log-level=[debug|info|warning|error]` to set the logging level
- `--engine=[phantomjs|slimerjs]` to select the browser engine you want to use. CasperJS supports PhantomJS (default) that runs Webkit, and SlimerJS that runs Gecko.

Warning

Deprecated since version 1.1.

The `--direct` option has been renamed to `--verbose`. Although `--direct` will still work, it is now considered deprecated.

Example:

    $ casperjs --verbose --log-level=debug myscript.js

Last but not least, you can still use all PhantomJS standard CLI options as you would do with any other PhantomJS script:

    $ casperjs --web-security=no --cookies-file=/tmp/mycookies.txt myscript.js

Hint

To remember what the native PhantomJS cli options are available, run the `phantomjs --help` command. SlimerJS supports almost same options as PhantomJS.

## Raw parameter values

New in version 1.0.

By default, the cli object will process every passed argument & cast them to the appropriate detected type; example script:

    var casper = require('casper').create();
    var utils = require('utils');

    utils.dump(casper.cli.get('foo'));

    casper.exit();

If you run this script:

    $ casperjs c.js --foo=01234567
    1234567

As you can see, the `01234567` value has been cast to a Number.

If you want the original string, use the `raw` property of the `cli` object, which contains the raw values of the passed parameters:

    var casper = require('casper').create();
    var utils = require('utils');

    utils.dump(casper.cli.get('foo'));
    utils.dump(casper.cli.raw.get('foo'));

    casper.exit();

Sample usage:

    $ casperjs c.js --foo=01234567
    1234567
    "01234567"