← Back to TypeScript Course | Chapter 28: Real World Projects | Lesson 4 of 14

TypeScript के साथ CLI Tool

TypeScript Node.js पर चलने वाले command-line tools बनाने के लिए उपयोग किया जा सकता है। Typed argument parsing और service functions CLI code को maintain करना आसान बनाते हैं।
Syntax
typescript
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

typescript
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

typescript
// 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

typescript
// 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

typescript
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

typescript
// 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");
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. process.argv values पढ़ना और उन्हें numbers मानना, जब वे हमेशा strings होती हैं और उन्हें Number(...) चाहिए।
  2. #!/usr/bin/env node line और package.json में bin field भूल जाना, इसलिए command runnable नहीं है।
  3. Compiled .js output के बजाय .ts file को सीधे चलाना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.