{"id":10,"date":"2014-07-26T18:30:00","date_gmt":"2014-07-26T18:30:00","guid":{"rendered":"\/blog\/blog\/command_line_apps_in_node.js.html"},"modified":"2014-07-26T18:30:00","modified_gmt":"2014-07-26T18:30:00","slug":"building-command-line-apps-in-node-js","status":"publish","type":"post","link":"https:\/\/mohammedlakkadshaw.com\/blog\/building-command-line-apps-in-node-js.html","title":{"rendered":"Building command line apps in Node.js"},"content":{"rendered":"<p>Node.js is becoming very popular platform for creating command line utilities, so I am writing this blog post to help you get started.<\/p>\n<h2 id=\"creating-an-executable-script\">Creating an executable script<\/h2>\n<p>To create an executable script you will have to add shebang at the top of the script. E.g:<\/p>\n<figure class=\"highlight\">\n<pre><code class=\"language-javascript\" data-lang=\"javascript\"><span class=\"cp\">#!\/usr\/bin\/env node\n<\/span>\n<span class=\"nx\">console<\/span><span class=\"p\">.<\/span><span class=\"nx\">log<\/span><span class=\"p\">(<\/span><span class=\"s2\">\"First program\"<\/span><span class=\"p\">);<\/span>\n\t\t<\/code><\/pre>\n<\/figure>\n<p>Assuming you are on a UNIX like system, you will also have to change the permission of the script and make it executable.<\/p>\n<div class=\"highlighter-rouge\">\n<pre class=\"highlight\"><code>chmod +x script.js\n<\/code><\/pre>\n<\/div>\n<p>and now you can execute the script:<\/p>\n<div class=\"highlighter-rouge\">\n<pre class=\"highlight\"><code>.\/script\nFirst Program\n<\/code><\/pre>\n<\/div>\n<h2 id=\"accepting-arguments\">Accepting arguments<\/h2>\n<p>You would also want use command line tool to accept arguments, which you can do using <strong>process.argv<\/strong>. This is an array which will contain all the supplied command line arguments.<\/p>\n<p><code class=\"highlighter-rouge\">script.js<\/code><\/p>\n<figure class=\"highlight\">\n<pre><code class=\"language-javascript\" data-lang=\"javascript\">\t<span class=\"nx\">process<\/span><span class=\"p\">.<\/span><span class=\"nx\">argv<\/span><span class=\"p\">.<\/span><span class=\"nx\">forEach<\/span><span class=\"p\">(<\/span><span class=\"kd\">function<\/span><span class=\"p\">(<\/span><span class=\"nx\">val<\/span><span class=\"p\">,<\/span> <span class=\"nx\">index<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n\t\t<span class=\"nx\">console<\/span><span class=\"p\">.<\/span><span class=\"nx\">log<\/span><span class=\"p\">(<\/span><span class=\"nx\">index<\/span> <span class=\"o\">+<\/span> <span class=\"s2\">\" : \"<\/span> <span class=\"o\">+<\/span> <span class=\"nx\">val<\/span><span class=\"p\">);<\/span>\n\t<span class=\"p\">});<\/span><\/code><\/pre>\n<\/figure>\n<p>The above script will print out all the supplied arguments, when executed it will generate the following out:<\/p>\n<div class=\"highlighter-rouge\">\n<pre class=\"highlight\"><code>$ node script.js argument-1 -f -k -d \n0: node\n1: \/Users\/mohammed\/code\/script.js\n2: argument-1\n3: -f\n4: -k\n5: -d\n<\/code><\/pre>\n<\/div>\n<p>To remove \u2018node\u2019 and path from your script can slice out those arguments:<\/p>\n<div class=\"highlighter-rouge\">\n<pre class=\"highlight\"><code>var args = process.argv.slice(2);\n<\/code><\/pre>\n<\/div>\n<p>There several libraries out there that will make the task of accepting arguments easier, but if you are doing something trivial, I would suggest sticking to the default method.<br \/>\nOne notable example of such library is <a href=\"https:\/\/github.com\/chriso\/cli\">cli<\/a>.<\/p>\n<h2 id=\"colors-on-the-command-line\">Colors on the command line<\/h2>\n<p>If you want to display messages with different colours, there is a nice library called as <a href=\"https:\/\/www.npmjs.org\/package\/colors\">colors<\/a> to do it effortlessly.<br \/>\nInstall it via nom:<\/p>\n<div class=\"highlighter-rouge\">\n<pre class=\"highlight\"><code>$ npm install colors\n<\/code><\/pre>\n<\/div>\n<p>And then include it in your script:<\/p>\n<div class=\"highlighter-rouge\">\n<pre class=\"highlight\"><code>var colors = require('.\/colors');\t\n\nconsole.log('hello'.green); \/\/ outputs green text\nconsole.log('i like cake and pies'.underline.red) \/\/ outputs red underlined text\nconsole.log('inverse the color'.inverse); \/\/ inverses the color\nconsole.log('OMG Rainbows!'.rainbow); \/\/ rainbow (ignores spaces)\n<\/code><\/pre>\n<\/div>\n<h2 id=\"exit-event\">Exit event<\/h2>\n<p>If you want to preform some operation on exit, you can listen to the exit event. Once the exit event is called there is no way to prevent the exiting of the event loop, hence you must only perform <strong>synchronous<\/strong> operations in this handler.<\/p>\n<div class=\"highlighter-rouge\">\n<pre class=\"highlight\"><code>process.on('exit', function(code) {\n\tconsole.log(\"About to exit\");\n});\n<\/code><\/pre>\n<\/div>\n<h2 id=\"exit-codes\">Exit codes<\/h2>\n<p>Node.js provides supports for exit codes, if you script exits without any error, the exit code should be 0. If your script exits with an error it should be 1 or higher.<br \/>\nExit codes in node.js are passed using <strong>process.exit()<\/strong>.<\/p>\n<div class=\"highlighter-rouge\">\n<pre class=\"highlight\"><code>process.exit(0)\n<\/code><\/pre>\n<\/div>\n<h2 id=\"process-object\">Process object<\/h2>\n<p><strong>process.uptime()<\/strong><br \/>\nReturns the number of seconds the script has been running<\/p>\n<p><strong>process.memoryUsage()<\/strong><br \/>\nReturns an object describing the memory usage<\/p>\n<p><strong>process.pid<\/strong><br \/>\nThe PID of the process<\/p>\n<p>If you have any questions or suggestions feel free to comment.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Node.js is becoming very popular platform for creating command line utilities, so I am writing this blog post to help you get started. Creating an executable script To create an executable script you will have to add shebang at the top of the script. E.g: #!\/usr\/bin\/env node console.log(&#8220;First program&#8221;); Assuming you are on a UNIX [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-10","post","type-post","status-publish","format-standard","hentry","category-nodejs"],"_links":{"self":[{"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/posts\/10","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/comments?post=10"}],"version-history":[{"count":0,"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/posts\/10\/revisions"}],"wp:attachment":[{"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/media?parent=10"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/categories?post=10"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/tags?post=10"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}