{"id":2381,"date":"2017-03-27T17:54:16","date_gmt":"2017-03-27T12:24:16","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=2381"},"modified":"2017-03-28T15:09:20","modified_gmt":"2017-03-28T09:39:20","slug":"pass-command-line-arguments","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/pass-command-line-arguments\/","title":{"rendered":"[ Solved &#8211; 10 Answers ] JAVASCRIPT &#8211; How to pass command line arguments"},"content":{"rendered":"<p><label class=\"label label-Warning\">PROBLEM<\/label><\/p>\n<ul>\n<li>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.<\/li>\n<\/ul>\n<h4 id=\"here-running-node-like-this\"><span style=\"color: #800000;\">Here running node like this:<\/span><\/h4>\n[pastacode lang=\u201djavascript\u201d manual=\u201d%24%20node%20server.js%20folder%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p>Where server.js is the code.<\/p>\n<p>$ node -h<br \/>\n<span style=\"color: #000000;\">Usage:<\/span> node [options] script.js [arguments]\n<p>How to access those arguments in JavaScript?<\/p>\n[pastacode lang=\u201djavascript\u201d manual=\u201dJavjavascript%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\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 1:<\/label><\/p>\n<p>The arguments are stored in process.argv<\/p>\n<p>process.argv is an array containing the command line arguments. The first element will be \u2018node\u2019, the second element will be the name of the JavaScript file. The next elements will be any additional command line arguments.<\/p>\n[pastacode lang=\u201djavascript\u201d manual=\u201d%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\u2019%3A%20\u2019%20%2B%20val)%3B%0A%7D)%3B%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h4 id=\"this-will-generate\"><span style=\"color: #800080;\">This will generate:<\/span><\/h4>\n[pastacode lang=\u201djavascript\u201d manual=\u201d%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\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 2:<\/label><\/p>\n<ul>\n<li><span style=\"color: #000000;\">To normalize the arguments like a regular javascript function would receive, we do this in my node.js shell scripts:<\/span><\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201dvar%20args%20%3D%20process.argv.slice(2)%3B%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><span style=\"color: #000000;\">Note : <\/span>The first arg is usually the path to nodejs, and the second arg is the location of the script you\u2019re executing.<\/p>\n<p><label class=\"label label-info\">SOLUTION 3:<\/label><\/p>\n<ul>\n<li><span style=\"color: #000000;\">To use the\u00a0minimist\u00a0library. We can use node-optimist but it has since been deprecated.<\/span><\/li>\n<\/ul>\n<p><strong>Here is an example of how to use it taken straight from the minimist documentation:<\/strong><\/p>\n[pastacode lang=\u201djavascript\u201d manual=\u201dvar%20argv%20%3D%20require(\u2018minimist\u2019)(process.argv.slice(2))%3B%0Aconsole.dir(argv)%3B%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[pastacode lang=\u201djavascript\u201d manual=\u201d%24%20node%20example%2Fparse.js%20-a%20beep%20-b%20boop%0A%7B%20_%3A%20%5B%5D%2C%20a%3A%20\u2019beep\u2019%2C%20b%3A%20\u2019boop\u2019%20%7D%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[pastacode lang=\u201djavascript\u201d manual=\u201d%24%20node%20example%2Fparse.js%20-x%203%20-y%204%20-n5%20-abc%20\u2013beep%3Dboop%20foo%20bar%20baz%0A%7B%20_%3A%20%5B%20\u2019foo\u2019%2C%20\u2019bar\u2019%2C%20\u2019baz\u2019%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\u2019boop\u2019%20%7D%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 4:<\/label><\/p>\n<ul>\n<li><span style=\"color: #000000;\">Vanilla javascript argument parsing:<\/span><\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201dconst%20args%20%3D%20process.argv%3B%0Aconsole.log(args)%3B%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h4 id=\"this-returns\"><span style=\"color: #000000;\">This returns:<\/span><\/h4>\n[pastacode lang=\u201djavascript\u201d manual=\u201d%24%20node%20process-2.js%20one%20two%3Dthree%20four%0A%5B\u2019node\u2019%2C%20\u2019%2FUsers%2Fdc%2Fnode%2Fserver.js\u2019%2C%20\u2019one\u2019%2C%20\u2019two%3Dthree\u2019%2C%20\u2019four\u2019%5D%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 5:<\/label><\/p>\n<ul>\n<li><span style=\"color: #000000;\">This is very similar to how bash scripts access argument values.<\/span><\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201d%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\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 6:<\/label><\/p>\n<ul>\n<li><span style=\"color: #000000;\">Stdio Library<\/span><\/li>\n<li><span style=\"color: #000000;\">The easiest way to parse command-line arguments in NodeJS is using the stdio module.<\/span><\/li>\n<\/ul>\n<h4 id=\"inspired-by-unix-getopt-utility-it-is-as-trivial-as-follows\"><span style=\"color: #000000;\">Inspired by UNIX getopt utility, it is as trivial as follows:<\/span><\/h4>\n[pastacode lang=\u201djavascript\u201d manual=\u201dvar%20stdio%20%3D%20require(\u2018stdio\u2019)%3B%0Avar%20ops%20%3D%20stdio.getopt(%7B%0A%20%20%20%20\u2019check\u2019%3A%20%7Bkey%3A%20\u2019c\u2019%2C%20args%3A%202%2C%20description%3A%20\u2019What%20this%20option%20means\u2019%7D%2C%0A%20%20%20%20\u2019map\u2019%3A%20%7Bkey%3A%20\u2019m\u2019%2C%20description%3A%20\u2019Another%20description\u2019%7D%2C%0A%20%20%20%20\u2019kaka\u2019%3A%20%7Bargs%3A%201%2C%20mandatory%3A%20true%7D%2C%0A%20%20%20%20\u2019ooo\u2019%3A%20%7Bkey%3A%20\u2019o\u2019%7D%0A%7D)%3B%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p style=\"top: 130px;\">[ad type=\u201dbanner\u201d]\n<h4 id=\"if-you-run-the-previous-code-with-this-command\"><span style=\"color: #000000;\">If you run the previous code with this command:<\/span><\/h4>\n[pastacode lang=\u201djavascript\u201d manual=\u201dnode%20%3Cyour_script.js%3E%20-c%2023%2045%20\u2013map%20-k%2023%20file1%20file2%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h4 id=\"then-ops-object-will-be-as-follows\"><span style=\"color: #000000;\">Then ops object will be as follows:<\/span><\/h4>\n[pastacode lang=\u201djavascript\u201d manual=\u201d%7B%20check%3A%20%5B%20\u201923\u2019%2C%20\u201945\u2019%20%5D%2C%0A%20%20args%3A%20%5B%20\u2019file1\u2019%2C%20\u2019file2\u2019%20%5D%2C%0A%20%20map%3A%20true%2C%0A%20%20kaka%3A%20\u201923\u2019%20%7D%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h4 id=\"so-you-can-use-it-as-you-want-for-instance\"><span style=\"color: #000000;\">So you can use it as you want. For instance:<\/span><\/h4>\n[pastacode lang=\u201djavascript\u201d manual=\u201dif%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\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Grouped options are also supported, so you can write -om instead of -o -m.<\/p>\n<p>Furthermore, stdio can generate a help\/usage output automatically.<\/p>\n<h4 id=\"if-we-call-ops-printhelp-we-will-get-the-following\"><span style=\"color: #000000;\">If we call ops.printHelp() we will get the following:<\/span><\/h4>\n[pastacode lang=\u201djavascript\u201d manual=\u201dUSAGE%3A%20node%20something.js%20%5B\u2013check%20%3CARG1%3E%20%3CARG2%3E%5D%20%5B\u2013kaka%5D%20%5B\u2013ooo%5D%20%5B\u2013map%5D%0A%20%20-c%2C%20\u2013check%20%3CARG1%3E%20%3CARG2%3E%20%20%20What%20this%20option%20means%20(mandatory)%0A%20%20-k%2C%20\u2013kaka%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(mandatory)%0A%20%20\u2013map%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\u2013ooo%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p style=\"top: 130px;\">[ad type=\u201dbanner\u201d]\n<p>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).<\/p>\n<h4 id=\"you-can-install-stdio-module-using-npm\"><span style=\"color: #000000;\">You can install stdio module using NPM:<\/span><\/h4>\n[pastacode lang=\u201djavascript\u201d manual=\u201dnpm%20install%20stdio%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 7:<\/label><\/p>\n<ul>\n<li>\n<h5 id=\"if-your-script-is-myscript-js-and-you-want-to-pass-the-first-and-last-name-wiki-techy-as-arguments-like-below\"><span style=\"color: #000000;\"><strong>If your script is myScript.js and you want to pass the first and last name, \u2018Wiki Techy\u2019, as arguments like below:<\/strong><\/span><\/h5>\n<\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201dnode%20myScript.js%20Wiki%20Techy%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h4 id=\"then-your-script-is-written-as-follows\"><span style=\"color: #000000;\">Then your script is written as follows:<\/span><\/h4>\n[pastacode lang=\u201djavascript\u201d manual=\u201dvar%20firstName%20%3D%20process.argv%5B2%5D%3B%20%2F%2F%20Will%20be%20set%20to%20%E2%80%98Wiki\u2019%0Avar%20lastName%20%3D%20process.argv%5B3%5D%3B%20%2F%2F%20Will%20be%20set%20to%20%E2%80%98Techy\u2019%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 8:<\/label><\/p>\n<ul>\n<li><strong><span style=\"color: #000000;\">Here is an another solution:<\/span><\/strong><\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201d%23!%2Fusr%2Fbin%2Fenv%20node%0Avar%20argv%20%3D%20require(\u2018yargs\u2019).argv%3B%0Aconsole.log(\u2018(%25d%2C%25d)\u2019%2C%20argv.x%2C%20argv.y)%3B%0Aconsole.log(argv._)%3B%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Output is here (it reads options with dashes etc, short and long, numeric etc).<\/p>\n[pastacode lang=\u201djavascript\u201d manual=\u201d%24%20.%2Fnonopt.js%20-x%206.82%20-y%203.35%20rum%0A(6.82%2C3.35)%0A%5B%20\u2019rum\u2019%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\u2019me%20hearties\u2019%2C%20\u2019yo\u2019%2C%20\u2019ho\u2019%20%5D%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 9:<\/label><\/p>\n<ul>\n<li>\n<h5 id=\"we-can-parse-all-arguments-and-check-if-they-exist\"><span style=\"color: #000000;\">we can parse all arguments and check if they exist.<\/span><\/h5>\n<\/li>\n<\/ul>\n<h4 id=\"file-parse-cli-arguments-js\"><span style=\"color: #000000;\">file: parse-cli-arguments.js:<\/span><\/h4>\n[pastacode lang=\u201djavascript\u201d manual=\u201dmodule.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(\u2018\u2013(%5BA-Za-z0-9_%5D%2B)%3D(%5BA%2F-Za-z0-9_%5D%2B)\u2019)%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\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[pastacode lang=\u201djavascript\u201d manual=\u201dfor%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\u2019%20not%20defined.%20Please%20add%20the%20argument%20with%20\u2013\u2018%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\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h4 id=\"than-just-do\"><span style=\"color: #000000;\">Than just do:<\/span><\/h4>\n[pastacode lang=\u201djavascript\u201d manual=\u201dvar%20arguments%20%3D%20require(\u2018.%2Fparse-cli-arguments\u2019)(%5B\u2019foo\u2019%2C%20\u2019bar\u2019%2C%20\u2019xpto\u2019%5D)%3B%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 10:<\/label><\/p>\n<ul>\n<li><span style=\"color: #000000;\">npm install ps-grab<\/span><\/li>\n<\/ul>\n<h4 id=\"if-we-want-to-run-something-like-this\"><span style=\"color: #000000;\">If we want to run something like this :<\/span><\/h4>\n[pastacode lang=\u201djavascript\u201d manual=\u201dnode%20greeting.js%20\u2013user%20Wikitechy%20\u2013website%20http%3A%2F%2Fwww.wikitechy.com%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[pastacode lang=\u201djavascript\u201d manual=\u201dvar%20grab%3Drequire(\u2018ps-grab\u2019)%3B%0Agrab(\u2018\u2013username\u2019)%20%2F%2F%20return%20%E2%80%98Wikitechy\u2019%0Agrab(\u2018\u2013action\u2019)%20%2F%2F%20return%20\u2019http%3A%2F%2Fwww.wikitechy.com\u2019%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h4 id=\"or-something-like\"><span style=\"color: #000000;\">Or something like :<\/span><\/h4>\n[pastacode lang=\u201djavascript\u201d manual=\u201dnode%20vbox.js%20-OS%20redhat%20-VM%20template-12332%20%3B%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[pastacode lang=\u201djavascript\u201d manual=\u201dvar%20grab%3Drequire(\u2018ps-grab\u2019)%3B%0Agrab(\u2018-OS\u2019)%20%2F%2F%20return%20\u2019redhat\u2019%0Agrab(\u2018-VM\u2019)%20%2F%2F%20return%20\u2019template-12332%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n","protected":false},"excerpt":{"rendered":"<p>PROBLEM 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=\u201djavascript\u201d manual=\u201d%24%20node%20server.js%20folder%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/] [ad type=\u201dbanner\u201d] Where server.js is the code. $ node -h Usage: node [options] script.js [arguments] [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[275],"tags":[5599,5612,5600,5609,5610,5611,5608,5607,5604,5606,5603,5605,5601,5598,5602],"class_list":["post-2381","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-c-command-line-arguments","tag-c-command-line-arguments-integer","tag-command-line-arguments-in-c","tag-command-line-arguments-in-c-example-with-output","tag-command-line-arguments-in-c-pdf","tag-command-line-arguments-in-python","tag-command-line-arguments-java","tag-command-line-parameters-tutorial-c","tag-command-line-arguments","tag-command-line-arguments-the-java-tutorials-essential-classes","tag-handling-command-line-arguments","tag-how-can-i-pass-a-command-line-argument-into-a-shell-script","tag-javascript-how-do-i-pass-command-line-arguments","tag-related-queries-java-command-line-arguments","tag-windows-how-to-pass-command-line-parameters-to-a-batch-file"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/2381","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=2381"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/2381\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=2381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=2381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=2381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}