I am running javascripts from shell using the spidermonkey javascript engine and have most things working as needed.
I would like to be able to pass variables to the javascripts as arguments from the command line but have not been able to find a way to do this, or even if it is possible.
As an example, if I have a javascript file (myJs.js) that parses text from some data stored in a file (data.dat), I currently call the javascript with normal syntax:
js -f myJs.js
and the javascript itself has the commands to get the data from whatever file I want to process, like this:
var fileData = read("data.dat");
This works fine but if I want to reuse the javascript for different data files, I have to edit the file name in the javascript each time I want to run it for new data.
My question is, is there a way to pass the filename (or any other variable) to the javascript from the command line and retrieve it as an argument from within the javascript, something like:
js -f myJs.js -- "othertData.dat"
where the javascript would be able to access the value "otherData.dat":
var fileData = commandLine.argument[0];
// (I know commandLine is not an object and argument not a property, just illustrating what I want to do!);
It may simply not be possible to do this. I can batch files through the javascript by having the script read another file containing a list of file names but passing the file name directly from the command line would be much easier.
I've searched extensively for information on this and there is some confusion as you can pass the arguments of spidermonkey itself to javascripts but I want to pass variables directly to the script in a way analogous to passing variable arguments to a bash script:
: myBashScript <myData1> <myData2>
where the variable arguments are retrieved into numbered argument arrays:
firstArgument=$1
If this cannot be done, an authoritative statement to that effect might be useful as I've found similar questions discussed across the web without clear answer (most discuss passing spidermonkey's own arguments rather than variables).
Thanks