Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

CLI Tool Project – `commander` i | TypeScript Mastery lesson | BraveProgrammer
  1. Courses
  2. /
  3. TypeScript Mastery

Lesson 54 of 56

·Typescript

CLI Tool Project – `commander` in TypeScript

25 minutes

Course progress96%

Install commander

npm i commander
npm i -D @types/commander

Basic CLI

#!/usr/bin/env node
import { Command } from 'commander';

const program = new Command();

program
  .()
  .()
  .(, )
  .(, , )
  .(, , )
  .(, )
  .( {
     : ;
     (op) {
       : result = a + b; ;
       : result = a - b; ;
       : result = a * b; ;
       :
         (b === ) { .(); process.(); }
        result = a / b; ;
      :
        .(); process.();
    }
     (opts.) .();
    .(result);
  });

program.();
name
'calc'
description
'Simple arithmetic calculator'
option
'-v, --verbose'
'Enable verbose output'
argument
'<a>'
'first operand'
parseFloat
argument
'<b>'
'second operand'
parseFloat
argument
'<op>'
'operation: add|sub|mul|div'
action
(a: number, b: number, op: string, opts) =>
let
result
number
switch
case
'add'
break
case
'sub'
break
case
'mul'
break
case
'div'
if
0
console
error
'Division by zero'
exit
1
break
default
console
error
'Unknown operation'
exit
1
if
verbose
console
info
`Calculating ${a} ${op} ${b}`
console
log
parse

Make the file executable (chmod +x cli.ts) and add a bin entry in package.json:

"bin": { "calc": "dist/cli.js" }

Build with tsc and publish.

Info

Validate numeric arguments with a custom parseInt function to provide clearer error messages.
Make it stickPractice what you've learned1 quiz · 3 practical tests · Optional activities from TypeScript MasteryBrowse activities
Previous lessonWebSockets Typed with `ws`Continue to lesson 55Project – Todo REST API (Express + TypeORM)Back to TypeScript Mastery curriculum