Skip to content
Myr

Wrap and indent help descriptions for options and commands

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

authored by John Gee · committed by John Gee · parent 1bed2ff · Browse files · 2 files+180 added−4 removed
This commit

State

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

Trailers

  • Co-authored-by: Ephigenia <love@ephigenia.de>

Diff stats

2 files+180 added−4 removed

  • tests:+123 added−0 removed
  • other:+57 added−4 removed

2 files+180 added−4 removed

added:tests/helpwrap.test.js+123 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,101`select time102103Time can also be specified using special values:104 "dawn" - From night to sunrise.105`);106107 const expectedOutput =108`Usage: [options]109110Options:111 ${optionSpec} select time112 113 Time can also be specified using special values:114 "dawn" - From night to sunrise.115 116 -h, --help output usage information117`;118119 expect(program.helpInformation()).toBe(expectedOutput);120 process.stdout.columns = oldColumns;121});122123// test for argsDescription passed to command ????
modified:index.js+57 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;1158 var minWidth = 40;11591160 // Append the help information1161 return this.options.map(function(option) {1158 return pad(option.flags, width) + ' ' + option.description +1162 const fullDesc = option.description +1163 ((!option.negate && option.defaultValue !== undefined) ? ' (default: ' + JSON.stringify(option.defaultValue) + ')' : '');1160 }).concat([pad(this._helpFlags, width) + ' ' + this._helpDescription])1164 return pad(option.flags, width) + ' ' + optionalWrap(fullDesc, descriptionWidth, width + 2, minWidth);1165 }).concat([pad(this._helpFlags, width) + ' ' + optionalWrap(this._helpDescription, descriptionWidth, width + 2, minWidth)])1166 .join('\n');1167};11681169/**
8 unmodified lines
11781179 var commands = this.prepareCommands();1180 var width = this.padWidth();11811182 var columns = process.stdout.columns || 80;1183 var descriptionWidth = columns - width - 4;1184 var minWidth = 40;11851186 return [1187 'Commands:',1188 commands.map(function(cmd) {1189 var desc = cmd[1] ? ' ' + cmd[1] : '';1181 return (desc ? pad(cmd[0], width) : cmd[0]) + desc;1190 return (desc ? pad(cmd[0], width) : cmd[0]) + optionalWrap(desc, descriptionWidth, width + 2, minWidth);1191 }).join('\n').replace(/^/gm, ' '),1192 ''1193 ].join('\n');1194};
15 unmodified lines
12101211 var argsDescription = this._argsDescription;1212 if (argsDescription && this._args.length) {1213 var width = this.padWidth();1214 var columns = process.stdout.columns || 80;1215 var descriptionWidth = columns - width - 5;1216 desc.push('Arguments:');1217 desc.push('');1218 this._args.forEach(function(arg) {1208 desc.push(' ' + pad(arg.name, width) + ' ' + argsDescription[arg.name]);1219 desc.push(' ' + pad(arg.name, width) + ' ' + wrap(argsDescription[arg.name], descriptionWidth, width + 4));1220 });1221 desc.push('');1222 }1223 }
116 unmodified lines
1340 return str + Array(len + 1).join(' ');1341}13421343/**1344 * Wraps the given string with line breaks at the specified width while breaking1345 * words and indenting every but the first line on the left.1346 *1347 * @param {String} str1348 * @param {Number} width1349 * @param {Number} indent1350 * @return {String}1351 * @api private1352 */1353function wrap(str, width, indent) {1354 var regex = new RegExp('.{1,' + (width - 1) + '}([\\s\u200B]|$)|[^\\s\u200B]+?([\\s\u200B]|$)', 'g');1355 var lines = str.match(regex) || [];1356 var result = lines.map(function(line, i) {1357 if (line.slice(-1) === '\n') {1358 line = line.slice(0, line.length - 1);1359 }1360 return ((i > 0 && indent) ? Array(indent + 1).join(' ') : '') + line;1361 }).join('\n');1362 return result;1363}13641365/**1366 * Optionally wrap the given str to a max width of width characters per line1367 * while indenting with indent spaces. Do not wrap the text when the minWidth1368 * is not hit.1369 *1370 * @param {String} str1371 * @param {Number} width1372 * @param {Number} indent1373 * @return {String}1374 * @api private1375 */1376function optionalWrap(str, width, indent, minWidth) {1377 // detected manually wrapped and indented strings by searching for line breaks1378 // followed by multiple spaces/tabs1379 if (str.match(/[\n]\s+/)) return str;1380 // check if minimum width is reached1381 if (width < minWidth) return str;1382 return wrap(str, width, indent);1383}13841385/**1386 * Output help information if necessary1387 *1388 * @param {Command} command to output help for1389 * @param {Array} array of options to search for -h or --help
88 unmodified lines