• We have a web server written in Node.js that we would like to launch with a specific folder. Does not sure how to access arguments in JavaScript.

Here running node like this:

[pastacode lang=”javascript” manual=”%24%20node%20server.js%20folder%0A” message=”jQuery Code” highlight=”” provider=”manual”/] [ad type=”banner”]

Where server.js is the code.

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

How to access those arguments in JavaScript?

[pastacode lang=”javascript” manual=”Javjavascript%20%20%20%20%20%20%20%20%20%20%20node.js%20%20%20%20%20%20%20%20arguments%20%20%20%20%20%20%20%20%20%20%20%20%20%20command-line-argumentsascript%0A” message=”jQuery Code” highlight=”” provider=”manual”/]

The arguments are stored in process.argv

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.

[pastacode lang=”javascript” manual=”%2F%2F%20print%20process.argv%0Aprocess.argv.forEach(function%20(val%2C%20index%2C%20array)%20%7B%0A%20%20console.log(index%20%2B%20’%3A%20’%20%2B%20val)%3B%0A%7D)%3B%0A” message=”jQuery Code” highlight=”” provider=”manual”/]

This will generate:

[pastacode lang=”javascript” manual=”%24%20node%20process-2.js%20one%20two%3Dthree%20four%0A0%3A%20node%0A1%3A%20%2FUsers%2Fmjr%2Fwork%2Fnode%2Fprocess-2.js%0A2%3A%20one%0A3%3A%20two%3Dthree%0A4%3A%20four%0A” message=”jQuery Code” highlight=”” provider=”manual”/]

  • To normalize the arguments like a regular javascript function would receive, we do this in my node.js shell scripts:
[pastacode lang=”javascript” manual=”var%20args%20%3D%20process.argv.slice(2)%3B%0A” message=”jQuery Code” highlight=”” provider=”manual”/]

Note : The first arg is usually the path to nodejs, and the second arg is the location of the script you’re executing.

  • To use the minimist library. We can use node-optimist but it has since been deprecated.

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

[pastacode lang=”javascript” manual=”var%20argv%20%3D%20require(‘minimist’)(process.argv.slice(2))%3B%0Aconsole.dir(argv)%3B%0A” message=”jQuery Code” highlight=”” provider=”manual”/] [pastacode lang=”javascript” manual=”%24%20node%20example%2Fparse.js%20-a%20beep%20-b%20boop%0A%7B%20_%3A%20%5B%5D%2C%20a%3A%20’beep’%2C%20b%3A%20’boop’%20%7D%0A” message=”jQuery Code” highlight=”” provider=”manual”/] [pastacode lang=”javascript” manual=”%24%20node%20example%2Fparse.js%20-x%203%20-y%204%20-n5%20-abc%20–beep%3Dboop%20foo%20bar%20baz%0A%7B%20_%3A%20%5B%20’foo’%2C%20’bar’%2C%20’baz’%20%5D%2C%0A%20%20x%3A%203%2C%0A%20%20y%3A%204%2C%0A%20%20n%3A%205%2C%0A%20%20a%3A%20true%2C%0A%20%20b%3A%20true%2C%0A%20%20c%3A%20true%2C%0A%20%20beep%3A%20’boop’%20%7D%0A” message=”jQuery Code” highlight=”” provider=”manual”/]

  • Vanilla javascript argument parsing:
[pastacode lang=”javascript” manual=”const%20args%20%3D%20process.argv%3B%0Aconsole.log(args)%3B%0A” message=”jQuery Code” highlight=”” provider=”manual”/]

This returns:

[pastacode lang=”javascript” manual=”%24%20node%20process-2.js%20one%20two%3Dthree%20four%0A%5B’node’%2C%20’%2FUsers%2Fdc%2Fnode%2Fserver.js’%2C%20’one’%2C%20’two%3Dthree’%2C%20’four’%5D%0A” message=”jQuery Code” highlight=”” provider=”manual”/]

  • This is very similar to how bash scripts access argument values.
