with one click
commander
Commander.js v14 CLI 框架最佳實踐。當需要建立 CLI 工具、解析命令列參數、設計 subcommands 時使用。
Install with Codex or Claude Copy this prompt, paste it into Codex, Claude, or another assistant, and let it review the skill page and install it for you.
Menu
Commander.js v14 CLI 框架最佳實踐。當需要建立 CLI 工具、解析命令列參數、設計 subcommands 時使用。
Install with Codex or Claude Copy this prompt, paste it into Codex, Claude, or another assistant, and let it review the skill page and install it for you.
Based on SOC occupation classification
Zod v4 schema validation 最佳實踐指南。當需要定義 schema、驗證/解析 JSON 資料、type inference、或處理 unknown data 時使用。
Svelte 5 + Astro 整合最佳實踐指南。當需要建立 Svelte 元件、使用 runes API、整合 Astro islands、或用 Testing Library 測試 Svelte 元件時使用。
GitHub GraphQL API 最佳實踐指南。當需要使用 GraphQL 查詢使用者資料、處理 cursor pagination、計算 rate limit、或除錯 GraphQL errors 時使用。
gayanvoice/top-github-users 架構參考指南。當需要了解 GitHub 使用者排行榜的資料抓取管線、國家設定、排行計算邏輯、已知問題、或社群需求時使用。
GitHub Actions CI/CD 最佳實踐指南。當需要設定 workflow、cron 排程、GitHub Pages 部署、使用 Octokit API、或處理 rate limiting 時使用。
Globe.GL 3D 地球視覺化指南。當需要建立互動式 3D 地球、國家熱力圖、點擊導航、或整合 Astro 時使用。
| name | commander |
| description | Commander.js v14 CLI 框架最佳實踐。當需要建立 CLI 工具、解析命令列參數、設計 subcommands 時使用。 |
import { Command } from "commander";
const program = new Command()
.name("my-cli")
.description("CLI description")
.version("1.0.0");
program
.option("-d, --debug", "debug mode") // boolean
.option("-p, --port <number>", "port", parseInt) // required value + parser
.option("-c, --config [path]", "config file") // optional value
.requiredOption("-u, --user <name>", "user"); // mandatory
program
.command("serve")
.description("start server")
.argument("[root]", "project root", ".")
.option("-p, --port <number>", "port", parseInt)
.action(async (root, options) => {
console.log(`Serving ${root} on ${options.port}`);
});
await program.parseAsync();
function addSharedOptions(cmd: Command) {
return cmd
.option("-c, --country <code>", "country code")
.option("-l, --limit <number>", "limit", parseInt);
}
addSharedOptions(program.command("collect")).action(async (opts) => { ... });
addSharedOptions(program.command("generate")).action(async (opts) => { ... });
用 parseAsync() 取代 parse():
program.command("fetch").action(async () => {
await fetchData();
});
await program.parseAsync();
import { InvalidArgumentError } from "commander";
function myParseInt(value: string): number {
const n = parseInt(value, 10);
if (isNaN(n)) throw new InvalidArgumentError("Not a number");
return n;
}
program
.hook("preAction", (_, cmd) => console.log(`Running: ${cmd.name()}`))
.hook("postAction", (_, cmd) => console.log(`Done: ${cmd.name()}`));
安裝 @commander-js/extra-typings 可自動推導 opts() 型別。
| 模式 | 做法 |
|---|---|
| 顯示 help on error | program.showHelpAfterError() |
| Exit override | program.exitOverride() + try/catch |
| Choices | new Option("--size <s>").choices(["s","m","l"]) |
| Env fallback | new Option("--port <n>").env("PORT") |