Passing args to a JS command line utility (Node or Narwhal)

399 Views Asked by At

I want to use NodeJS or Narwhal to create a JS utility which takes an argument, like so:

$ node myscript.js http://someurl.com/for/somefile.js

or

$ js myscript.js http://someurl.com/for/somefile.js

but I'm wondering how I can get that argument within my script, or if that is even possible atm?

Thanks.

4

There are 4 best solutions below

0
Jörg W Mittag On BEST ANSWER

On Node.JS, that information is available in process.argv.

0
meder omuraliev On

process.arg[2] should be the reference. Check for process.argv.length to see if it's 3.

Docs

0
generalhenry On

You can use optimist, a module for node.js that makes processing args a bit easier.

0
sgmonda On

This things now are very easy to do, using stdio module for NodeJS.