Skip to content
Myr

add requiredOption and exitOverride for Chinese Readme

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by oGsLP · committed by John Gee · parent 2ef5401 · Browse files · 1 file+39 added−1 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+39 added−1 removed

  • other:+39 added−1 removed

1 file+39 added−1 removed

modified:Readme_zh-CN.md+39 added-1 removed
236 unmodified lines
237$ custom --list x,y,z238[ 'x', 'y', 'z' ]239```240241### 必需的选项242243你可以使用`.requiredOption`指定一个必需的(强制性的)选项,这样的选项在命令行的命令中必须被给出,除非它拥有一个默认的值。另外,这个方法在格式上和使用`.option`一样:The method is otherwise the same as `.option` in format, 采用标志和说明,以及可选的默认值或自定义处理。244245```js246const program = require('commander');247248program249 .requiredOption('-c, --cheese <type>', 'pizza must have cheese');250251program.parse(process.argv);252```253254```255$ pizza256error: required option '-c, --cheese <type>' not specified257```258259### 版本选项260261`version`方法会处理显示版本命令,默认选项标识为`-V`和`--version`,当存在时会打印版本号并退出。262
299 unmodified lines
562563如果你在使用 node inspector的 `node -inspect` 等命令来[调试](https://nodejs.org/en/docs/guides/debugging-getting-started/) git风格的可执行命令,564对于生成的子命令,inspector端口递增1。565566### 重载退出(exit)处理567568默认情况下,当检测到错误以及打印出帮助信息或版本信息时Commander将调用`process.exit`方法。你可以重写覆盖这项操作并提供一个可选的回调。默认的实现会抛出一个`CommanderError`。569570重载的回调接受一个包含了 Number类型的`exitCode`、String类型的`code`和`message` 属性的`CommanderError`。除了对可执行子命令完成的异步处理之外,默认的实现方法会抛出这个错。正常情况下,打印错误信息以及帮助或版本信息不会被重载所影响,因为重载的调用在打印之后。571572``` js573program.exitOverride();574575try {576 program.parse(process.argv);577} catch (err) {578 // 自定义处理...579}580```581582## 例子583584```js585const program = require('commander');
37 unmodified lines
623624program.parse(process.argv);625```626593 更多的 [演示](https://github.com/tj/commander.js/tree/master/examples) 可以在这里找到。627更多的 [演示](https://github.com/tj/commander.js/tree/master/examples) 可以在这里找到。628629## 许可证630631[MIT](https://github.com/tj/commander.js/blob/master/LICENSE)632633## 支持634635Commander 现在在Node 8以及更高的版本上得到支持。(尽管Commander仍有可能在更低的Node版本上工作,但是Node 8以下版本不再保证相关的测试)636637主要的社区支持的免费论坛就在Github上的项目[Issues](https://github.com/tj/commander.js/issues)638639[专业支持的commander现在已经可用!](https://tidelift.com/subscription/pkg/npm-commander?utm_source=npm-commander&utm_medium=referral&utm_campaign=readme)640641Tidelift为软件开发团队提供了购买和维护其软件的单一来源,并由最了解软件的专家提供专业级保证,同时与现有工具无缝集成。