Yargs - Build Interactive command-line tools in Node

Yargs is a third-party package that helps the Node js developers in parsing the command line arguments.

Node provides a global object i.e process that helps Node developers to read command-line arguments but it has few limitations.

To Install:
npm install yargs




Import the yargs package in your Js file

  const yargs = require('yargs')

 Yargs divides the arguments into two parts:
1. command
2. options (passed using --)

By default, node has 2 inbuilt options i.e help, version


In the above example, you can see that options are shown with -- . In the same way, you can add options to your arguments.

Read the command line arguments using yargs.argv


In the above example, yargs will take "addToCart" as a command and --title and --desc are the options.

Create an addToCart command in yargs

const yargs = require('yargs')

yargs.command({
    command:'addToCart',
    describe : 'Add the new product to the Cart',
    builder : {
        title: {
            describe : "Title of the Product"
        }
    },
    handler : function(argv){
        console.log("Title of Product: " + argv.title)
    }
})

yargs.parse()


Yargs.parse()
tells the yargs package that it has to read arguments from the command line. Without this command, yargs will not read the arguments from the command line.

describe
is the optional parameter that you can use to add a description to the command.

The handler is the function, that gets the arguments as a parameter. Here you can get the title and description of the product. You can either save the details into the database, log them or anything you want to do in the handler section.

The builder property defines the options for your command.



In the above code, the title option is not mandatory, If you do not pass you won't get any error but you will get the title as "undefined". So to make sure that everyone has to pass the title when using the command. Use demandOption = true.


const yargs = require('yargs')
yargs.command({
    command:'addToCart',
    describe : 'Add the new product to the Cart',
    builder : {
        title: {
            describe : "Title of the Product",
            demandOption:true
        }
    },
    handler : function(argv){
        console.log("Title of Product: " + argv.title)
    }
})

yargs.parse()


Now, If you do not pass the title with the command, you get error



Note that the title option is marked as [required].

By default, the options are boolean and has default value as true. If you don't pass any value to the title option, it will automatically take the "true" value.



const yargs = require('yargs')
yargs.command({
    command:'addToCart',
    describe : 'Add the new product to the Cart',
    builder : {
        title: {
            describe : "Title of the Product",
            demandOption:true,
            type: "string"
        }
    },
    handler : function(argv){
        console.log("Title of Product: " + argv.title)
    }
})

yargs.parse()

I hope you liked this post 😊. Please share.