<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rushan Khan</title>
    <description>The latest articles on DEV Community by Rushan Khan (@rushankhan1).</description>
    <link>https://dev.to/rushankhan1</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F477205%2F4f29ec6b-6f70-46d7-ab25-2d1a960e4e6f.jpeg</url>
      <title>DEV Community: Rushan Khan</title>
      <link>https://dev.to/rushankhan1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rushankhan1"/>
    <language>en</language>
    <item>
      <title>Write Effective Markdown in Emacs (with live preview)</title>
      <dc:creator>Rushan Khan</dc:creator>
      <pubDate>Sun, 25 Jul 2021 12:00:06 +0000</pubDate>
      <link>https://dev.to/rushankhan1/write-effective-markdown-in-emacs-with-live-preview-41p9</link>
      <guid>https://dev.to/rushankhan1/write-effective-markdown-in-emacs-with-live-preview-41p9</guid>
      <description>&lt;p&gt;Do you use &lt;em&gt;Emacs&lt;/em&gt; and write a lot of markdown? Or are you interested in learning more about Emacs packages? If yes then this article is for you. &lt;br&gt;&lt;/p&gt;

&lt;p&gt;Today we are going to be talking about getting &lt;strong&gt;live preview&lt;/strong&gt; while writing markdown in emacs. This was a problem that I used to face everyday, I couldn't write markdown in emacs because there was no preview, writing blind markdown and then copying pasting it in a web editor, fixing bugs/errors and doing the whole thing over wasn't really efficient and editing text directly in the web editor without evil was painful. &lt;br&gt;&lt;/p&gt;

&lt;p&gt;So I looked around and found a great, less popular emacs package called &lt;a href="https://github.com/seagle0128/grip-mode"&gt;&lt;strong&gt;grip-mode&lt;/strong&gt;&lt;/a&gt; which is basically an emacs integration for the command-line python application called &lt;a href="https://github.com/joeyespo/grip"&gt;&lt;strong&gt;grip&lt;/strong&gt;&lt;/a&gt;. Grip starts a live server locally to render a project's README file using the &lt;a href="https://docs.github.com/en/rest/reference/markdown"&gt;GitHub Markdown API&lt;/a&gt; so you can get live preview before pushing to GitHub or using the web editor.&lt;br&gt; So enough talk let's see how we can make this work. &lt;br&gt; &lt;br&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Installing Prerequisites
&lt;/h2&gt;

&lt;p&gt;The only prerequisites required for this are: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.python.org/downloads/"&gt;Python&lt;/a&gt; ;and&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/joeyespo/grip"&gt;Grip&lt;/a&gt; (just do &lt;code&gt;pip install grip&lt;/code&gt; in your home directory) &lt;br&gt; &lt;br&gt; &lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  Installing grip-mode
&lt;/h2&gt;

&lt;p&gt;Grip is available on MELPA so installing it is as simple as doing:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;M-x package-install RET grip-mode RET&lt;/code&gt; &lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  Setting up grip mode
&lt;/h2&gt;

&lt;p&gt;Now we configure grip mode. The following customizations are supposed to go in your &lt;code&gt;.emacs&lt;/code&gt; file or your &lt;code&gt;init.el&lt;/code&gt; file, whichever you use.&lt;/p&gt;

&lt;p&gt;First we let Emacs know the exact location where the grip binary is installed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;;; Path to grip
(setq grip-binary-path "/absolute/path/to/grip")

