Skip to content
Myr

Merge pull request #1026 from abetomo/unify_readme_code_examples_with_const

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

Unify readme code examples with 'const'

authored by Tomoaki Abe · committed by GitHub · parents 3b3d3b5, e9c24fe · Browse files · 2 files+18 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

2 files+18 added−18 removed

  • other:+18 added−18 removed

2 files+18 added−18 removed

modified:Readme_zh-CN.md+9 added-9 removed
284 unmodified lines
285286你可以通过使用 `.arguments` 来为最顶级命令指定参数,对于子命令来说参数都包括在 `.command` 调用之中了。尖括号(e.g. `<required>`)意味着必须的输入,而方括号(e.g. `[optional]`)则是代表了可选的输入287288```js289var program = require('commander');289const program = require('commander');290291program292 .version('0.1.0')293 .arguments('<cmd> [env]')
14 unmodified lines
308309一个命令有且仅有最后一个参数是可变的,你需要在参数名后加上 `...` 来使它可变,例如310311```js312var program = require('commander');312const program = require('commander');313314program315 .version('0.1.0')316 .command('rmdir <dir> [otherDirs...]')
16 unmodified lines
333你可以使用操作处理程序为一个命令增加选项options。334操作处理程序会接收每一个你声明的参数的变量,和一个额外的参数——这个命令对象自己。这个命令的参数包括添加的命令特定选项的值。335336```js337var program = require('commander');337const program = require('commander');338339program340 .command('rm <dir>')341 .option('-r, --recursive', 'Remove recursively')
18 unmodified lines
360你可以在可执行文件里处理可执行(子)命令的选项,而不必在顶层声明它们361362```js363// file: ./examples/pm364var program = require('commander');364const program = require('commander');365366program367 .version('0.1.0')368 .command('install [name]', 'install one or more packages')
35 unmodified lines
404405```js406#!/usr/bin/env node407408var program = require('commander');408const program = require('commander');409410program411 .version('0.0.1')412 .option('-f, --foo', 'enable some foo')
54 unmodified lines
467可选的回调可在显示帮助文本后处理。468如果你想显示默认的帮助(例如,如果没有提供命令),你可以使用类似的东西:469470```js471var program = require('commander');472var colors = require('colors');471const program = require('commander');472const colors = require('colors');473474program475 .version('0.1.0')476 .command('getstream [url]', 'get stream URL')
70 unmodified lines
547548## 例子549550```js551var program = require('commander');551const program = require('commander');552553program554 .version('0.1.0')555 .option('-C, --chdir <path>', 'change the working directory')
4 unmodified lines
560 .command('setup [env]')561 .description('run setup commands for all envs')562 .option("-s, --setup_mode [mode]", "Which setup mode to use")563 .action(function(env, options){564 var mode = options.setup_mode || "normal";564 const mode = options.setup_mode || "normal";565 env = env || 'all';566 console.log('setup for %s env(s) with %s mode', env, mode);567 });568
35 unmodified lines
modified:Readme.md+9 added-9 removed
287 unmodified lines
288289You use `.arguments` to specify the arguments for the top-level command, and for subcommands they are included in the `.command` call. Angled brackets (e.g. `<required>`) indicate required input. Square brackets (e.g. `[optional]`) indicate optional input.290291```js292var program = require('commander');292const program = require('commander');293294program295 .version('0.1.0')296 .arguments('<cmd> [env]')
15 unmodified lines
312 The last argument of a command can be variadic, and only the last argument. To make an argument variadic you313 append `...` to the argument name. For example:314315```js316var program = require('commander');316const program = require('commander');317318program319 .version('0.1.0')320 .command('rmdir <dir> [otherDirs...]')
17 unmodified lines
338The action handler gets passed a parameter for each argument you declared, and one additional argument which is the339command object itself. This command argument has the values for the command-specific options added as properties.340341```js342var program = require('commander');342const program = require('commander');343344program345 .command('rm <dir>')346 .option('-r, --recursive', 'Remove recursively')
17 unmodified lines
364You handle the options for an executable (sub)command in the executable, and don't declare them at the top-level.365366```js367// file: ./examples/pm368var program = require('commander');368const program = require('commander');369370program371 .version('0.1.0')372 .command('install [name]', 'install one or more packages')
39 unmodified lines
412413```js414#!/usr/bin/env node415416var program = require('commander');416const program = require('commander');417418program419 .version('0.1.0')420 .option('-f, --foo', 'enable some foo')
56 unmodified lines
477478If you want to display help by default (e.g. if no command was provided), you can use something like:479480```js481var program = require('commander');482var colors = require('colors');481const program = require('commander');482const colors = require('colors');483484program485 .version('0.1.0')486 .command('getstream [url]', 'get stream URL')
69 unmodified lines
556557## Examples558559```js560var program = require('commander');560const program = require('commander');561562program563 .version('0.1.0')564 .option('-C, --chdir <path>', 'change the working directory')
4 unmodified lines
569 .command('setup [env]')570 .description('run setup commands for all envs')571 .option("-s, --setup_mode [mode]", "Which setup mode to use")572 .action(function(env, options){573 var mode = options.setup_mode || "normal";573 const mode = options.setup_mode || "normal";574 env = env || 'all';575 console.log('setup for %s env(s) with %s mode', env, mode);576 });577
35 unmodified lines