[pastacode lang=”javascript” manual=”%24%20node%20yourscript.js%20banana%20monkey%0A%0Avar%20program_name%20%3D%20process.argv%5B0%5D%3B%20%2F%2Fvalue%20will%20be%20%22node%22%0Avar%20script_path%20%3D%20process.argv%5B1%5D%3B%20%2F%2Fvalue%20will%20be%20%22yourscript.js%22%0Avar%20first_value%20%3D%20process.argv%5B2%5D%3B%20%2F%2Fvalue%20will%20be%20%22banana%22%0Avar%20second_value%20%3D%20process.argv%5B3%5D%3B%20%2F%2Fvalue%20will%20be%20%22monkey%22%0A” message=”jQuery Code” highlight=”” provider=”manual”/]

  • 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:

[pastacode lang=”javascript” manual=”var%20stdio%20%3D%20require(‘stdio’)%3B%0Avar%20ops%20%3D%20stdio.getopt(%7B%0A%20%20%20%20’check’%3A%20%7Bkey%3A%20’c’%2C%20args%3A%202%2C%20description%3A%20’What%20this%20option%20means’%7D%2C%0A%20%20%20%20’map’%3A%20%7Bkey%3A%20’m’%2C%20description%3A%20’Another%20description’%7D%2C%0A%20%20%20%20’kaka’%3A%20%7Bargs%3A%201%2C%20mandatory%3A%20true%7D%2C%0A%20%20%20%20’ooo’%3A%20%7Bkey%3A%20’o’%7D%0A%7D)%3B%0A” message=”jQuery Code” highlight=”” provider=”manual”/]

[ad type=”banner”]

If you run the previous code with this command:

[pastacode lang=”javascript” manual=”node%20%3Cyour_script.js%3E%20-c%2023%2045%20–map%20-k%2023%20file1%20file2%0A” message=”jQuery Code” highlight=”” provider=”manual”/]

Then ops object will be as follows:

[pastacode lang=”javascript” manual=”%7B%20check%3A%20%5B%20’23’%2C%20’45’%20%5D%2C%0A%20%20args%3A%20%5B%20’file1’%2C%20’file2’%20%5D%2C%0A%20%20map%3A%20true%2C%0A%20%20kaka%3A%20’23’%20%7D%0A” message=”jQuery Code” highlight=”” provider=”manual”/]

So you can use it as you want. For instance:

[pastacode lang=”javascript” manual=”if%20(ops.kaka%20%26%26%20ops.check)%20%7B%0A%20%20%20%20console.log(ops.kaka%20%2B%20ops.check%5B0%5D)%3B%0A%7D%0A” message=”jQuery Code” highlight=”” provider=”manual”/]

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:

[pastacode lang=”javascript” manual=”USAGE%3A%20node%20something.js%20%5B–check%20%3CARG1%3E%20%3CARG2%3E%5D%20%5B–kaka%5D%20%5B–ooo%5D%20%5B–map%5D%0A%20%20-c%2C%20–check%20%3CARG1%3E%20%3CARG2%3E%20%20%20What%20this%20option%20means%20(mandatory)%0A%20%20-k%2C%20–kaka%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(mandatory)%0A%20%20–map%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Another%20description%0A%20%20-o%2C%20–ooo%0A” message=”jQuery Code” highlight=”” provider=”manual”/]

