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
Provenance
- Commit
55386f4- Tree
f585986- Author
John Gee <john@ruru.gen.nz>- Committer
GitHub <noreply@github.com>- Parent 1
85d4c2b- Parent 2
97b5fee
2 files changed+178 added−4 removed
added:tests/helpwrap.test.js+122 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, `select time101+102+Time can also be specified using special values:103+ "dawn" - From night to sunrise.104+`);105+106+ const expectedOutput =107+`Usage: [options]108+109+Options: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+`;117+118+ expect(program.helpInformation()).toBe(expectedOutput);119+ process.stdout.columns = oldColumns;120+});121+122+// test for argsDescription passed to command ????modified:index.js+56 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+1159 // 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 };1167 1168 /**8 unmodified lines1177 1178 var commands = this.prepareCommands();1179 var width = this.padWidth();1180 1181+ var columns = process.stdout.columns || 80;1182+ var descriptionWidth = columns - width - 4;1183+1184 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 lines1208 1209 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 lines1338 return str + Array(len + 1).join(' ');1339 }1340 1341 /**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+ */1351+function 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+}1361+1362+/**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+ */1373+function 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;1380+1381+ return wrap(str, width, indent);1382+}1383+1384+/**1385 * Output help information if necessary1386 *1387 * @param {Command} command to output help for1388 * @param {Array} array of options to search for -h or --help88 unmodified lines