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
- Setup the Project with initial command
npm init
Create a folder named
srcin the root directory of your project.Inside
srccreate a file calledindex.jsThis is going to be the entry point of our CLI.In the
package.jsonfile, change the “main” part tosrc/index.js.Now manually add another entry into the
package.jsonfile calledbinand set its key tocalcand 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
--verboseor--format json. -
Automatic help → Generates
--helpoutput for free. -
Versioning → Add
--versioneasily.
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
As you noticed in the above examples, there are multiple parts in the single commands, let’s break it down.
calcis the main command (the tool itself).add,multiplyrepresents sub-command (an action insidecalc).5,7represents the arguments (values you pass)—verboseis 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