[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:

[pastacode lang=”javascript” manual=”npm%20install%20stdio%0A” message=”jQuery Code” highlight=”” provider=”manual”/]

  • If your script is myScript.js and you want to pass the first and last name, ‘Wiki Techy’, as arguments like below:
[pastacode lang=”javascript” manual=”node%20myScript.js%20Wiki%20Techy%0A” message=”jQuery Code” highlight=”” provider=”manual”/]

Then your script is written as follows:

[pastacode lang=”javascript” manual=”var%20firstName%20%3D%20process.argv%5B2%5D%3B%20%2F%2F%20Will%20be%20set%20to%20%E2%80%98Wiki’%0Avar%20lastName%20%3D%20process.argv%5B3%5D%3B%20%2F%2F%20Will%20be%20set%20to%20%E2%80%98Techy’%0A” message=”jQuery Code” highlight=”” provider=”manual”/]

  • Here is an another solution:
[pastacode lang=”javascript” manual=”%23!%2Fusr%2Fbin%2Fenv%20node%0Avar%20argv%20%3D%20require(‘yargs’).argv%3B%0Aconsole.log(‘(%25d%2C%25d)’%2C%20argv.x%2C%20argv.y)%3B%0Aconsole.log(argv._)%3B%0A” message=”jQuery Code” highlight=”” provider=”manual”/]

Output is here (it reads options with dashes etc, short and long, numeric etc).

[pastacode lang=”javascript” manual=”%24%20.%2Fnonopt.js%20-x%206.82%20-y%203.35%20rum%0A(6.82%2C3.35)%0A%5B%20’rum’%20%5D%20%0A%24%20.%2Fnonopt.js%20%22me%20hearties%22%20-x%200.54%20yo%20-y%201.12%20ho%0A(0.54%2C1.12)%0A%5B%20’me%20hearties’%2C%20’yo’%2C%20’ho’%20%5D%0A” message=”jQuery Code” highlight=”” provider=”manual”/]

  • we can parse all arguments and check if they exist.

file: parse-cli-arguments.js:

[pastacode lang=”javascript” manual=”module.exports%20%3D%20function(requiredArguments)%7B%0A%20%20%20%20var%20arguments%20%3D%20%7B%7D%3B%0A%0A%20%20%20%20for%20(var%20index%20%3D%200%3B%20index%20%3C%20process.argv.length%3B%20index%2B%2B)%20%7B%0A%20%20%20%20%20%20%20%20var%20re%20%3D%20new%20RegExp(‘–(%5BA-Za-z0-9_%5D%2B)%3D(%5BA%2F-Za-z0-9_%5D%2B)’)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20matches%20%3D%20re.exec(process.argv%5Bindex%5D)%3B%0A%0A%20%20%20%20%20%20%20%20if(matches%20!%3D%3D%20null)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20arguments%5Bmatches%5B1%5D%5D%20%3D%20matches%5B2%5D%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A” message=”jQuery Code” highlight=”” provider=”manual”/] [pastacode lang=”javascript” manual=”for%20(var%20index%20%3D%200%3B%20index%20%3C%20requiredArguments.length%3B%20index%2B%2B)%20%7B%0A%20%20%20%20%20%20%20%20if%20(arguments%5BrequiredArguments%5Bindex%5D%5D%20%3D%3D%3D%20undefined)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20throw(requiredArguments%5Bindex%5D%20%2B%20’%20not%20defined.%20Please%20add%20the%20argument%20with%20–‘%20%2B%20requiredArguments%5Bindex%5D)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%0A%20%20%20%20return%20arguments%3B%0A%7D” message=”jQuery Code” highlight=”” provider=”manual”/]

Than just do:

[pastacode lang=”javascript” manual=”var%20arguments%20%3D%20require(‘.%2Fparse-cli-arguments’)(%5B’foo’%2C%20’bar’%2C%20’xpto’%5D)%3B%0A” message=”jQuery Code” highlight=”” provider=”manual”/]

  • npm install ps-grab

If we want to run something like this :

[pastacode lang=”javascript” manual=”node%20greeting.js%20–user%20Wikitechy%20–website%20http%3A%2F%2Fwww.wikitechy.com%0A” message=”jQuery Code” highlight=”” provider=”manual”/] [pastacode lang=”javascript” manual=”var%20grab%3Drequire(‘ps-grab’)%3B%0Agrab(‘–username’)%20%2F%2F%20return%20%E2%80%98Wikitechy’%0Agrab(‘–action’)%20%2F%2F%20return%20’http%3A%2F%2Fwww.wikitechy.com’%0A” message=”jQuery Code” highlight=”” provider=”manual”/]

Or something like :

[pastacode lang=”javascript” manual=”node%20vbox.js%20-OS%20redhat%20-VM%20template-12332%20%3B%0A” message=”jQuery Code” highlight=”” provider=”manual”/] [pastacode lang=”javascript” manual=”var%20grab%3Drequire(‘ps-grab’)%3B%0Agrab(‘-OS’)%20%2F%2F%20return%20’redhat’%0Agrab(‘-VM’)%20%2F%2F%20return%20’template-12332%0A” message=”jQuery Code” highlight=”” provider=”manual”/]

Categorized in: