How to pass a variable as an argument to a CasperJS script through the command line?

前端 未结 5 825
挽巷
挽巷 2020-12-10 11:38

I\'m using PhantomJs, CasperJs, and Js in a js file ran through the cmd.

Imagine we had two files(test1.js, and test2.js). Both files have a url/site variable that d

5条回答
  •  甜味超标
    2020-12-10 12:00

    Found the answers too hard to understand at a glance. You can pass arg or option parameters.

    Example: Passing Options *(Using = is required)

    $ casperjs myscript.js --username=user --password=123
    
    var casper = require('casper').create();
    var username = casper.cli.options.username;
    var password = casper.cli.options.password;
    console.log(username + ':' + password); // user:123
    casper.exit();
    

    Example: Passing Args

    $ casperjs myscript.js user 123
    
    var casper = require('casper').create();
    var username = casper.cli.args[0];
    var password = casper.cli.args[1];
    console.log(username + ':' + password); // user:123
    casper.exit();
    

提交回复
热议问题