Yargs Package
In the 3rd part of the blog, we explored how we can get the user input from the command line through a global variable “process.argv”. However, these data were not phrased for us, and we should write some logic and codes to extract them and used them to build our application. As we already mention these operations are not the core of our application, so we need to find the right package that cares of these common tasks for us in a way to focus on the things that make our app unique.
Yargs npm package
Let’s install the Yargs module and required on our module as we learned already previously.
npm i yargs
Require in the module
const yargs = require('yargs')
We are going to explore the difference between using, process.argv & yargs. argv, by printing in the console the outcome of both.
We can see that the first value, the one provided by Node, is an array with a lot of information printed that we do not require. The second value, the one from yargs, is an object with an underscore property were we going to place some argument soon, ad the $0, which is the name of the file we executed.
It’s time to test how yargs will work in our favour and help us to phrase batter the information passed from the command line, in the terminal run this command.
node app.js add --title="Life is better with code"
We still have the same two values printed but with additional information now. As we can notice the yargs one give us the info in a key-value pair, making this a valuable tool to use in a way to have easy access to the information passed in the command line.
Another thing we need to change is the version number because by default yargs start at version 1.0.0, and to check this we can run:
node app.js --version
To update to the latest version, in my case 15.4.1, we need to run this line of code after requiring yargs.
yargs.version(‘15.4.1’)
The aim is to set up yargs to work with the commands we needed to do with such as: add, remove, read and list notes.
Add command
We first need a command name an optional description, and a handler function, that at the moment we go to just to log to the console a test string.
remember to set at “yargs.parse()” in the file, to be able to see the command methods works.
yargs.command({command: "add",description: "Add notes",handler: function(){console.log("we are adding a note ")}})yargs.command({command: "remove",description: "Remove notes",handler: function(){console.log("we are Removing a note ")}})yargs.command({command: "read",description: "Read notes",handler: function(){console.log("we are Reading a note ")}})yargs.command({command: "list",description: "Add notes",handler: function(){console.log("we are adding a note ")}})
These are the list of the 4 commands we need to start building the application. If we run in the terminal:
node app.js --help
we should see a list of the new commands we added like this:
In additional to the properties we added there is one more important one to add, the builder properties, which the value is an object {}. In the object we can write all the options that we want the giving command to support. Exaple for the add comand:
builder: {
title:{
describe: "Note title"
}}
Inside the builder our first option is “title” which has another object as value with the describe property inside. Now in the handler function we need to pass an argument, which will call argv:
handler: function(argv) { console.log('Add note', argv)}
If we access now to our argv argument we can use the title the user has passed in. In the command line to run this command write:
node app.js add --title="this is a new title"
And if you followed all the instruction you should see something like this:
If now we want to make the tile a requirement to run this command, mean that without the application should not run, we need to add a “demandOption” property inside the title option and set the value to true.
builder: {title: { describe: "add title ", demandOption: true }},
If we try to run this command now in the terminal without providing a title we can see an error coming that says “Missing required argument: title”. We can add an additional validation by enforcing the value to be a string. To do that we need to add an additional property in the title object called “type” and set the value equal to “string”.
builder: {title: { describe: "add title ", demandOption: true, type: 'string' }},
We can now complete our add command by adding another value to the builder the body properties.
To run both command :
node app.js add --title="the title" --body="the note body"
Wow!! we been able to set the first functionality of our app whchi is to get the user input and used them in our logic to do something. At the moment we just printing the value but in the next blog we will explore how we can save this data and create a data store.
See you next bog folks!!