Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 694 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I pass command line arguments to a Node.js program?

#1
I have a web server written in [Node.js][1] and I would like to launch with a specific folder. I'm not sure how to access arguments in JavaScript. I'm running node like this:

$ node server.js folder

here `server.js` is my server code. Node.js help says this is possible:

$ node -h
Usage: node [options] script.js [arguments]

How would I access those arguments in JavaScript? Somehow I was not able to find this information on the web.

[1]:

[To see links please register here]

Reply

#2
# [Commander.js](

[To see links please register here]

)

Works great for defining your options, actions, and arguments. It also generates the help pages for you.

# [Promptly](

[To see links please register here]

)

Works great for getting input from the user, if you like the callback approach.

# [Co-Prompt](

[To see links please register here]

)

Works great for getting input from the user, if you like the generator approach.
Reply

#3
You can parse all arguments and check if they exist.

file: parse-cli-arguments.js:

module.exports = function(requiredArguments){
var arguments = {};

for (var index = 0; index < process.argv.length; index++) {
var re = new RegExp('--([A-Za-z0-9_]+)=([A/-Za-z0-9_]+)'),
matches = re.exec(process.argv[index]);

if(matches !== null) {
arguments[matches[1]] = matches[2];
}
}

for (var index = 0; index < requiredArguments.length; index++) {
if (arguments[requiredArguments[index]] === undefined) {
throw(requiredArguments[index] + ' not defined. Please add the argument with --' + requiredArguments[index]);
}
}

return arguments;
}


Than just do:

var arguments = require('./parse-cli-arguments')(['foo', 'bar', 'xpto']);
Reply

#4
Several great answers here, but it all seems very complex. This is very similar to how bash scripts access argument values and it's already provided standard with node.js as MooGoo pointed out.
(Just to make it understandable to somebody that's new to node.js)

Example:

$ node yourscript.js banana monkey

var program_name = process.argv[0]; //value will be "node"
var script_path = process.argv[1]; //value will be "yourscript.js"
var first_value = process.argv[2]; //value will be "banana"
var second_value = process.argv[3]; //value will be "monkey"
Reply

#5
The up-to-date *right* answer for this it to use the [minimist](

[To see links please register here]

) library. We used to use [node-optimist](

[To see links please register here]

) but it has since been deprecated.

Here is an example of how to use it taken straight from the minimist documentation:

var argv = require('minimist')(process.argv.slice(2));
console.dir(argv);

-

$ node example/parse.js -a beep -b boop
{ _: [], a: 'beep', b: 'boop' }

-

$ node example/parse.js -x 3 -y 4 -n5 -abc --beep=boop foo bar baz
{ _: [ 'foo', 'bar', 'baz' ],
x: 3,
y: 4,
n: 5,
a: true,
b: true,
c: true,
beep: 'boop' }
Reply

#6
To normalize the arguments like a regular javascript function would receive, I do this in my node.js shell scripts:

var args = process.argv.slice(2);

Note that the first arg is usually the path to nodejs, and the second arg is the location of the script you're executing.
Reply

#7
Passing,parsing arguments is an easy process. Node provides you with the process.argv property, which is an array of strings, which are the arguments that were used when Node was invoked.
The first entry of the array is the Node executable, and the second entry is the name of your script.

If you run script with below atguments

$ node args.js arg1 arg2

File : args.js

console.log(process.argv)

You will get array like

['node','args.js','arg1','arg2']
Reply

#8
npm install ps-grab

##If you want to run something like this :


node greeting.js --user Abdennour --website

[To see links please register here]


--

var grab=require('ps-grab');
grab('--username') // return 'Abdennour'
grab('--action') // return 'http://abdennoor.com'

____
Or something like :

node vbox.js -OS redhat -VM template-12332 ;

--


var grab=require('ps-grab');
grab('-OS') // return 'redhat'
grab('-VM') // return 'template-12332'
Reply

#9
You can reach command line arguments using `system.args`. And i use the solution below to parse arguments into an object, so i can get which one i want by name.

var system = require('system');

var args = {};
system.args.map(function(x){return x.split("=")})
.map(function(y){args[y[0]]=y[1]});

now you don't need to know the index of the argument. use it like `args.whatever`

> Note: you should use named arguments like `file.js x=1 y=2` to use
> this solution.

Reply

#10
If your script is called myScript.js and you want to pass the first and last name, 'Sean Worthington', as arguments like below:

node myScript.js Sean Worthington

Then within your script you write:

var firstName = process.argv[2]; // Will be set to 'Sean'
var lastName = process.argv[3]; // Will be set to 'Worthington'
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through