Building Your First CLI Tool

Hello developers!! You must have use multiple CLI tools in your everyday development like echo, ls, cd etc.

Let’s try to understand it in a better way by creating your own simple CLI tool.

In this article, we will create a simple Calculator CLI tool….


This content originally appeared on DEV Community and was authored by Anuradha Aggarwal

Hello developers!! You must have use multiple CLI tools in your everyday development like echo, ls, cd etc.

Let’s try to understand it in a better way by creating your own simple CLI tool.

In this article, we will create a simple Calculator CLI tool.

🎯 What is CLI Tool?

A CLI (Command Line Interface) tool is just a program you run in the terminal by typing commands with your keyboard, instead of clicking buttons in a GUI (Graphical User Interface).

🎯 Project Setup

  1. Setup the Project with initial command
npm init

Project Setup

  1. Create a folder named src in the root directory of your project.

  2. Inside src create a file called index.js This is going to be the entry point of our CLI.

  3. In the package.json file, change the “main” part to src/index.js.

  4. Now manually add another entry into the package.json file called bin and set its key to calc and it's value to ./src/index.js.

Your resultant package.json file would look like this:

{
  "name": "calculator-cli-tool",
  "version": "1.0.0",
  "description": "A CLI tool used for calculations",
  "main": "src/index.js",
  "bin": {
    "calc": "./src/index.js"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

The key calc, is the keyword for calling the CLI. This is the keyword that user will type in the terminal for using your CLI.

🎯 Commander

  • Commander is a popular Node.js library that makes it easy to build CLI tools.

What commander gives you?

  • Commands → Define sub-commands like add, multiply.
  • Arguments → Easily get values like 5 7.
  • Options (flags) → Support things like --verbose or --format json.
  • Automatic help → Generates --help output for free.
  • Versioning → Add --version easily.

Let’s install commander:

npm install commander

In src/index.js file:

#!/usr/bin/env node
const { Command } = require('commander');
const program = new Command();

program
  .name('calc')
  .description('A simple calculator CLI')
  .version('1.0.0');

// Command: add
program
  .command('add <a> <b>')
  .description('Add two numbers')
  .action((a, b) => {
    console.log(Number(a) + Number(b));
  });

// Command: multiply
program
  .command('multiply <a> <b>')
  .description('Multiply two numbers')
  .option('-v, --verbose', 'Show detailed steps')
  .action((a, b, options) => {
    const result = Number(a) * Number(b);
    if (options.verbose) {
      console.log(`${a} * ${b} = ${result}`);
    } else {
      console.log(result);
    }
  });

program.parse();

🎯 Link it globally

Now if you run the below command in your project folder, this creates a global symlink so you can use calc anywhere.

npm link

Now your CLI tool is ready to use anywhere in your system. Let’s try it now:

🎯 CLI Building Blocks

CLI Building Block

As you noticed in the above examples, there are multiple parts in the single commands, let’s break it down.

  • calc is the main command (the tool itself).

  • add, multiply represents sub-command (an action inside calc).

  • 5, 7 represents the arguments (values you pass)

  • —verbose is a option which acts like a flag & modifies behavior

🎯 Wrap Up!!

That's all for this article. Thank you for your time!! Let's connect to learn and grow together. LinkedIn Twitter Instagram


This content originally appeared on DEV Community and was authored by Anuradha Aggarwal


Print Share Comment Cite Upload Translate Updates
APA

Anuradha Aggarwal | Sciencx (2025-08-24T14:14:40+00:00) Building Your First CLI Tool. Retrieved from https://www.scien.cx/2025/08/24/building-your-first-cli-tool/

MLA
" » Building Your First CLI Tool." Anuradha Aggarwal | Sciencx - Sunday August 24, 2025, https://www.scien.cx/2025/08/24/building-your-first-cli-tool/
HARVARD
Anuradha Aggarwal | Sciencx Sunday August 24, 2025 » Building Your First CLI Tool., viewed ,<https://www.scien.cx/2025/08/24/building-your-first-cli-tool/>
VANCOUVER
Anuradha Aggarwal | Sciencx - » Building Your First CLI Tool. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/08/24/building-your-first-cli-tool/
CHICAGO
" » Building Your First CLI Tool." Anuradha Aggarwal | Sciencx - Accessed . https://www.scien.cx/2025/08/24/building-your-first-cli-tool/
IEEE
" » Building Your First CLI Tool." Anuradha Aggarwal | Sciencx [Online]. Available: https://www.scien.cx/2025/08/24/building-your-first-cli-tool/. [Accessed: ]
rf:citation
» Building Your First CLI Tool | Anuradha Aggarwal | Sciencx | https://www.scien.cx/2025/08/24/building-your-first-cli-tool/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.