Add TOC and some lint
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by John Gee · committed by John Gee · parent dab6ba8 · Browse files · 1 file·+63 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+63 added−18 removed
- other:+63 added−18 removed
Provenance
- Commit
b19b58e- Tree
cf65335- Author
John Gee <john@ruru.gen.nz>- Committer
John Gee <john@ruru.gen.nz>- Parent
dab6ba8
1 file changed+63 added−18 removed
modified:Readme_zh-CN.md+63 added-18 removed
5 unmodified lines6 [](https://packagephobia.now.sh/result?p=commander)7 8 [node.js](http://nodejs.org) 命令行接口的完整解决方案,灵感来自 Ruby 的 [commander](https://github.com/commander-rb/commander)。9 10+- [Commander.js](#commanderjs)11+ - [安装](#%e5%ae%89%e8%a3%85)12+ - [声明program变量](#%e5%a3%b0%e6%98%8eprogram%e5%8f%98%e9%87%8f)13+ - [选项](#%e9%80%89%e9%a1%b9)14+ - [常用选项类型,boolean和值](#%e5%b8%b8%e7%94%a8%e9%80%89%e9%a1%b9%e7%b1%bb%e5%9e%8bboolean%e5%92%8c%e5%80%bc)15+ - [默认选项值](#%e9%bb%98%e8%ae%a4%e9%80%89%e9%a1%b9%e5%80%bc)16+ - [其他选项类型,可忽略的布尔值和标志值](#%e5%85%b6%e4%bb%96%e9%80%89%e9%a1%b9%e7%b1%bb%e5%9e%8b%e5%8f%af%e5%bf%bd%e7%95%a5%e7%9a%84%e5%b8%83%e5%b0%94%e5%80%bc%e5%92%8c%e6%a0%87%e5%bf%97%e5%80%bc)17+ - [自定义选项处理](#%e8%87%aa%e5%ae%9a%e4%b9%89%e9%80%89%e9%a1%b9%e5%a4%84%e7%90%86)18+ - [版本选项](#%e7%89%88%e6%9c%ac%e9%80%89%e9%a1%b9)19+ - [Commands](#commands)20+ - [指定参数语法](#%e6%8c%87%e5%ae%9a%e5%8f%82%e6%95%b0%e8%af%ad%e6%b3%95)21+ - [操作处理程序(Action handler) (子)命令](#%e6%93%8d%e4%bd%9c%e5%a4%84%e7%90%86%e7%a8%8b%e5%ba%8faction-handler-%e5%ad%90%e5%91%bd%e4%bb%a4)22+ - [Git 风格的子命令](#git-%e9%a3%8e%e6%a0%bc%e7%9a%84%e5%ad%90%e5%91%bd%e4%bb%a4)23+ - [自动化帮助信息 --help](#%e8%87%aa%e5%8a%a8%e5%8c%96%e5%b8%ae%e5%8a%a9%e4%bf%a1%e6%81%af---help)24+ - [自定义帮助](#%e8%87%aa%e5%ae%9a%e4%b9%89%e5%b8%ae%e5%8a%a9)25+ - [.usage and .name](#usage-and-name)26+ - [.outputHelp(cb)](#outputhelpcb)27+ - [.helpOption(flags, description)](#helpoptionflags-description)28+ - [.help(cb)](#helpcb)29+ - [自定义事件监听](#%e8%87%aa%e5%ae%9a%e4%b9%89%e4%ba%8b%e4%bb%b6%e7%9b%91%e5%90%ac)30+ - [零碎知识](#%e9%9b%b6%e7%a2%8e%e7%9f%a5%e8%af%86)31+ - [TypeScript](#typescript)32+ - [Node 选项例如 `--harmony`](#node-%e9%80%89%e9%a1%b9%e4%be%8b%e5%a6%82---harmony)33+ - [Node 调试](#node-%e8%b0%83%e8%af%95)34+ - [例子](#%e4%be%8b%e5%ad%90)35+ - [许可证](#%e8%ae%b8%e5%8f%af%e8%af%81)36+ - [支持](#%e6%94%af%e6%8c%81)37+38 ## 安装39 40+```bash12- $ npm install commander41+npm install commander42+```43 44 ## 声明program变量45 46 Commander为了方便快速编程导出了一个全局对象。为简洁起见,本README中的示例中使用了它。97 unmodified lines144 const cheeseStr = (program.cheese === false) ? 'no cheese' : `${program.cheese} cheese`;145 console.log(`You ordered a pizza with ${sauceStr} and ${cheeseStr}`);146 ```147 118-```119-$ pizza-options 148+```bash149+$ pizza-options150 You ordered a pizza with sauce and mozzarella cheese151 $ pizza-options --sauce152 error: unknown option '--sauce'153 $ pizza-options --cheese=blue25 unmodified lines179 $ pizza-options --cheese mozzarella180 add cheese type mozzarella181 ```182 153-## 自定义选项处理183+### 自定义选项处理184 185 你可以指定一个函数来处理选项的值,接收两个参数:用户传入的值、上一个值(previous value),它会返回新的选项值。186 187 你可以将选项值强制转换为所需类型,或累积值,或完全自定义处理。189 unmodified lines377 使用 `executableFile` 指定一个名字会覆盖默认构造的名称378 379 如果你打算全局安装该命令,请确保可执行文件有对应的权限,例如 `755`。380 351-381 ## 自动化帮助信息 --help382 383 帮助信息是 commander 基于你的程序自动生成的,下面是 `--help` 生成的帮助信息:384 12 unmodified lines397 -C, --no-cheese You do not want any cheese398 -h, --help output usage information399 ```400 372-## 自定义帮助401+### 自定义帮助402 403 你可以通过监听 `--help` 来控制 `-h, --help` 显示任何信息。一旦调用完成, Commander 将自动退出,你的程序的其余部分不会展示。例如在下面的 “stuff” 将不会在执行 `--help` 时输出。404 405 ```js406 #!/usr/bin/env node407 379-/**380- * Module dependencies.381- */382-408 var program = require('commander');409 410 program411 .version('0.0.1')17 unmodified lines429 ```430 431 下列帮助信息是运行 `node script-name.js -h` or `node script-name.js --help` 时输出的:432 408-```433+```Text434 Usage: custom-help [options]435 436 Options:437 -h, --help output usage information6 unmodified lines444 $ custom-help --help445 $ custom-help -h446 ```447 423-## .outputHelp(cb)448+### .usage and .name449 450+These allow you to customise the usage description in the first line of the help. The name is otherwise451+deduced from the (full) program arguments. Given:452+453+```js454+program455+ .name("my-command")456+ .usage("[global options] command")457+```458+459+The help will start with:460+461+```Text462+Usage: my-command [global options] command463+```464+465+### .outputHelp(cb)466+467 输出帮助信息的同时不退出。468 可选的回调可在显示帮助文本后处理。469 如果你想显示默认的帮助(例如,如果没有提供命令),你可以使用类似的东西:470 13 unmodified lines484 function make_red(txt) {485 return colors.red(txt); //display the help text in red on the console486 }487 ```446-## .helpOption(flags, description)488 489+### .helpOption(flags, description)490+491 重写覆盖默认的帮助标识和描述492 493 ```js494 program495 .helpOption('-e, --HELP', 'read more information');496 ```497 455-## .help(cb)498+### .help(cb)499 500 输出帮助信息并立即退出。501 可选的回调可在显示帮助文本后处理。502 29 unmodified lines532 533 ```bash534 node -r ts-node/register pm.ts535 ```536+537 ### Node 选项例如 `--harmony`538 539 你可以采用两种方式启用 `--harmony`:496-* 在子命令脚本中加上 `#!/usr/bin/env node --harmony`。注意一些系统版本不支持此模式。497-* 在指令调用时加上 `--harmony` 参数,例如 `node --harmony examples/pm publish`。`--harmony` 选项在开启子进程时会被保留。540 541+- 在子命令脚本中加上 `#!/usr/bin/env node --harmony`。注意一些系统版本不支持此模式。542+- 在指令调用时加上 `--harmony` 参数,例如 `node --harmony examples/pm publish`。`--harmony` 选项在开启子进程时会被保留。543+544 ### Node 调试545 546 如果你在使用 node inspector的 `node -inspect` 等命令来[调试](https://nodejs.org/en/docs/guides/debugging-getting-started/) git风格的可执行命令,547 对于生成的子命令,inspector端口递增1。548 504- ## 例子549+## 例子550 551 ```js552 var program = require('commander');553 46 unmodified lines600 ## 支持601 602 [专业支持的commander现在已经可用!](https://tidelift.com/subscription/pkg/npm-commander?utm_source=npm-commander&utm_medium=referral&utm_campaign=readme)603 559-Tidelift为软件开发团队提供了购买和维护其软件的单一来源,并由最了解软件的专家提供专业级保证,同时与现有工具无缝集成。604+Tidelift为软件开发团队提供了购买和维护其软件的单一来源,并由最了解软件的专家提供专业级保证,同时与现有工具无缝集成。