CLI frameworks for TypeScript: building powerful console applications
Modern development in TypeScript is unthinkable without creating command-line tools (CLI). Whether it's a project builder, code generator, or automation utility — every developer has at least once thought about writing their own console program. In this article, we will thoroughly review the best CLI frameworks for TypeScript, their features, and provide practical code examples.
Why do you need a CLI framework?
Writing a CLI "from scratch" involves not only parsing command-line arguments but also managing subcommands, input validation, colored output, and interactive prompts. A framework handles the routine, allowing you to focus on business logic. TypeScript adds strict typing, which is especially valuable when developing complex console applications.
Top 4 CLI frameworks for TypeScript
1. Commander.js
The most popular framework (over 30 million downloads per week). Ideal for creating complex multi-level CLIs with subcommands, options, and automatic help generation.
Installation:
npm install commanderBasic application example:
import { Command } from 'commander';
const program = new Command();
program .name('greet') .description('CLI for greetings') .version('1.0.0');
program .command('hello') .argument('<name>', 'user name') .option('-l, --lang <lang>', 'greeting language', 'ru') .action((name, options) => { const greetings: Record<string, string> = { ru: 'Привет', en: 'Hello', de: 'Hallo' }; const greeting = greetings[options.lang] || greetings.ru; console.log(`${greeting}, ${name}!`); });
program.parse();Running:
npx ts-node index.ts hello Ivan --lang en# Output: Hello, Ivan!2. Inquirer.js
A library for creating interactive prompts in the terminal. It allows asking questions, selecting options, entering passwords, and much more. Works great in combination with Commander.
Installation:
npm install inquirer @types/inquirerInteractive survey example:
import inquirer from 'inquirer';
interface Answers { name: string; role: string; experience: number;}
async function main() { const answers: Answers = await inquirer.prompt([ { type: 'input', name: 'name', message: 'What is your name?', validate: (input: string) => input.length > 0 || 'Name cannot be empty' }, { type: 'list', name: 'role', message: 'What is your role?', choices: ['Frontend', 'Backend', 'Fullstack', 'DevOps'] }, { type: 'number', name: 'experience', message: 'Years of experience?', default: 1 } ]);
console.log(`✅ ${answers.name}, you are a ${answers.role} with ${answers.experience} years of experience.`);}
main();3. Clack
A modern framework from the creators of Astro. It features a minimalistic API, beautiful animations, and built-in TypeScript support. Ideal for rapid prototyping.
Installation:
npm install @clack/promptsColor selection example:
import * as clack from '@clack/prompts';
async function main() { clack.intro('Color Selection');
const color = await clack.select({ message: 'Choose your favorite color:', options: [ { value: 'red', label: 'Red' }, { value: 'green', label: 'Green' }, { value: 'blue', label: 'Blue' } ] });
clack.note(`You selected: ${color}`); clack.outro('Thank you for choosing!');}
main();4. Yargs
A powerful argument parser
Also in library
Task Queues in C# (.NET): System.Threading.Channels, Hangfire, and RabbitMQ
Carter for C# (.NET): Complete Guide with Examples
JWT Tokens in Java: Complete Guide to Working with JSON Web Tokens
PDF Generation in Kotlin: Libraries and Tools (iText, Apache PDFBox)