Merge pull request #456 from monkeym4ster/master
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Add chinese Readme file
authored by Zhiye Li · committed by Zhiye Li · parents b2aad7a, 89e77c2 · Browse files · 1 file·+343 added−0 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+343 added−0 removed
- other:+343 added−0 removed
Provenance
- Commit
e741c18- Tree
c8c23b5- Author
Zhiye Li <zhiyelee@gmail.com>- Committer
Zhiye Li <zhiyelee@gmail.com>- Parent 1
b2aad7a- Parent 2
89e77c2
1 file changed+343 added−0 removed
added:Readme_zh-CN.md+343 added
1+# Commander.js2+3+4+[](http://travis-ci.org/tj/commander.js)5+[](https://www.npmjs.org/package/commander)6+[](https://www.npmjs.org/package/commander)7+[](https://gitter.im/tj/commander.js?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)8+9+ [node.js](http://nodejs.org) 命令行接口的完整解决方案,灵感来自 Ruby 的 [commander](https://github.com/tj/commander)。 10+ [API 文档](http://tj.github.com/commander.js/)11+12+13+## 安装14+15+ $ npm install commander16+17+## 参数解析18+ 定义并使用 commander 的选项功能 `.option()` 方法。作为这些选项的文档,下面的例子会解析来自 `progress.argv` 指定的参数和选项,留下剩余未被选择的参数放到 `program.args` 数组中。19+20+```js21+#!/usr/bin/env node22+23+/**24+ * Module dependencies.25+ */26+27+var program = require('commander');28+29+program30+ .version('0.0.1')31+ .option('-p, --peppers', 'Add peppers')32+ .option('-P, --pineapple', 'Add pineapple')33+ .option('-b, --bbq-sauce', 'Add bbq sauce')34+ .option('-c, --cheese [type]', 'Add the specified type of cheese [marble]', 'marble')35+ .parse(process.argv);36+37+console.log('you ordered a pizza with:');38+if (program.peppers) console.log(' - peppers');39+if (program.pineapple) console.log(' - pineapple');40+if (program.bbqSauce) console.log(' - bbq');41+console.log(' - %s cheese', program.cheese);42+```43+44+ 短标志可以作为单独的参数传递。像 `-abc` 等于 `-a -b -c`。多词组成的选项,像“--template-engine”会变成 `program.templateEngine` 等。45+46+47+## 强制多态48+49+```js50+function range(val) {51+ return val.split('..').map(Number);52+}53+54+function list(val) {55+ return val.split(',');56+}57+58+function collect(val, memo) {59+ memo.push(val);60+ return memo;61+}62+63+function increaseVerbosity(v, total) {64+ return total + 1;65+}66+67+program68+ .version('0.0.1')69+ .usage('[options] <file ...>')70+ .option('-i, --integer <n>', 'An integer argument', parseInt)71+ .option('-f, --float <n>', 'A float argument', parseFloat)72+ .option('-r, --range <a>..<b>', 'A range', range)73+ .option('-l, --list <items>', 'A list', list)74+ .option('-o, --optional [value]', 'An optional value')75+ .option('-c, --collect [value]', 'A repeatable value', collect, [])76+ .option('-v, --verbose', 'A value that can be increased', increaseVerbosity, 0)77+ .parse(process.argv);78+79+console.log(' int: %j', program.integer);80+console.log(' float: %j', program.float);81+console.log(' optional: %j', program.optional);82+program.range = program.range || [];83+console.log(' range: %j..%j', program.range[0], program.range[1]);84+console.log(' list: %j', program.list);85+console.log(' collect: %j', program.collect);86+console.log(' verbosity: %j', program.verbose);87+console.log(' args: %j', program.args);88+```89+90+## 正则表达式91+```js92+program93+ .version('0.0.1')94+ .option('-s --size <size>', 'Pizza size', /^(large|medium|small)$/i, 'medium')95+ .option('-d --drink [drink]', 'Drink', /^(coke|pepsi|izze)$/i)96+ .parse(process.argv);97+ 98+console.log(' size: %j', program.size);99+console.log(' drink: %j', program.drink);100+```101+102+## 可变参数103+104+ 一个命令的最后一个参数可以是可变参数, 并且只能是最后一个参数。为了使参数可变,你需要在参数名后面追加 `...`。 下面是个示例:105+106+```js107+#!/usr/bin/env node108+109+/**110+ * Module dependencies.111+ */112+113+var program = require('commander');114+115+program116+ .version('0.0.1')117+ .command('rmdir <dir> [otherDirs...]')118+ .action(function (dir, otherDirs) {119+ console.log('rmdir %s', dir);120+ if (otherDirs) {121+ otherDirs.forEach(function (oDir) {122+ console.log('rmdir %s', oDir);123+ });124+ }125+ });126+127+program.parse(process.argv);128+```129+130+ `数组` 是可以用于给可变参数传值的。 这适用于 `program.args` 以及参数传递,以你的行动证明上述。131+ 你可以如上所示的去尝试。132+133+## 指定参数的语法134+135+```js136+#!/usr/bin/env node137+138+var program = require('../');139+140+program141+ .version('0.0.1')142+ .arguments('<cmd> [env]')143+ .action(function (cmd, env) {144+ cmdValue = cmd;145+ envValue = env;146+ });147+148+program.parse(process.argv);149+150+if (typeof cmdValue === 'undefined') {151+ console.error('no command given!');152+ process.exit(1);153+}154+console.log('command:', cmdValue);155+console.log('environment:', envValue || "no environment given");156+```157+158+## Git 风格的子命令159+160+```js161+// file: ./examples/pm162+var program = require('..');163+164+program165+ .version('0.0.1')166+ .command('install [name]', 'install one or more packages')167+ .command('search [query]', 'search with optional query')168+ .command('list', 'list packages installed', {isDefault: true})169+ .parse(process.argv);170+```171+172+ 当说明参数调用 `.command()` 时,没有 `.action(callback)` 应调用来处理子命令,否则会出错。这告诉 commander,你要使用单独的可执行文件的子命令,就像 `git(1)` 和其他流行工具一样。 173+ Commander 将尝试在入口脚本的目录中搜索可执行文件,(像 `./examples/pm`)与名称 `program-command`,像 `pm-install`,`pm-search`。174+175+ 对 `.command()` 的调用,可以传递选项。指定 `opts.noHelp` 为 `true` 将从生成的帮助输出中删除选项。如果没有其他子命令指定,指定 `opts.isDefault` 为 `true` 将运行子命令。176+177+ 如果该计划旨在全球范围内安装,请确保可执行文件有适当的模式,如 '755'。178+179+### `--harmony`180+181+您可以启用 `--harmoney` 选项在两个方面:182+* 用 `#!/usr/bin/env node --harmony` 在子命令脚本中。注意一些系统版本不支持此模式。183+* 用 `--harmoney` 选项时调用的命令,像 `node --harmony examples/pm publish`。`--harmoney` 选项当产生一个子命令进程时保留选项。184+185+## 自动化帮助信息 --help186+187+ 帮助信息是 commander 基于你的程序自动生成的,下面是 `--help` 生成的帮助信息:188+189+``` 190+ $ ./examples/pizza --help191+192+ Usage: pizza [options]193+194+ An application for pizzas ordering195+196+ Options:197+198+ -h, --help output usage information199+ -V, --version output the version number200+ -p, --peppers Add peppers201+ -P, --pineapple Add pineapple202+ -b, --bbq Add bbq sauce203+ -c, --cheese <type> Add the specified type of cheese [marble]204+ -C, --no-cheese You do not want any cheese205+206+```207+208+## 自定义帮助209+210+ 你可以显示任何 `-h, --help` 信息,通过监听 `--help` 。一旦你完成了 Commander 将自动退出,你的程序的其余部分不会展示。例如在下面的 “stuff” 将不会在执行 `--help` 时输出。211+212+```js213+#!/usr/bin/env node214+215+/**216+ * Module dependencies.217+ */218+219+var program = require('commander');220+221+program222+ .version('0.0.1')223+ .option('-f, --foo', 'enable some foo')224+ .option('-b, --bar', 'enable some bar')225+ .option('-B, --baz', 'enable some baz');226+227+// must be before .parse() since228+// node's emit() is immediate229+230+program.on('--help', function(){231+ console.log(' Examples:');232+ console.log('');233+ console.log(' $ custom-help --help');234+ console.log(' $ custom-help -h');235+ console.log('');236+});237+238+program.parse(process.argv);239+240+console.log('stuff');241+```242+243+下列帮助信息是运行 `node script-name.js -h` or `node script-name.js --help` 时输出的:244+245+```246+247+Usage: custom-help [options]248+249+Options:250+251+ -h, --help output usage information252+ -V, --version output the version number253+ -f, --foo enable some foo254+ -b, --bar enable some bar255+ -B, --baz enable some baz256+257+Examples:258+259+ $ custom-help --help260+ $ custom-help -h261+262+```263+264+## .outputHelp(cb)265+266+不退出输出帮助信息。267+可选的回调可在显示帮助文本后处理。268+如果你想显示默认的帮助(例如,如果没有提供命令),你可以使用类似的东西:269+270+```js271+var program = require('commander');272+var colors = require('colors');273+274+program275+ .version('0.0.1')276+ .command('getstream [url]', 'get stream URL')277+ .parse(process.argv);278+279+ if (!process.argv.slice(2).length) {280+ program.outputHelp(make_red);281+ }282+283+function make_red(txt) {284+ return colors.red(txt); // 在控制台上显示红色的帮助文本285+}286+```287+288+## .help(cb)289+290+ 输出帮助信息并立即退出。291+ 可选的回调可在显示帮助文本后处理。292+293+## 例子294+295+```js296+var program = require('commander');297+298+program299+ .version('0.0.1')300+ .option('-C, --chdir <path>', 'change the working directory')301+ .option('-c, --config <path>', 'set config path. defaults to ./deploy.conf')302+ .option('-T, --no-tests', 'ignore test hook')303+304+program305+ .command('setup [env]')306+ .description('run setup commands for all envs')307+ .option("-s, --setup_mode [mode]", "Which setup mode to use")308+ .action(function(env, options){309+ var mode = options.setup_mode || "normal";310+ env = env || 'all';311+ console.log('setup for %s env(s) with %s mode', env, mode);312+ });313+314+program315+ .command('exec <cmd>')316+ .alias('ex')317+ .description('execute the given remote cmd')318+ .option("-e, --exec_mode <mode>", "Which exec mode to use")319+ .action(function(cmd, options){320+ console.log('exec "%s" using %s mode', cmd, options.exec_mode);321+ }).on('--help', function() {322+ console.log(' Examples:');323+ console.log();324+ console.log(' $ deploy exec sequential');325+ console.log(' $ deploy exec async');326+ console.log();327+ });328+329+program330+ .command('*')331+ .action(function(env){332+ console.log('deploying "%s"', env);333+ });334+335+program.parse(process.argv);336+```337+338+ 更多的 [演示](https://github.com/tj/commander.js/tree/master/examples) 可以在这里找到.339+340+## 许可证341+342+MIT343+