Book Image

PhantomJS Cookbook

By : Rob Friesel
Book Image

PhantomJS Cookbook

By: Rob Friesel

Overview of this book

Table of Contents (15 chapters)
PhantomJS Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Inspecting command-line arguments


In this recipe, we introduce the system module and discuss how to inspect arguments that are passed to the PhantomJS runtime environment from the command line. The system module is the bridge between PhantomJS, its host operating system, and the process it runs in.

Getting ready

To run this recipe, we will need a script that accepts arguments from the command line.

The script in this recipe is available in the downloadable code repository as recipe06.js under chapter02. If we run the provided example script, we must change to the root directory for the book's sample code.

How to do it…

Consider the following script:

var system = require('system'),
    args   = system.args;

console.log('script name is: ' + args[0]);

if (args.length > 1) {
  var restArgs = args.slice(1);
  restArgs.forEach(function(arg, i) {
    console.log('[' + (i + 1) + '] ' + arg);
  });
} else {
  console.log('No arguments were passed.');
}

phantom.exit();

Given the preceding script, enter...