Open In App

How to create a CLI based calculator app using Node.js ?

Improve
Improve
Like Article
Like
Save
Share
Report

 In this article, we are going to learn about passing arguments to the command line using node.js.

Approach: We are going to use the process object, process object contains various properties in which we will use the arguments property. This will helps to fetch the arguments passed in the command line and then we will loop through every single element and perform the different operations.

Functionalities: In this article, we are going to create a simple application where we can add or subtract numbers using the command line argument:

The below command will add the numbers num1 and num2 and print the result to the console.

node app.js add num1 num2

The below command will subtract the numbers num1 and num2 and print the result to the console.

node app.js subtract num1 num2

The below command will multiply the numbers num1 and num2 and print the result to the console.

node app.js multiply num1 num2

The below command will divide the numbers num1 and num2 and print the result to the console.

node app.js divide num1 num2

Implementation: Below is the step-by-step implementation of the above approach with the above functionalities.

Step 1: Create and Open your project folder in the Code Editor.

Step 2: Create a file ‘app.js’: Inside this file, we are going to write the code.

Step 3: Create a constant ‘argvs’ and set it to process.argv properties: This will contain the arguments that the user passes in the command line.

const argvs = process.argv

Step 4: Create another const ‘argv’ and set it to the ‘argvs’ with the slice method: Since the first two arguments passed in the command line are irrelevant to the program because the first one is ‘node’ itself since we use it to run the application and second is the name of the file which we want it to execute in our case its ‘app.js’, so we have to remove it by using slice method.

const argv = argvs.slice(2)

Step 5: Set the remaining arguments inside ‘argv’ array to different constants:

const operation = argv[0]
const operator1 = parseInt(argv[1])
const operator2 = parseInt(argv[2]) 

Step 6: Set if operator for Add: If the first contact which is the ‘operation’ value is added then we execute the following command and print the addition to the terminal using the console.log command.

if (operation === 'add') {
       console.log(operation + ' is ' 
           + (operator1 + operator2));
}

Step 7: Set if operator for Subtract: If the first contact which is the ‘operation’ value is subtracted then we execute the following command and print the subtraction to the terminal using the console.log command.

if (operation === 'subtract') {
       console.log(operation + ' is ' 
           + (operator1 - operator2));
}

Step 8: Set if operator for Multiplication: If the first contact which is the ‘operation’ value is multiplied then we execute the following command and print the multiplication to the terminal using the console.log command.

if (operation === 'multiply') {
      console.log(operation + ' is ' 
          + (operator1 * operator2));
}

Step 9: Set if operator for Division: If the first contact which is the ‘operation’ value is divided then we execute the following command and print the division to the terminal using the console.log command.

if (operation === 'divide') {
      console.log(operation + ' is ' 
          + (operator1 / operator2));
}

Complete Code:

Javascript




const argvs = process.argv
const argv = argvs.slice(2)
const operation = argv[0]
const operator1 = parseInt(argv[1])
const operator2 = parseInt(argv[2])
 
if (operation === 'add') {
    console.log(operation + ' is '
        + (operator1 + operator2));
}
if (operation === 'subtract') {
    console.log(operation + ' is '
        + (operator1 - operator2));
}
if (operation === 'multiply') {
    console.log(operation + ' is '
        + (operator1 + operator2));
}
if (operation === 'divide') {
    console.log(operation + ' is '
        + (operator1 - operator2));
}


Inside the command line use the below commands to see different outputs:

node app.js add 4 5

This will add the numbers 4 and 5 and print the result 9 to the console.

node app.js subtract 10 5

This will subtract the numbers 10 and 5 and print the result 5 to the console.

Output:

 



Last Updated : 08 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads