process.argv is an array containing the command line arguments. The first element will be ‘node’, the second element will be the name of the JavaScript file. The next elements will be any additional command line arguments.
$ node process-2.js one two=three four ['node', '/Users/dc/node/server.js', 'one', 'two=three', 'four']
This is very similar to how bash scripts access argument values.
jQuery Code
$ 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"
Stdio Library
The easiest way to parse command-line arguments in NodeJS is using the stdio module.
Inspired by UNIX getopt utility, it is as trivial as follows:
if (ops.kaka && ops.check) { console.log(ops.kaka + ops.check[0]); }
Grouped options are also supported, so you can write -om instead of -o -m.
Furthermore, stdio can generate a help/usage output automatically.
If we call ops.printHelp() we will get the following:
jQuery Code
USAGE: node something.js [--check <ARG1> <ARG2>] [--kaka] [--ooo] [--map] -c, --check <ARG1> <ARG2> What this option means (mandatory) -k, --kaka (mandatory) --map Another description -o, --ooo
[ad type=”banner”]
The previous message is shown also if a mandatory option is not given (preceded by the error message) or if it is mispecified (for instance, if you specify a single arg for an option and it needs 2).
You can install stdio module using NPM:
jQuery Code
npm install stdio
If your script is myScript.js and you want to pass the first and last name, ‘Wiki Techy’, as arguments like below:
jQuery Code
node myScript.js Wiki Techy
Then your script is written as follows:
jQuery Code
var firstName = process.argv[2]; // Will be set to ‘Wiki' var lastName = process.argv[3]; // Will be set to ‘Techy'
Here is an another solution:
jQuery Code
#!/usr/bin/env node var argv = require('yargs').argv; console.log('(%d,%d)', argv.x, argv.y); console.log(argv._);
Output is here (it reads options with dashes etc, short and long, numeric etc).
jQuery Code
$ ./nonopt.js -x 6.82 -y 3.35 rum (6.82,3.35) [ 'rum' ] $ ./nonopt.js "me hearties" -x 0.54 yo -y 1.12 ho (0.54,1.12) [ 'me hearties', 'yo', 'ho' ]
we can parse all arguments and check if they exist.
file: parse-cli-arguments.js:
jQuery Code
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]);
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:
jQuery Code
var arguments = require('./parse-cli-arguments')(['foo', 'bar', 'xpto']);
Wikitechy Founder, Author, International Speaker, and Job Consultant. My role as the CEO of Wikitechy, I help businesses build their next generation digital platforms and help with their product innovation and growth strategy. I'm a frequent speaker at tech conferences and events.