Skip to content
Myr

Standardize help output (#853)

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

* Removed newline before and after help output * Updated English documentation and examples * Unindented help output * Fixed newline and indentation for missing command error * Updated REAME examples in English and Chinese and fixed example indentation * Removed newlines surrounding EACCESS error message

authored by Jared Petersen · committed by Roman Vanesyan · parent af17996 · Browse files · 8 files+67 added−78 removed
This commit

State

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

Diff stats

8 files+67 added−78 removed

  • tests:+14 added−15 removed
  • other:+53 added−63 removed

8 files+67 added−78 removed

modified:examples/custom-help+3 added-3 removed
14 unmodified lines
15// must be before .parse() since16// node's emit() is immediate1718program.on('--help', function(){19 console.log(' Examples:');19 console.log('');21 console.log(' $ custom-help --help');22 console.log(' $ custom-help -h');20 console.log('Examples:');21 console.log('');22 console.log(' $ custom-help --help');23 console.log(' $ custom-help -h');24});2526program.parse(process.argv);27
1 unmodified line
modified:test/test.command.help.js+2 added-2 removed
modified:test/test.command.helpInformation.js+7 added-8 removed
modified:test/test.command.name.js+1 added-1 removed
modified:test/test.command.nohelp.js+4 added-4 removed
modified:index.js+10 added-12 removed
578 unmodified lines
579 });580 proc.on('close', process.exit.bind(process));581 proc.on('error', function(err) {582 if (err.code === 'ENOENT') {583 console.error('\n %s(1) does not exist, try --help\n', bin);583 console.error('%s(1) does not exist, try --help', bin);584 } else if (err.code === 'EACCES') {585 console.error('\n %s(1) not executable. try chmod or run with root\n', bin);585 console.error('%s(1) not executable. try chmod or run with root', bin);586 }587 process.exit(1);588 });589
481 unmodified lines
1071 var commands = this.prepareCommands();1072 var width = this.padWidth();10731074 return [1075 ' Commands:',1075 'Commands:',1076 '',1077 commands.map(function(cmd) {1078 var desc = cmd[1] ? ' ' + cmd[1] : '';1079 return (desc ? pad(cmd[0], width) : cmd[0]) + desc;1080 }).join('\n').replace(/^/gm, ' '),1080 }).join('\n').replace(/^/gm, ' '),1081 ''1082 ].join('\n');1083};1084
7 unmodified lines
1092Command.prototype.helpInformation = function() {1093 var desc = [];1094 if (this._description) {1095 desc = [1096 ' ' + this._description,1096 this._description,1097 ''1098 ];10991100 var argsDescription = this._argsDescription;1101 if (argsDescription && this._args.length) {1102 var width = this.padWidth();1103 desc.push(' Arguments:');1103 desc.push('Arguments:');1104 desc.push('');1105 this._args.forEach(function(arg) {1106 desc.push(' ' + pad(arg.name, width) + ' ' + argsDescription[arg.name]);1106 desc.push(' ' + pad(arg.name, width) + ' ' + argsDescription[arg.name]);1107 });1108 desc.push('');1109 }1110 }
2 unmodified lines
1113 if (this._alias) {1114 cmdName = cmdName + '|' + this._alias;1115 }1116 var usage = [1117 '',1118 ' Usage: ' + cmdName + ' ' + this.usage(),1117 'Usage: ' + cmdName + ' ' + this.usage(),1118 ''1119 ];11201121 var cmds = [];1122 var commandHelp = this.commandHelp();1123 if (commandHelp) cmds = [commandHelp];11241125 var options = [1127 ' Options:',1126 'Options:',1127 '',1129 '' + this.optionHelp().replace(/^/gm, ' '),1128 '' + this.optionHelp().replace(/^/gm, ' '),1129 ''1130 ];11311132 return usage1133 .concat(desc)1134 .concat(options)1135 .concat(cmds)1137 .concat([''])1136 .join('\n');1137};11381139/**
98 unmodified lines
modified:Readme_zh-CN.md+17 added-21 removed
184 unmodified lines
185186 帮助信息是 commander 基于你的程序自动生成的,下面是 `--help` 生成的帮助信息:187188``` 189 $ ./examples/pizza --help189$ ./examples/pizza --help190Usage: pizza [options]191191 Usage: pizza [options]192An application for pizzas ordering193193 An application for pizzas ordering194Options:195195 Options:196197 -h, --help output usage information198 -V, --version output the version number199 -p, --peppers Add peppers200 -P, --pineapple Add pineapple201 -b, --bbq Add bbq sauce202 -c, --cheese <type> Add the specified type of cheese [marble]203 -C, --no-cheese You do not want any cheese196 -h, --help output usage information197 -V, --version output the version number198 -p, --peppers Add peppers199 -P, --pineapple Add pineapple200 -b, --bbq Add bbq sauce201 -c, --cheese <type> Add the specified type of cheese [marble]202 -C, --no-cheese You do not want any cheese204203```204205## 自定义帮助206
17 unmodified lines
224// must be before .parse() since225// node's emit() is immediate226227program.on('--help', function(){228 console.log('');229 console.log(' Examples:');230 console.log('');231 console.log(' $ custom-help --help');232 console.log(' $ custom-help -h');234 console.log('');233});234235program.parse(process.argv);236
2 unmodified lines
239240下列帮助信息是运行 `node script-name.js -h` or `node script-name.js --help` 时输出的:241242```245243Usage: custom-help [options]244245Options:246
6 unmodified lines
253Examples:254255 $ custom-help --help256 $ custom-help -h260257```258259## .outputHelp(cb)260
52 unmodified lines
313 .option("-e, --exec_mode <mode>", "Which exec mode to use")314 .action(function(cmd, options){315 console.log('exec "%s" using %s mode', cmd, options.exec_mode);316 }).on('--help', function() {321 console.log(' Examples:');322 console.log();323 console.log(' $ deploy exec sequential');324 console.log(' $ deploy exec async');325 console.log();317 console.log('');318 console.log('Examples:');319 console.log('');320 console.log(' $ deploy exec sequential');321 console.log(' $ deploy exec async');322 });323324program325 .command('*')
12 unmodified lines
modified:Readme.md+23 added-27 removed
43 unmodified lines
44```4546Short flags may be passed as a single arg, for example `-abc` is equivalent to `-a -b -c`. Multi-word options such as "--template-engine" are camel-cased, becoming `program.templateEngine` etc.4748Note that multi-word options starting with `--no` prefix negate the boolean value of the following word. For example, `--no-sauce` sets the value of `program.sauce` to false. 48Note that multi-word options starting with `--no` prefix negate the boolean value of the following word. For example, `--no-sauce` sets the value of `program.sauce` to false.4950```js51#!/usr/bin/env node52
99 unmodified lines
152 .version('0.1.0')153 .option('-s --size <size>', 'Pizza size', /^(large|medium|small)$/i, 'medium')154 .option('-d --drink [drink]', 'Drink', /^(coke|pepsi|izze)$/i)155 .parse(process.argv);156 156157console.log(' size: %j', program.size);158console.log(' drink: %j', program.drink);159```160
86 unmodified lines
247248 The help information is auto-generated based on the information commander already knows about your program, so the following `--help` info is for free:249250``` 251 $ ./examples/pizza --help251$ ./examples/pizza --help252Usage: pizza [options]253253 Usage: pizza [options]254An application for pizzas ordering255255 An application for pizzas ordering256Options:257257 Options:258259 -h, --help output usage information260 -V, --version output the version number261 -p, --peppers Add peppers262 -P, --pineapple Add pineapple263 -b, --bbq Add bbq sauce264 -c, --cheese <type> Add the specified type of cheese [marble]265 -C, --no-cheese You do not want any cheese258 -h, --help output usage information259 -V, --version output the version number260 -p, --peppers Add peppers261 -P, --pineapple Add pineapple262 -b, --bbq Add bbq sauce263 -c, --cheese <type> Add the specified type of cheese [marble]264 -C, --no-cheese You do not want any cheese266265```266267## Custom help268269 You can display arbitrary `-h, --help` information270 by listening for "--help". Commander will automatically271 exit once you are done so that the remainder of your program274 does not execute causing undesired behaviours, for example272 does not execute causing undesired behaviors, for example273 in the following executable "stuff" will not output when274 `--help` is used.275276```js
14 unmodified lines
291// must be before .parse() since292// node's emit() is immediate293294program.on('--help', function(){297 console.log(' Examples:');295 console.log('')296 console.log('Examples:');297 console.log('');299 console.log(' $ custom-help --help');300 console.log(' $ custom-help -h');301 console.log('');298 console.log(' $ custom-help --help');299 console.log(' $ custom-help -h');300});301302program.parse(process.argv);303
2 unmodified lines
306307Yields the following help output when `node script-name.js -h` or `node script-name.js --help` are run:308309```312310Usage: custom-help [options]311312Options:313
6 unmodified lines
320Examples:321322 $ custom-help --help323 $ custom-help -h327324```325326## .outputHelp(cb)327
69 unmodified lines
397 .option("-e, --exec_mode <mode>", "Which exec mode to use")398 .action(function(cmd, options){399 console.log('exec "%s" using %s mode', cmd, options.exec_mode);400 }).on('--help', function() {405 console.log(' Examples:');406 console.log();407 console.log(' $ deploy exec sequential');408 console.log(' $ deploy exec async');409 console.log();401 console.log('');402 console.log('Examples:');403 console.log('');404 console.log(' $ deploy exec sequential');405 console.log(' $ deploy exec async');406 });407408program409 .command('*')
12 unmodified lines