TypeScript के साथ CLI Tool
In this page:
program.option("-p, --port <number>", "description", Number); // commander
const options = program.opts<{ port: number }>();
Core Concept
एक typed CLI tool को TypeScript से फायदा होता है अपने argument parsing और command structure को type-safe बनाकर, इसलिए एक option name में typo या एक गलत flag type tool ship होने से पहले catch होता है, जब एक user इसे hit करे तब नहीं।
उदाहरण: Core Concept
interface Options {
port: number;
verbose: boolean;
}
function parseArgs(args: string[]): Options {
return { port: 3000, verbose: args.includes("--verbose") };
}
console.log(parseArgs(["--verbose"]));
Basic Setup
एक basic setup commander या yargs जैसी library उपयोग करता है (दोनों TypeScript types ship करते हैं) commands और options define करने के लिए, .ts source को tsc के through एक runnable Node script में compile करते हुए।
उदाहरण: Basic Setup
// npm install commander
// import { program } from "commander";
console.log("commander/yargs ship TypeScript types for CLI parsing");
Typed Example
एक typed example: program.option('-p, --port <number>', 'port number', Number) yargs के inferred Arguments type के साथ combine होकर इसका मतलब है कि argv.port हर जगह number के रूप में जाना जाता है जहां यह उपयोग होता है, एक raw string के बजाय जिसे manually parse करने की ज़रूरत हो।
उदाहरण: Typed Example
// program.option('-p, --port <number>', 'port number', Number);
interface Argv {
port: number;
}
const argv: Argv = { port: 8080 };
console.log(argv.port); // known to be a number, not a raw string
Project Usage
एक असली project में, एक CLI के typed command handlers समय के साथ ज़्यादा commands और flags जुड़ने पर maintainable रहते हैं, क्योंकि compiler एक ऐसे handler को catch करता है जो newly-added required option account करना भूल गया हो।
उदाहरण: Project Usage
interface Command {
name: string;
run: (args: string[]) => void;
}
const commands: Command[] = [
{ name: "build", run: () => console.log("Building...") },
];
commands[0].run([]);
Best Practices
अपने compiled entry file की ओर point करता एक bin field package.json में जोड़ें (एक #!/usr/bin/env node shebang के साथ) इसलिए tool globally install हो सके और built होने के बाद एक असली command के रूप में run हो सके।
उदाहरण: Best Practices
// package.json: { "bin": { "mytool": "./dist/cli.js" } }
// dist/cli.js starts with: #!/usr/bin/env node
console.log("bin field + shebang lets the tool run as a global command");
process.argvvalues पढ़ना और उन्हें numbers मानना, जब वे हमेशा strings होती हैं और उन्हेंNumber(...)चाहिए।#!/usr/bin/env nodeline औरpackage.jsonमेंbinfield भूल जाना, इसलिए command runnable नहीं है।- Compiled
.jsoutput के बजाय.tsfile को सीधे चलाना।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Todo App with TypeScript
- REST API with Express + TypeScript
- React Dashboard with TypeScript
- CLI Tool with TypeScript
- Library with TypeScript
- Full Stack TypeScript App
- TypeScript Design System
- TypeScript Monorepo Project
- Authentication System
- Real-time App with Socket.io
- GraphQL API with TypeScript
- Microservices with TypeScript
- TypeScript Best Practices Review
- What to Learn Next