;; in my case it is /home/rushan/.local/bin/grip

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; If you don't know where &lt;em&gt;grip&lt;/em&gt; is installed on your system, you can easily check using &lt;code&gt;~$ which grip&lt;/code&gt; the output will be the absoulute path to grip. &lt;br&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Next, we add a hook to start grip mode autmatically when &lt;code&gt;markdown-mode&lt;/code&gt; or &lt;code&gt;org-mode&lt;/code&gt; is activated:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;;; Start grip when opening a markdown/org buffer
(add-hook 'markdown-mode-hook #'grip-mode)
(add-hook 'org-mode-hook #'grip-mode)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also specify a browser for the live preview:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;;; set a browser for preview
(setq grip-url-browser "google-chrome")

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default or when set to &lt;code&gt;nil&lt;/code&gt; grip mode will open up in the system's default browser.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: While specifying a name, the name should be the name of the &lt;strong&gt;executable program&lt;/strong&gt;, which means that &lt;strong&gt;if you execute the name in shell then that browser should launch.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Also, it is good to update the preview on save to optimize performance, you can do that by:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;;; update the preview after file saves only, instead of after every text change. 
(setq grip-update-after-change nil)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;p&gt;This is the basic setup for &lt;code&gt;grip-mode&lt;/code&gt;, other customizations can be found at &lt;a href="https://github.com/seagle0128/grip-mode#customize"&gt;grip's docs&lt;/a&gt;. &lt;strong&gt;But Wait!&lt;/strong&gt; this is not all! If you tried to use grip-mode with this setup you'll notice that it works but it is &lt;strong&gt;extremely limited&lt;/strong&gt;. &lt;br&gt;&lt;/p&gt;

&lt;p&gt;By default users which haven't authenticated themselves will only get a certain amount of requests to the GitHub APIs, which is, sadly, less than usable.&lt;/p&gt;

&lt;p&gt;To &lt;strong&gt;remove this limitation&lt;/strong&gt; we need to tell grip our username and password (personal access token) for API authentication:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;;; GitHub username for API authentication
(setq grip-github-user "your_username")

;; GitHub password or auth token for API auth
(setq grip-github-password "your_personal_access_token")

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you haven't generated a personal access token before, here is how you do it: &lt;a href="https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token"&gt;GitHub Docs&lt;/a&gt;. &lt;br&gt;&lt;/p&gt;

&lt;p&gt;After this is done your limitation should now be removed, you would still be under a new limit though, but that limit is very generous and should be more than enough for normal usage. &lt;br&gt; &lt;br&gt; &lt;br&gt;&lt;/p&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I hope this article was helpful to you guys and please feel free to ask any questions in the comments. &lt;/p&gt;

&lt;p&gt;You can also connect with me on other social media platforms :) &lt;br&gt;&lt;/p&gt;

&lt;p&gt;Fork me on &lt;a href="https://github.com/RushanKhan1"&gt;GitHub&lt;/a&gt; &lt;br&gt;&lt;br&gt;
Connect with me on &lt;a href="https://www.linkedin.com/in/rushankhan/"&gt;LinkedIn&lt;/a&gt; &lt;br&gt;&lt;br&gt;
Read my other stories on &lt;a href="https://hackernoon.com/u/rushankhan"&gt;Hackernoon&lt;/a&gt; &lt;br&gt;&lt;/p&gt;

&lt;p&gt;Happy Coding! &lt;br&gt;&lt;/p&gt;

</description>
      <category>markdown</category>
      <category>emacs</category>
      <category>productivity</category>
      <category>elisp</category>
    </item>
    <item>
      <title>Build a CLI with Node.js</title>
      <dc:creator>Rushan Khan</dc:creator>
      <pubDate>Mon, 18 Jan 2021 10:03:43 +0000</pubDate>
      <link>https://dev.to/rushankhan1/build-a-cli-with-node-js-4jbi</link>
      <guid>https://dev.to/rushankhan1/build-a-cli-with-node-js-4jbi</guid>
      <description>&lt;p&gt;&lt;em&gt;Command-line utilities are the most basic and beautiful apps ever created, the apps that started it all.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We use command-line utilities every day, whether it be git, grep, awk, npm, or any other terminal app. CLIs are super useful and usually the fastest way to get something done. Do you have something in your specific workflow that you have to do over and over again? Chances are that can be automated with a CLI.                      &lt;/p&gt;

&lt;p&gt;We are going to use &lt;a href="https://nodejs.org/en/" rel="noopener noreferrer"&gt;&lt;strong&gt;Node.js&lt;/strong&gt;&lt;/a&gt; to make our CLI if it wasn’t clear from the title itself. Why? Because the Node.js ecosystem has thousands of extremely useful packages that we can utilize to achieve what we are trying to do. Whatever it may be that you are trying to do, it is highly probable that there exists a package for it on &lt;a href="https://www.npmjs.com/" rel="noopener noreferrer"&gt;npm&lt;/a&gt;, also node has built-in libraries to do a lot of things like &lt;em&gt;handling files, launching other applications, asserting tests, etc.&lt;/em&gt; Apart from that CLIs built in Node.js are &lt;strong&gt;highly portable,&lt;/strong&gt; meaning they are easy to install on different OSs.                                  &lt;/p&gt;

