Skip to content
Myr

Merge pull request #1051 from shadowspawn/feature/pretty-help

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

Wrap and indent help descriptions for options and commands

authored by John Gee · committed by GitHub · parents 85d4c2b, 97b5fee · Browse files · 2 files+178 added−4 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+178 added−4 removed

  • tests:+122 added−0 removed
  • other:+56 added−4 removed

2 files+178 added−4 removed

added:tests/helpwrap.test.js+122 added
1const commander = require('../');23// Test auto wrap and indent with some manual strings.45test('when long option description then wrap and indent', () => {6 const oldColumns = process.stdout.columns;7 process.stdout.columns = 80;8 const program = new commander.Command();9 program10 .option('-x -extra-long-option-switch', 'kjsahdkajshkahd kajhsd akhds kashd kajhs dkha dkh aksd ka dkha kdh kasd ka kahs dkh sdkh askdh aksd kashdk ahsd kahs dkha skdh');1112 const expectedOutput =13`Usage: [options]1415Options:16 -x -extra-long-option-switch kjsahdkajshkahd kajhsd akhds kashd kajhs dkha 17 dkh aksd ka dkha kdh kasd ka kahs dkh sdkh 18 askdh aksd kashdk ahsd kahs dkha skdh19 -h, --help output usage information20`;2122 expect(program.helpInformation()).toBe(expectedOutput);23 process.stdout.columns = oldColumns;24});2526test('when long option description and default then wrap and indent', () => {27 const oldColumns = process.stdout.columns;28 process.stdout.columns = 80;29 const program = new commander.Command();30 program31 .option('-x -extra-long-option <value>', 'kjsahdkajshkahd kajhsd akhds', 'aaa bbb ccc ddd eee fff ggg');3233 const expectedOutput =34`Usage: [options]3536Options:37 -x -extra-long-option <value> kjsahdkajshkahd kajhsd akhds (default: "aaa 38 bbb ccc ddd eee fff ggg")39 -h, --help output usage information40`;4142 expect(program.helpInformation()).toBe(expectedOutput);43 process.stdout.columns = oldColumns;44});4546test('when long command description then wrap and indent', () => {47 const oldColumns = process.stdout.columns;48 process.stdout.columns = 80;49 const program = new commander.Command();50 program51 .option('-x -extra-long-option-switch', 'x')52 .command('alpha', 'Lorem mollit quis dolor ex do eu quis ad insa a commodo esse.');5354 const expectedOutput =55`Usage: [options] [command]5657Options:58 -x -extra-long-option-switch x59 -h, --help output usage information6061Commands:62 alpha Lorem mollit quis dolor ex do eu quis ad insa 63 a commodo esse.64`;6566 expect(program.helpInformation()).toBe(expectedOutput);67 process.stdout.columns = oldColumns;68});6970test('when not enough room then help not wrapped', () => {71 // Not wrapping if less than 40 columns available for wrapping.72 const oldColumns = process.stdout.columns;73 process.stdout.columns = 60;74 const program = new commander.Command();75 const commandDescription = 'description text of very long command which should not be automatically be wrapped. Do fugiat eiusmod ipsum laboris excepteur pariatur sint ullamco tempor labore eu.';76 program77 .command('1234567801234567890x', commandDescription);7879 const expectedOutput =80`Usage: [options] [command]8182Options:83 -h, --help output usage information8485Commands:86 1234567801234567890x ${commandDescription}87`;8889 expect(program.helpInformation()).toBe(expectedOutput);90 process.stdout.columns = oldColumns;91});9293test('when option descripton preformatted then only add small indent', () => {94 const oldColumns = process.stdout.columns;95 process.stdout.columns = 80;96 // #396: leave custom format alone, apart from space-space indent97 const optionSpec = '-t, --time <HH:MM>';98 const program = new commander.Command();99 program100 .option(optionSpec, `select time101102Time can also be specified using special values:103 "dawn" - From night to sunrise.104`);105106 const expectedOutput =107`Usage: [options]108109Options:110 ${optionSpec} select time111 112 Time can also be specified using special values:113 "dawn" - From night to sunrise.114 115 -h, --help output usage information116`;117118 expect(program.helpInformation()).toBe(expectedOutput);119 process.stdout.columns = oldColumns;120});121122// test for argsDescription passed to command ????
modified:index.js+56 added-4 removed
1151 unmodified lines
11521153Command.prototype.optionHelp = function() {1154 var width = this.padWidth();11551156 var columns = process.stdout.columns || 80;1157 var descriptionWidth = columns - width - 4;11581159 // Append the help information1160 return this.options.map(function(option) {1158 return pad(option.flags, width) + ' ' + option.description +1161 const fullDesc = option.description +1162 ((!option.negate && option.defaultValue !== undefined) ? ' (default: ' + JSON.stringify(option.defaultValue) + ')' : '');1160 }).concat([pad(this._helpFlags, width) + ' ' + this._helpDescription])1163 return pad(option.flags, width) + ' ' + optionalWrap(fullDesc, descriptionWidth, width + 2);1164 }).concat([pad(this._helpFlags, width) + ' ' + optionalWrap(this._helpDescription, descriptionWidth, width + 2)])1165 .join('\n');1166};11671168/**
8 unmodified lines
11771178 var commands = this.prepareCommands();1179 var width = this.padWidth();11801181 var columns = process.stdout.columns || 80;1182 var descriptionWidth = columns - width - 4;11831184 return [1185 'Commands:',1186 commands.map(function(cmd) {1187 var desc = cmd[1] ? ' ' + cmd[1] : '';1181 return (desc ? pad(cmd[0], width) : cmd[0]) + desc;1188 return (desc ? pad(cmd[0], width) : cmd[0]) + optionalWrap(desc, descriptionWidth, width + 2);1189 }).join('\n').replace(/^/gm, ' '),1190 ''1191 ].join('\n');1192};
15 unmodified lines
12081209 var argsDescription = this._argsDescription;1210 if (argsDescription && this._args.length) {1211 var width = this.padWidth();1212 var columns = process.stdout.columns || 80;1213 var descriptionWidth = columns - width - 5;1214 desc.push('Arguments:');1215 desc.push('');1216 this._args.forEach(function(arg) {1208 desc.push(' ' + pad(arg.name, width) + ' ' + argsDescription[arg.name]);1217 desc.push(' ' + pad(arg.name, width) + ' ' + wrap(argsDescription[arg.name], descriptionWidth, width + 4));1218 });1219 desc.push('');1220 }1221 }
116 unmodified lines
1338 return str + Array(len + 1).join(' ');1339}13401341/**1342 * Wraps the given string with line breaks at the specified width while breaking1343 * words and indenting every but the first line on the left.1344 *1345 * @param {String} str1346 * @param {Number} width1347 * @param {Number} indent1348 * @return {String}1349 * @api private1350 */1351function wrap(str, width, indent) {1352 var regex = new RegExp('.{1,' + (width - 1) + '}([\\s\u200B]|$)|[^\\s\u200B]+?([\\s\u200B]|$)', 'g');1353 var lines = str.match(regex) || [];1354 return lines.map(function(line, i) {1355 if (line.slice(-1) === '\n') {1356 line = line.slice(0, line.length - 1);1357 }1358 return ((i > 0 && indent) ? Array(indent + 1).join(' ') : '') + line;1359 }).join('\n');1360}13611362/**1363 * Optionally wrap the given str to a max width of width characters per line1364 * while indenting with indent spaces. Do not wrap if insufficient width or1365 * string is manually formatted.1366 *1367 * @param {String} str1368 * @param {Number} width1369 * @param {Number} indent1370 * @return {String}1371 * @api private1372 */1373function optionalWrap(str, width, indent) {1374 // Detect manually wrapped and indented strings by searching for line breaks1375 // followed by multiple spaces/tabs.1376 if (str.match(/[\n]\s+/)) return str;1377 // Do not wrap to narrow columns (or can end up with a word per line).1378 const minWidth = 40;1379 if (width < minWidth) return str;13801381 return wrap(str, width, indent);1382}13831384/**1385 * Output help information if necessary1386 *1387 * @param {Command} command to output help for1388 * @param {Array} array of options to search for -h or --help
88 unmodified lines