Skip to content
Myr

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

1 file+63 added−18 removed

modified:Readme_zh-CN.md+63 added-18 removed
5 unmodified lines
6[![Install Size](https://packagephobia.now.sh/badge?p=commander)](https://packagephobia.now.sh/result?p=commander)78[node.js](http://nodejs.org) 命令行接口的完整解决方案,灵感来自 Ruby 的 [commander](https://github.com/commander-rb/commander)。910- [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)3738## 安装3940```bash12 $ npm install commander41npm install commander42```4344## 声明program变量4546Commander为了方便快速编程导出了一个全局对象。为简洁起见,本README中的示例中使用了它。
97 unmodified lines
144const cheeseStr = (program.cheese === false) ? 'no cheese' : `${program.cheese} cheese`;145console.log(`You ordered a pizza with ${sauceStr} and ${cheeseStr}`);146```147118```119$ pizza-options 148```bash149$ pizza-options150You ordered a pizza with sauce and mozzarella cheese151$ pizza-options --sauce152error: unknown option '--sauce'153$ pizza-options --cheese=blue
25 unmodified lines
179$ pizza-options --cheese mozzarella180add cheese type mozzarella181```182153## 自定义选项处理183### 自定义选项处理184185你可以指定一个函数来处理选项的值,接收两个参数:用户传入的值、上一个值(previous value),它会返回新的选项值。186187你可以将选项值强制转换为所需类型,或累积值,或完全自定义处理。
189 unmodified lines
377使用 `executableFile` 指定一个名字会覆盖默认构造的名称378379如果你打算全局安装该命令,请确保可执行文件有对应的权限,例如 `755`。380351381## 自动化帮助信息 --help382383 帮助信息是 commander 基于你的程序自动生成的,下面是 `--help` 生成的帮助信息:384
12 unmodified lines
397 -C, --no-cheese You do not want any cheese398 -h, --help output usage information399```400372## 自定义帮助401### 自定义帮助402403 你可以通过监听 `--help` 来控制 `-h, --help` 显示任何信息。一旦调用完成, Commander 将自动退出,你的程序的其余部分不会展示。例如在下面的 “stuff” 将不会在执行 `--help` 时输出。404405```js406#!/usr/bin/env node407379/**380 * Module dependencies.381 */382408var program = require('commander');409410program411 .version('0.0.1')
17 unmodified lines
429```430431下列帮助信息是运行 `node script-name.js -h` or `node script-name.js --help` 时输出的:432408```433```Text434Usage: custom-help [options]435436Options:437 -h, --help output usage information
6 unmodified lines
444 $ custom-help --help445 $ custom-help -h446```447423## .outputHelp(cb)448### .usage and .name449450These allow you to customise the usage description in the first line of the help. The name is otherwise451deduced from the (full) program arguments. Given:452453```js454program455 .name("my-command")456 .usage("[global options] command")457```458459The help will start with:460461```Text462Usage: my-command [global options] command463```464465### .outputHelp(cb)466467输出帮助信息的同时不退出。468可选的回调可在显示帮助文本后处理。469如果你想显示默认的帮助(例如,如果没有提供命令),你可以使用类似的东西:470
13 unmodified lines
484function make_red(txt) {485 return colors.red(txt); //display the help text in red on the console486}487```446## .helpOption(flags, description)488489### .helpOption(flags, description)490491 重写覆盖默认的帮助标识和描述492493```js494program495 .helpOption('-e, --HELP', 'read more information');496```497455## .help(cb)498### .help(cb)499500 输出帮助信息并立即退出。501 可选的回调可在显示帮助文本后处理。502
29 unmodified lines
532533```bash534node -r ts-node/register pm.ts535```536537### Node 选项例如 `--harmony`538539你可以采用两种方式启用 `--harmony`:496* 在子命令脚本中加上 `#!/usr/bin/env node --harmony`。注意一些系统版本不支持此模式。497* 在指令调用时加上 `--harmony` 参数,例如 `node --harmony examples/pm publish`。`--harmony` 选项在开启子进程时会被保留。540541- 在子命令脚本中加上 `#!/usr/bin/env node --harmony`。注意一些系统版本不支持此模式。542- 在指令调用时加上 `--harmony` 参数,例如 `node --harmony examples/pm publish`。`--harmony` 选项在开启子进程时会被保留。543544### Node 调试545546如果你在使用 node inspector的 `node -inspect` 等命令来[调试](https://nodejs.org/en/docs/guides/debugging-getting-started/) git风格的可执行命令,547对于生成的子命令,inspector端口递增1。548504 ## 例子549## 例子550551```js552var program = require('commander');553
46 unmodified lines
600## 支持601602[专业支持的commander现在已经可用!](https://tidelift.com/subscription/pkg/npm-commander?utm_source=npm-commander&utm_medium=referral&utm_campaign=readme)603559Tidelift为软件开发团队提供了购买和维护其软件的单一来源,并由最了解软件的专家提供专业级保证,同时与现有工具无缝集成。604Tidelift为软件开发团队提供了购买和维护其软件的单一来源,并由最了解软件的专家提供专业级保证,同时与现有工具无缝集成。