&lt;p&gt;For the purpose of this tutorial, we’ll be building a simple CLI to &lt;strong&gt;translate between languages&lt;/strong&gt;. We’ll accept string type arguments, parse them into a sentence, shoot them off to a translation API which will fetch us the translations, and then display the result. The complete code for this can be found on the &lt;a href="https://github.com/RushanKhan1/termTranslate" rel="noopener noreferrer"&gt;&lt;strong&gt;Github repository&lt;/strong&gt;&lt;/a&gt;. Let’s dive right into it!&lt;br&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Prerequisites
&lt;/h1&gt;

&lt;p&gt;Here are the tools that are required to make the CLI so make sure you have them before starting:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; A recent version of &lt;a href="https://nodejs.org/en/download/" rel="noopener noreferrer"&gt;Node.js&lt;/a&gt; installed.&lt;/li&gt;
&lt;li&gt; A text editor.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;and that’s probably it.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Setting up the project
&lt;/h1&gt;

&lt;p&gt;We’ll be setting up a basic Node.js project:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Open up your terminal.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;2. Create a folder for the project.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

~$mkdir termTranslate


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;3. Navigate to it.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

~$cd termTranslate 


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;4. Initialize a Node.js project in that folder.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

~$npm init


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;5. Fill in the prompt.&lt;/p&gt;

&lt;p&gt;Your project is now set up.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Building the basic CLI
&lt;/h1&gt;

&lt;p&gt;Now that we have our node project ready we move to actually making the CLI. Here’s what you have to do:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Create a folder named &lt;code&gt;bin&lt;/code&gt; in the root directory of your project.&lt;/li&gt;
&lt;li&gt; Inside &lt;code&gt;bin&lt;/code&gt; create a file called &lt;code&gt;index.js&lt;/code&gt; This is going to be the entry point of our CLI.&lt;/li&gt;
&lt;li&gt; Now open the &lt;code&gt;package.json&lt;/code&gt; file and change the “main” part to &lt;code&gt;bin/index.js&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; Now manually add another entry into the &lt;code&gt;package.json&lt;/code&gt; file called &lt;code&gt;bin&lt;/code&gt; and set it’s key to &lt;code&gt;tran&lt;/code&gt; and it’
s value to &lt;code&gt;./bin/index.js&lt;/code&gt;. The addition should look something like this:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

"bin": {  
    "tran": "./bin/index.js"  
  }


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The key, &lt;code&gt;tran&lt;/code&gt;, is the &lt;strong&gt;keyword for calling the CLI&lt;/strong&gt;. This is the keyword that people will type in the terminal for using your CLI. Be free to name it whatever you like, although I would suggest keeping the name short &lt;br&gt;
and semantic so that it’s quick to type and easy to remember.                                                 &lt;/p&gt;

&lt;p&gt;The name defined here is &lt;strong&gt;not permanent&lt;/strong&gt; and can be changed whenever you like.&lt;/p&gt;

&lt;p&gt;Your entire &lt;code&gt;package.json&lt;/code&gt; file should look something like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

