Node.js & NPM

Making CLI app with ease using commander.js and Inquirer.js

Uday Hiwarale
JsPoint

--

In my previous post about Making CLI Application in Node.js (I am going to call it the previous blog), which you can find it below.

In this article, I talked about basic steps to make a global module which can be accessed from anywhere on the system using terminal window or command line tool.

Today, we are extending functionality of that application to access wide variety of command arguments and making our app simpler to use for end users. There are two great npm modules available to achieve that. Commander.js (https://github.com/tj/commander.js) is node module which will help us parse process.argv in much easy and better way while Inquirer.js (https://github.com/SBoudrias/Inquirer.js) will help us design interactive CLI application. This is going to be very simple tutorial to showcase how a simple example would work combining these two modules, but you should explore more functionality of these modules from their documentation.

I advice you to read my previous blog first so that you can understand how to create a npm global app that works with CLI.

Let’s talk little bit about what we are going to develop. We are making a CLI application for coffee shop. Using this app, people can either view coffee menu or order a coffee of their choice. A user has choice to select coffee type, sugar level, regular or decaf, served hot or cold, served in mug, cup or takeaway package, stirrer included or not etc.

CLI commands a person can use are like

coffee-shop list
coffee-shop order [type] -d --sugar-level low ...

We are going to explore more about command arguments to incorporate all choices a person can use to customize his/her coffee. But at this point, let’s get all our project dependencies installed. I hope you have read by previous blog, so I am not going to explain everything about setting up the project.

--

--