commander
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCommander.js
Commander.js
Overview
概述
Commander is the standard library for building Node.js CLIs. Parse arguments, define subcommands, generate help text, and handle options. Powers thousands of CLIs including create-react-app, eslint, and prisma.
Commander是用于构建Node.js CLI的标准库,可用于解析参数、定义子命令、生成帮助文本以及处理选项。它为数千个CLI工具提供支持,包括create-react-app、eslint和prisma。
Instructions
使用说明
Step 1: Basic CLI
步骤1:基础CLI
typescript
// cli.ts — CLI with commands and options
import { Command } from 'commander'
const program = new Command()
.name('mytools')
.description('Developer productivity toolkit')
.version('1.0.0')
program
.command('init')
.description('Initialize a new project')
.argument('<name>', 'project name')
.option('-t, --template <type>', 'project template', 'default')
.option('--no-git', 'skip git initialization')
.option('-d, --dry-run', 'show what would be created')
.action(async (name, opts) => {
console.log(`Creating project: ${name}`)
console.log(`Template: ${opts.template}`)
if (opts.dryRun) { console.log('(dry run)'); return }
await createProject(name, opts)
})
program
.command('deploy')
.description('Deploy to production')
.option('-e, --env <environment>', 'target environment', 'production')
.option('--force', 'skip confirmation')
.action(async (opts) => {
if (!opts.force) {
const ok = await confirm(`Deploy to ${opts.env}?`)
if (!ok) process.exit(0)
}
await deploy(opts.env)
})
program.parse()typescript
// cli.ts — CLI with commands and options
import { Command } from 'commander'
const program = new Command()
.name('mytools')
.description('Developer productivity toolkit')
.version('1.0.0')
program
.command('init')
.description('Initialize a new project')
.argument('<name>', 'project name')
.option('-t, --template <type>', 'project template', 'default')
.option('--no-git', 'skip git initialization')
.option('-d, --dry-run', 'show what would be created')
.action(async (name, opts) => {
console.log(`Creating project: ${name}`)
console.log(`Template: ${opts.template}`)
if (opts.dryRun) { console.log('(dry run)'); return }
await createProject(name, opts)
})
program
.command('deploy')
.description('Deploy to production')
.option('-e, --env <environment>', 'target environment', 'production')
.option('--force', 'skip confirmation')
.action(async (opts) => {
if (!opts.force) {
const ok = await confirm(`Deploy to ${opts.env}?`)
if (!ok) process.exit(0)
}
await deploy(opts.env)
})
program.parse()Step 2: Package Setup
步骤2:包配置
json
{
"name": "mytools",
"bin": { "mytools": "./dist/cli.js" },
"scripts": {
"build": "tsc",
"dev": "tsx src/cli.ts"
}
}bash
undefinedjson
{
"name": "mytools",
"bin": { "mytools": "./dist/cli.js" },
"scripts": {
"build": "tsc",
"dev": "tsx src/cli.ts"
}
}bash
undefinedDevelopment
开发阶段
npx tsx src/cli.ts init my-project --template react
npx tsx src/cli.ts init my-project --template react
After build + npm link
构建完成 + 执行npm link后
mytools init my-project --template react
mytools deploy --env staging
mytools --help
undefinedmytools init my-project --template react
mytools deploy --env staging
mytools --help
undefinedGuidelines
使用指南
- Commander auto-generates from your command definitions.
--help - Use for required positional args,
argument()for flags.option() - flags automatically create boolean negations (e.g.,
--no-*→--no-git).opts.git === false - Exit codes: 0 for success, 1 for errors. Commander handles parse errors automatically.
- For interactive prompts, pair with Inquirer or @clack/prompts.
- Commander会根据你定义的命令自动生成帮助文本。
--help - 必选位置参数使用定义,标志位使用
argument()定义。option() - 格式的标志会自动创建布尔类型的否定值(例如
--no-*→--no-git)。opts.git === false - 退出码:0代表成功,1代表出错。Commander会自动处理解析错误。
- 如果需要交互式提示,可以搭配Inquirer或者@clack/prompts使用。