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
Provenance
- Commit
725ebd1- Tree
0e2318f- Author
John Gee <john@ruru.gen.nz>- Committer
John Gee <john@ruru.gen.nz>- Parent
1bed2ff
2 files changed+180 added−4 removed
added:tests/helpwrap.test.js+123 added
1+const commander = require('../');2+3+// Test auto wrap and indent with some manual strings.4+5+test('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');11+12+ const expectedOutput =13+`Usage: [options]14+15+Options: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+`;21+22+ expect(program.helpInformation()).toBe(expectedOutput);23+ process.stdout.columns = oldColumns;24+});25+26+test('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');32+33+ const expectedOutput =34+`Usage: [options]35+36+Options:37+ -x -extra-long-option <value> kjsahdkajshkahd kajhsd akhds (default: "aaa 38+ bbb ccc ddd eee fff ggg")39+ -h, --help output usage information40+`;41+42+ expect(program.helpInformation()).toBe(expectedOutput);43+ process.stdout.columns = oldColumns;44+});45+46+test('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.');53+54+ const expectedOutput =55+`Usage: [options] [command]56+57+Options:58+ -x -extra-long-option-switch x59+ -h, --help output usage information60+61+Commands:62+ alpha Lorem mollit quis dolor ex do eu quis ad insa 63+ a commodo esse.64+`;65+66+ expect(program.helpInformation()).toBe(expectedOutput);67+ process.stdout.columns = oldColumns;68+});69+70+test('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);78+79+ const expectedOutput =80+`Usage: [options] [command]81+82+Options:83+ -h, --help output usage information84+85+Commands:86+ 1234567801234567890x ${commandDescription}87+`;88+89+ expect(program.helpInformation()).toBe(expectedOutput);90+ process.stdout.columns = oldColumns;91+});92+93+test('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 time102+103+Time can also be specified using special values:104+ "dawn" - From night to sunrise.105+`);106+107+ const expectedOutput =108+`Usage: [options]109+110+Options: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+`;118+119+ expect(program.helpInformation()).toBe(expectedOutput);120+ process.stdout.columns = oldColumns;121+});122+123+// test for argsDescription passed to command ????modified:index.js+57 added-4 removed
1151 unmodified lines1152 1153 Command.prototype.optionHelp = function() {1154 var width = this.padWidth();1155 1156+ var columns = process.stdout.columns || 80;1157+ var descriptionWidth = columns - width - 4;1158+ var minWidth = 40;1159+1160 // 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 };1168 1169 /**8 unmodified lines1178 1179 var commands = this.prepareCommands();1180 var width = this.padWidth();1181 1182+ var columns = process.stdout.columns || 80;1183+ var descriptionWidth = columns - width - 4;1184+ var minWidth = 40;1185+1186 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 lines1210 1211 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 lines1340 return str + Array(len + 1).join(' ');1341 }1342 1343 /**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+ */1353+function 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+}1364+1365+/**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+ */1376+function 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+}1384+1385+/**1386 * Output help information if necessary1387 *1388 * @param {Command} command to output help for1389 * @param {Array} array of options to search for -h or --help88 unmodified lines