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
Provenance
- Commit
713db77- Tree
f67471f- Author
Jared Petersen <jaredpetersen@users.noreply.github.com>- Committer
Roman Vanesyan <romain.vanesyan@gmail.com>- Parent
af17996
8 files changed+67 added−78 removed
modified:examples/custom-help+3 added-3 removed
14 unmodified lines15 // must be before .parse() since16 // node's emit() is immediate17 18 program.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 });25 26 program.parse(process.argv);27 1 unmodified linemodified:test/test.command.help.js+2 added-2 removed
3 unmodified lines4567889101112123 unmodified lines
program.command('bare');
program.commandHelp().should.equal(' Commands:\n\n bare\n');program.commandHelp().should.equal('Commands:\n\n bare\n');
program.command('mycommand [options]');
program.commandHelp().should.equal(' Commands:\n\n bare\n mycommand [options]\n');No newline at end of fileprogram.commandHelp().should.equal('Commands:\n\n bare\n mycommand [options]\n');modified:test/test.command.helpInformation.js+7 added-8 removed
5 unmodified lines6789101111121313141515161717181920211819202122235 unmodified linesprogram.command('somecommand');program.command('anothercommand [options]');
var expectedHelpInformation = [ 'Usage: [options] [command]', '', ' Usage: [options] [command]', 'Options:', '', ' Options:', ' -h, --help output usage information', '', ' -h, --help output usage information', 'Commands:', '', ' Commands:', '', ' somecommand', ' anothercommand [options]', '\n' ' somecommand', ' anothercommand [options]', ''].join('\n');
program.helpInformation().should.equal(expectedHelpInformation);modified:test/test.command.name.js+1 added-1 removed
16 unmodified lines17181920212122232416 unmodified lines
var output = process.stdout.write.args[0];
output[0].should.containEql([ ' mycommand [options] this is my command' ' mycommand [options] this is my command'].join('\n'));
sinon.restore();modified:test/test.command.nohelp.js+4 added-4 removed
45 unmodified lines46474849505051525354525354555645 unmodified lines
var output = process.stdout.write.args[0];
var expect = [ ' Commands:', 'Commands:', '', ' mycommand [options] this is my command', ' anothercommand [options]', ' help [cmd] display help for [cmd]' ' mycommand [options] this is my command', ' anothercommand [options]', ' help [cmd] display help for [cmd]'].join('\n');output[0].indexOf(expect).should.not.be.equal(-1);modified:index.js+10 added-12 removed
578 unmodified lines579 });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 lines1071 var commands = this.prepareCommands();1072 var width = this.padWidth();1073 1074 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 lines1092 Command.prototype.helpInformation = function() {1093 var desc = [];1094 if (this._description) {1095 desc = [1096- ' ' + this._description,1096+ this._description,1097 ''1098 ];1099 1100 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 lines1113 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 ];1120 1121 var cmds = [];1122 var commandHelp = this.commandHelp();1123 if (commandHelp) cmds = [commandHelp];1124 1125 var options = [1127- ' Options:',1126+ 'Options:',1127 '',1129- '' + this.optionHelp().replace(/^/gm, ' '),1128+ '' + this.optionHelp().replace(/^/gm, ' '),1129 ''1130 ];1131 1132 return usage1133 .concat(desc)1134 .concat(options)1135 .concat(cmds)1137- .concat([''])1136 .join('\n');1137 };1138 1139 /**98 unmodified linesmodified:Readme_zh-CN.md+17 added-21 removed
184 unmodified lines185 186 帮助信息是 commander 基于你的程序自动生成的,下面是 `--help` 生成的帮助信息:187 188 ``` 189- $ ./examples/pizza --help189+$ ./examples/pizza --help190+Usage: pizza [options]191 191- Usage: pizza [options]192+An application for pizzas ordering193 193- An application for pizzas ordering194+Options:195 195- Options:196-197- -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 cheese204-203 ```204 205 ## 自定义帮助206 17 unmodified lines224 // must be before .parse() since225 // node's emit() is immediate226 227 program.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 });234 235 program.parse(process.argv);236 2 unmodified lines239 240 下列帮助信息是运行 `node script-name.js -h` or `node script-name.js --help` 时输出的:241 242 ```245-243 Usage: custom-help [options]244 245 Options:246 6 unmodified lines253 Examples:254 255 $ custom-help --help256 $ custom-help -h260-257 ```258 259 ## .outputHelp(cb)260 52 unmodified lines313 .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 });323 324 program325 .command('*')12 unmodified linesmodified:Readme.md+23 added-27 removed
43 unmodified lines44 ```45 46 Short 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.47 48-Note 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. 48+Note 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.49 50 ```js51 #!/usr/bin/env node52 99 unmodified lines152 .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- 156+157 console.log(' size: %j', program.size);158 console.log(' drink: %j', program.drink);159 ```160 86 unmodified lines247 248 The help information is auto-generated based on the information commander already knows about your program, so the following `--help` info is for free:249 250 ``` 251- $ ./examples/pizza --help251+$ ./examples/pizza --help252+Usage: pizza [options]253 253- Usage: pizza [options]254+An application for pizzas ordering255 255- An application for pizzas ordering256+Options:257 257- Options:258-259- -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 cheese266-265 ```266 267 ## Custom help268 269 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.275 276 ```js14 unmodified lines291 // must be before .parse() since292 // node's emit() is immediate293 294 program.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 });301 302 program.parse(process.argv);303 2 unmodified lines306 307 Yields the following help output when `node script-name.js -h` or `node script-name.js --help` are run:308 309 ```312-310 Usage: custom-help [options]311 312 Options:313 6 unmodified lines320 Examples:321 322 $ custom-help --help323 $ custom-help -h327-324 ```325 326 ## .outputHelp(cb)327 69 unmodified lines397 .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 });407 408 program409 .command('*')12 unmodified lines