{  
    "name": "termtran",  
    "version": "1.0.0",  
    "description": "A CLI to translate between languages in the terminal",  
    "main": "bin/index.js",  
    "scripts": {  
 "test": "echo "Error: no test specified" &amp;amp;&amp;amp; exit 1"  
    },  
    "keywords": [  
 "cli"  
    ],  
    "bin": {  
 "tran": "./bin/index.js"  
    },  

    "author": "Your Name",  
    "license": "ISC"  
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Don’t forget to add the extra comma after adding the new entry into the &lt;code&gt;package.json&lt;/code&gt; file. That is an easy mistake.                                                                                            &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
5. Open the &lt;code&gt;index.js&lt;/code&gt; file in the &lt;code&gt;bin&lt;/code&gt; folder. And put the following code in it:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

#! /usr/bin/env node
console.log("Hello World!");


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The first line starting with &lt;code&gt;#!&lt;/code&gt; is called a &lt;code&gt;shebang&lt;/code&gt; line or a &lt;code&gt;bang&lt;/code&gt; line. A shebang line is used to specify the absolute path to the interpreter that will run the below code. The shebang line used here is for Linux or UNIX type systems but node requires it for Windows and macOS too, for proper installation and execution of the script.                                                                                                    &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
Now let’s install and test our CLI.&lt;/p&gt;

&lt;p&gt;People may call our CLI from anywhere in the system so let's install it &lt;strong&gt;globally&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Navigate to the root directory of the project and then run&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

~$npm install -g .


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;-g&lt;/code&gt; flag tells npm to install the package globally on the system.&lt;/p&gt;

&lt;p&gt;Test the CLI by typing the specified keyword in the terminal.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

~$tran


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If everything was done correctly then you should be greeted with the message which we console.logged in the &lt;code&gt;in&lt;br&gt;
dex.js&lt;/code&gt; file.&lt;br&gt;
&lt;br&gt;&lt;br&gt;
Something like this:                                                                                                                                                                                       &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzy8ju8oewt91ugnfhm12.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzy8ju8oewt91ugnfhm12.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All good!&lt;/p&gt;



&lt;h1&gt;
  
  
  Handling Command Line arguments
&lt;/h1&gt;

&lt;p&gt;Our basic CLI is ready and now we move to adding further functionality.&lt;/p&gt;

&lt;p&gt;The most basic task that any CLI does is &lt;em&gt;handling command-line arguments&lt;/em&gt;. In our CLI, we will be receiving the language name and the sentence to be translated as arguments and then we will parse it.                     &lt;/p&gt;

&lt;p&gt;Although Node.js offers built-in functionality for handling command line arguments, we are going to use an npm package called &lt;a href="https://www.npmjs.com/package/yargs" rel="noopener noreferrer"&gt;&lt;strong&gt;yargs&lt;/strong&gt;&lt;/a&gt; 🏴‍☠ which is specifically made for building CLI&lt;br&gt;
s. yargs will simplify our process of parsing arguments and help us organize command line flags.              &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Install yargs&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

~$npm i yargs


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;2. After installing it, include the module in your &lt;code&gt;index.js&lt;/code&gt; :&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

~$const yargs = require("yargs");


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;3. Then create the &lt;code&gt;options&lt;/code&gt; object containing all your command line flags:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const usage = "\nUsage: tran &amp;lt;lang_name&amp;gt; sentence to be translated";const options = yargs  
      .usage(usage)  
      .option("l", {alias:"languages", describe: "List all supported languages.", type: "boolean", demandOption
: false })                                                                                                    
      .help(true)  
      .argv;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the above code, I have defined an option &lt;code&gt;-l&lt;/code&gt; which, when passed will print all the supported languages by the API, we will implement this later. Yargs provides us with &lt;code&gt;--help&lt;/code&gt; and &lt;code&gt;--version&lt;/code&gt; flags by default.       &lt;/p&gt;

&lt;p&gt;If you want an option to be compulsory then you can set it’s &lt;code&gt;demandOption&lt;/code&gt; value to &lt;code&gt;true&lt;/code&gt; , this will get yar&lt;br&gt;
gs to throw a &lt;code&gt;Missing argument&lt;/code&gt; error if the flag is not provided.&lt;br&gt;
&lt;br&gt;&lt;br&gt;
Testing it:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Foxqt5569yrxlshurtt05.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Foxqt5569yrxlshurtt05.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Nice!&lt;/p&gt;



&lt;blockquote&gt;
&lt;p&gt;All the arguments that you pass with the command gets stored under the list&lt;code&gt;yargs.argv._&lt;/code&gt; unless the argument begin with a &lt;code&gt;—&lt;/code&gt; or a &lt;code&gt;--&lt;/code&gt; in that case, it is treated as a flag with a default value of boolean. You can console.log &lt;code&gt;yargs.argv&lt;/code&gt; to get a better picture of how the arguments are stored.                            &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Access the value of the passed flags using &lt;code&gt;yargs.argv.flagname&lt;/code&gt;.&lt;/p&gt;



&lt;h1&gt;
  
  
  Adding Utility Functions
&lt;/h1&gt;

&lt;p&gt;Now it’s time to add utility functions.&lt;/p&gt;

&lt;p&gt;I plan to take input as:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

~$tran lang_name the sentence to be translated 


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;So we will need to parse the arguments.&lt;/p&gt;

&lt;p&gt;We can write all the utility functions in our &lt;code&gt;index.js&lt;/code&gt; but that wouldn’t look neat so I will make a separate file &lt;code&gt;utils.js&lt;/code&gt; for all functions. Here’s what we need to do:                                                 &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Create another file called &lt;code&gt;utils.js&lt;/code&gt; in the &lt;code&gt;bin&lt;/code&gt; folder.&lt;/li&gt;
&lt;li&gt; Include the file in your &lt;code&gt;index.js&lt;/code&gt; :&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const utils = require('./utils.js')


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;3. Create a function for parsing the sentence:&lt;/p&gt;

&lt;p&gt;Write the function in&lt;code&gt;utils.js&lt;/code&gt; and then export it:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

module.exports = { parseSentence: parseSentence };function parseSentence(words) {  
    var sentence = "";  
    for(var i = 1; i &amp;lt; words.length; i++) {  
 sentence = sentence + words[i] + " ";  
    }


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Call it in &lt;code&gt;index.js&lt;/code&gt; :&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

var sentence = utils.parseSentence(yargs.argv._);


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;4. Create a function to show help when no argument is passed:&lt;/p&gt;

&lt;p&gt;Create a function in your &lt;code&gt;utils.js&lt;/code&gt; :&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

module.exports = { showHelp: showHelp, parseSentence: parseSentence };const usage = "\nUsage: tran &amp;lt;lang_name
&amp;gt; sentence to be translated";
function showHelp() {                                                            
    console.log(usage);  
    console.log('\nOptions:\r')  
    console.log('\t--version\t      ' + 'Show version number.' + '\t\t' + '[boolean]\r')  
    console.log('    -l, --languages\t' + '      ' + 'List all languages.' + '\t\t' + '[boolean]\r')  
    console.log('\t--help\t\t      ' + 'Show help.' + '\t\t\t' + '[boolean]\n')  
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Call it in &lt;code&gt;index.js&lt;/code&gt; :&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

if(yargs.argv._[0] == null){  
    utils.showHelp();  
    return;  
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;5. Write a function in &lt;code&gt;utils.js&lt;/code&gt; to show all supported languages:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

module.exports = { showAll: showAll, showHelp: showHelp, parseSentence: parseSentence};  
function showAll(){  
    console.log(chalk.magenta.bold("\nLanguage Name\t\tISO-639-1 Code\n"))  
    for(let [key, value] of languages) {  
 console.log(key + "\\t\\t" + value + "\\n")  
    }  
}
let languages = new Map();
languages.set('afrikaans',      'af')  
languages.set('albanian', 'sq')  
languages.set('amharic', 'am')  
languages.set('arabic',         'ar')  
languages.set('armenian', 'hy')  
languages.set('azerbaijani', 'az')  
languages.set('basque',         'eu')  
languages.set('belarusian', 'be')  
languages.set('bengali', 'bn')  
languages.set('bosnian', 'bs')  
languages.set('bulgarian', 'bg')  
languages.set('catalan', 'ca')  
languages.set('cebuano', 'ceb')   
languages.set('chinese',        'zh')   
languages.set('corsican', 'co')  
languages.set('croatian', 'hr')  
languages.set('czech',         'cs')  
languages.set('danish',         'da')  
languages.set('dutch',         'nl')  
languages.set('english', 'en')  
languages.set('esperanto', 'eo')  
languages.set('estonian', 'et')  
languages.set('finnish', 'fi')  
languages.set('french',         'fr')  
languages.set('frisian', 'fy')  
languages.set('galician', 'gl')  
languages.set('georgian', 'ka')  
languages.set('german',         'de')  
languages.set('greek',         'el')  
languages.set('gujarati', 'gu')  
languages.set('haitian creole', 'ht')  
languages.set('hausa',         'ha')  
languages.set('hawaiian', 'haw') // (iso-639-2)  
languages.set('hebrew',         'he') //or iw  
languages.set('hindi',         'hi')  
languages.set('hmong',         'hmn') //(iso-639-2)  
languages.set('hungarian', 'hu')  
languages.set('icelandic', 'is')  
languages.set('igbo',         'ig')  
languages.set('indonesian', 'id')  
languages.set('irish',         'ga')  
languages.set('italian', 'it')  
languages.set('japanese', 'ja')  
languages.set('javanese', 'jv')  
languages.set('kannada', 'kn')  
languages.set('kazakh',         'kk')  
languages.set('khmer',         'km')  
languages.set('kinyarwanda', 'rw')  
languages.set('korean',         'ko')  
languages.set('kurdish', 'ku')  
languages.set('kyrgyz',         'ky')  
languages.set('lao',         'lo')  
languages.set('latin',         'la')  
languages.set('latvian', 'lv')  
languages.set('lithuanian', 'lt')  
languages.set('luxembourgish', 'lb')  
languages.set('macedonian', 'mk')  
languages.set('malagasy', 'mg')  
languages.set('malay',         'ms')  
languages.set('malayalam', 'ml')  
languages.set('maltese', 'mt')  
languages.set('maori',         'mi')  
languages.set('marathi', 'mr')  
languages.set('mongolian', 'mn')  
languages.set('burmese', 'my')  
languages.set('nepali',         'ne')  
languages.set('norwegian', 'no')  
languages.set('nyanja',         'ny')  
languages.set('odia',         'or')  
languages.set('pashto',         'ps')  
languages.set('persian', 'fa')  
languages.set('polish',         'pl')  
languages.set('portuguese', 'pt')  
languages.set('punjabi', 'pa')  
languages.set('romanian', 'ro')  
languages.set('russian', 'ru')  
languages.set('samoan',         'sm')  
languages.set('scots',          'gd')//gd gaelic  
languages.set('serbian', 'sr')  
languages.set('sesotho', 'st')  
languages.set('shona',         'sn')  
languages.set('sindhi',         'sd')  
languages.set('sinhalese', 'si')  
languages.set('slovak',         'sk')  
languages.set('slovenian', 'sl')  
languages.set('somali',         'so')  
languages.set('spanish', 'es')  
languages.set('sundanese', 'su')  
languages.set('swahili', 'sw')  
languages.set('swedish', 'sv')  
languages.set('tagalog', 'tl')  
languages.set('tajik',         'tg')  
languages.set('tamil',         'ta')  
languages.set('tatar',         'tt')  
languages.set('telugu',         'te')  
languages.set('thai',         'th')  
languages.set('turkish', 'tr')  
languages.set('turkmen', 'tk')  
languages.set('ukrainian', 'uk')  
languages.set('urdu',         'ur')  
languages.set('uyghur',         'ug')  
languages.set('uzbek',         'uz')  
languages.set('vietnamese', 'vi')  
languages.set('welsh',         'cy')  
languages.set('xhosa',         'xh')  
languages.set('yiddish',        'yi')  
languages.set('yoruba',         'yo')  
languages.set('zulu',    'zu')


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
Here I have created a &lt;strong&gt;hash map&lt;/strong&gt; to map all the language names to their ISO-639–1 code. This will &lt;strong&gt;&lt;em&gt;serve two purposes&lt;/em&gt;&lt;/strong&gt;, firstly it will help display all languages when needed, secondly, the API only takes the language code so even if the user enters the language name we can swap it with the language code before passing it to the API. Sneaky! 🤫. The swap would be in &lt;strong&gt;constant time&lt;/strong&gt; since we are using a hash map.                    &lt;/p&gt;

&lt;p&gt;Call the &lt;code&gt;showAll()&lt;/code&gt; function in your &lt;code&gt;index.js&lt;/code&gt; if the &lt;code&gt;-l&lt;/code&gt; or &lt;code&gt;-languages&lt;/code&gt; flag is true:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

if(yargs.argv.l == true || yargs.argv.languages == true){  
    utils.showAll();  
    return;  
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;6. Now write the function to do the dirty deed we talked about in your &lt;code&gt;utils.js&lt;/code&gt; :&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

module.exports = { parseLanguage: parseLanguage, showAll: showAll, showHelp: showHelp, parseSentence: parseSent
ence };
function parseLanguage (language) {                                                                    
    if(language.length == 2){  
 return language;  
    }  
    if(languages.has(language)){  
 return languages.get(language)  
    }  
    else {  
 console.error("Language not supported!")  
 return; //returning null if the language is unsupported.  
    }  
};


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Convert the language to lower case and then call the function in &lt;code&gt;index.js&lt;/code&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

if(yargs.argv._[0])  
var language = yargs.argv._[0].toLowerCase(); // stores the language.
//parsing the language specified to the ISO-639-1 code.                                                                                              
language = utils.parseLanguage(language);


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;7. Now check if the sentence is empty, if not send it to the API:&lt;/p&gt;

&lt;p&gt;Include the API at the top of your &lt;code&gt;index.js&lt;/code&gt; :&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const translate = require('[@vitalets/google-translate-api](http://twitter.com/vitalets/google-translate-api)')
;if(sentence == ""){                                                                                          
    console.error("\nThe entered sentence is like John Cena, I can't see it!\n")  
    console.log("Enter tran --help to get started.\n")  
    return;
}translate(sentence, {to: language}).then(res =&amp;gt; {console.log("\n" + "\n" + res.text + "\n" + "\n";}).catch
(err =&amp;gt; {                                                                                                     
     console.error(err);  
 });


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Your CLI is complete now! One thing more that you can do is to decorate the output and errors with boxes and colors, we can do that using &lt;a href="https://www.npmjs.com/package/boxen" rel="noopener noreferrer"&gt;boxen&lt;/a&gt; and &lt;a href="https://www.npmjs.com/package/chalk" rel="noopener noreferrer"&gt;chalk&lt;/a&gt;.&lt;br&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Beautification using Boxen and Chalk
&lt;/h1&gt;

&lt;p&gt;We can use terminal colors using &lt;a href="https://www.npmjs.com/package/chalk" rel="noopener noreferrer"&gt;&lt;strong&gt;chalk&lt;/strong&gt;&lt;/a&gt; and boxes to decorate our output using &lt;a href="https://www.npmjs.com/package/boxen" rel="noopener noreferrer"&gt;&lt;strong&gt;boxen&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;.&lt;/strong&gt;                                               &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Install chalk and boxen.&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

npm install chalk boxen


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;2. Include them in your &lt;code&gt;index.js&lt;/code&gt; and &lt;code&gt;utils.js&lt;/code&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const chalk = require('chalk');  
const boxen = require('boxen');


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;3. Add color to the usage constant.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const usage = chalk.hex('#83aaff')("\\nUsage: tran &amp;lt;lang\_name&amp;gt; sentence to be translated");


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;4. Display the output using a beautiful box.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

translate(sentence, {to: language}).then(res =&amp;gt; {console.log("\n" + boxen(chalk.green("\n" + res.text + "\n"
), {padding: 1, borderColor: 'green', dimBorder: true}) + "\n");}).catch(err =&amp;gt; {                            
     console.error(err);  
 });


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Feel free to explore both the packages and add customization to your heart’s content. :)&lt;/p&gt;

&lt;p&gt;The CLI in all its glory:&lt;br&gt;
&lt;br&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fq7kur2yqy3gcbm1jx569.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fq7kur2yqy3gcbm1jx569.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ahh yes&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
Hope you had fun learning how to build your own and fully portable CLI :) because I had a lot of fun.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

&lt;p&gt;The complete code for this can be found at: &lt;a href="https://github.com/RushanKhan1/termTranslate" rel="noopener noreferrer"&gt;https://github.com/RushanKhan1/termTranslate&lt;/a&gt;                                                                                 &lt;/p&gt;

&lt;p&gt;Fork me on &lt;a href="https://github.com/RushanKhan1/" rel="noopener noreferrer"&gt;Github&lt;/a&gt; maybe :)&lt;/p&gt;

&lt;p&gt;Connect with me on &lt;a href="https://www.linkedin.com/in/rushankhan/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Edit: This post is now also featured on &lt;a href="https://hackernoon.com/how-to-build-a-command-line-utility-cli-with-nodejs-gm24315b" rel="noopener noreferrer"&gt;Hackernoon&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>npm</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
