0

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

1
  • Please don't mark this as a duplicate of link as, although I am sure he wants to do the same thing, the question appears to have been confused with the spidermonkey arguments I refer to here and does not have a satisfactory answer so I hope this question will clarify what's needed and solve the problem. Thanks. Commented Jun 7, 2013 at 10:19

4 Answers 4

2

According to this: Shell global objects page from MDN

it may also be scriptArgs.

Sign up to request clarification or add additional context in comments.

Comments

1

With top-of-tree SpiderMonkey:

$ ./js --help
[snip]
Version: JavaScript-C24.0a1
[snip]
$ cat myscript.js 
print(uneval(arguments));
$ ./js -f myscript.js -- foo bar baz
["foo", "bar", "baz"]

I think that does what you want, yes?

2 Comments

It would be great if that worked, but alas it doesn't. Mac's inbuilt jsc supports argument passing following -- but spidermonkey doesn't work this way
Thanks. I've just noticed my version is JavaScript-C 1.8.5 2011-03-31 so that may make a difference, but your lines throw an error. I can now read a string of passed parameters if I use a single - delimiter (although ["a", "b", "c"] unevals to : ["[a,b,c]"] and if I try to eval it to an array, it has a single member. At least that's some progress. I'll continue experimenting.
0

I've finally given up and use a workaround... I've written a short (reuseable) shell script that acts as a bridge between terminal and spidermonkey js. From terminal I call the shell script with a list of arguments that I want fed to the javascript (the first one being the name of the javascript file to be run). so instead of (the equivalent of):

js -f myscript - arg1 arg2 arg3

(which I am still unable to achieve)

I use

javascript myscript arg1 arg2 arg3

(javascript being the name of my bridge script). That's it, it works fine.

The bridge script simply reads the arguments, writes them to a temporary file formatted as javascript array assignments, apends the .js script to the temp file and executes it.

# create jscode file a write js to declare an array to hold arguments
echo "var args = new Array();" > jscode

# write assignments to each array index to jscode file
# counter initialised to -1 to allow first argument (filename) to be skipped
i=-1
for TOKEN in $*
do
if [ if $i != -1 ]
  then
  echo "args[$i] = \"$TOKEN\";" >> jscode
fi
i=$((i+1))
done

# append script file (first arg is file name) to jscode file
cat $1 >> jscode
#run jscode using spidermonkey
jsc -f jscode

so, if I want to send arguments "hello" "world" to a javascript called greeting.js, from terminal I use:

javascript greeting.js hello world

the temp jscode file created now begins:

var args = new Array();
arg[0] = "hello";
arg[1] = "world";
// contents of greeting.js appended here

Comments

0

https://developer.mozilla.org/en-US/docs/SpiderMonkey/Shell_global_objects specifies that `arguments' at the top-level should give the arguments passed to the shell, ignoring any shell-related options.

I find this is broken in some versions on the Mozilla 24 branch, though, and filed a bug.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.