No Description

formatter.js 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. /**
  2. * class HelpFormatter
  3. *
  4. * Formatter for generating usage messages and argument help strings. Only the
  5. * name of this class is considered a public API. All the methods provided by
  6. * the class are considered an implementation detail.
  7. *
  8. * Do not call in your code, use this class only for inherits your own forvatter
  9. *
  10. * ToDo add [additonal formatters][1]
  11. *
  12. * [1]:http://docs.python.org/dev/library/argparse.html#formatter-class
  13. **/
  14. 'use strict';
  15. var _ = require('underscore');
  16. _.str = require('underscore.string');
  17. // Constants
  18. var $$ = require('../const');
  19. /*:nodoc:* internal
  20. * new Support(parent, heding)
  21. * - parent (object): parent section
  22. * - heading (string): header string
  23. *
  24. **/
  25. function Section(parent, heading) {
  26. this._parent = parent;
  27. this._heading = heading;
  28. this._items = [];
  29. }
  30. /*:nodoc:* internal
  31. * Section#addItem(callback) -> Void
  32. * - callback (array): tuple with function and args
  33. *
  34. * Add function for single element
  35. **/
  36. Section.prototype.addItem = function (callback) {
  37. this._items.push(callback);
  38. };
  39. /*:nodoc:* internal
  40. * Section#formatHelp(formatter) -> string
  41. * - formatter (HelpFormatter): current formatter
  42. *
  43. * Form help section string
  44. *
  45. **/
  46. Section.prototype.formatHelp = function (formatter) {
  47. var itemHelp, heading;
  48. // format the indented section
  49. if (!!this._parent) {
  50. formatter._indent();
  51. }
  52. itemHelp = this._items.map(function (item) {
  53. var obj, func, args;
  54. obj = formatter;
  55. func = item[0];
  56. args = item[1];
  57. return func.apply(obj, args);
  58. });
  59. itemHelp = formatter._joinParts(itemHelp);
  60. if (!!this._parent) {
  61. formatter._dedent();
  62. }
  63. // return nothing if the section was empty
  64. if (!itemHelp) {
  65. return '';
  66. }
  67. // add the heading if the section was non-empty
  68. heading = '';
  69. if (!!this._heading && this._heading !== $$.SUPPRESS) {
  70. var currentIndent = formatter.currentIndent;
  71. heading = _.str.repeat(' ', currentIndent) + this._heading + ':' + $$.EOL;
  72. }
  73. // join the section-initialize newline, the heading and the help
  74. return formatter._joinParts([$$.EOL, heading, itemHelp, $$.EOL]);
  75. };
  76. /**
  77. * new HelpFormatter(options)
  78. *
  79. * #### Options:
  80. * - `prog`: program name
  81. * - `indentIncriment`: indent step, default value 2
  82. * - `maxHelpPosition`: max help position, default value = 24
  83. * - `width`: line width
  84. *
  85. **/
  86. var HelpFormatter = module.exports = function HelpFormatter(options) {
  87. options = options || {};
  88. this._prog = options.prog;
  89. this._maxHelpPosition = options.maxHelpPosition || 24;
  90. this._width = (options.width || ((process.env.COLUMNS || 80) - 2));
  91. this._currentIndent = 0;
  92. this._indentIncriment = options.indentIncriment || 2;
  93. this._level = 0;
  94. this._actionMaxLength = 0;
  95. this._rootSection = new Section(null);
  96. this._currentSection = this._rootSection;
  97. this._whitespaceMatcher = new RegExp('\\s+', 'g');
  98. this._longBreakMatcher = new RegExp($$.EOL + $$.EOL + $$.EOL + '+', 'g');
  99. };
  100. HelpFormatter.prototype._indent = function () {
  101. this._currentIndent += this._indentIncriment;
  102. this._level += 1;
  103. };
  104. HelpFormatter.prototype._dedent = function () {
  105. this._currentIndent -= this._indentIncriment;
  106. this._level -= 1;
  107. if (this._currentIndent < 0) {
  108. throw new Error('Indent decreased below 0.');
  109. }
  110. };
  111. HelpFormatter.prototype._addItem = function (func, args) {
  112. this._currentSection.addItem([func, args]);
  113. };
  114. //
  115. // Message building methods
  116. //
  117. /**
  118. * HelpFormatter#startSection(heading) -> Void
  119. * - heading (string): header string
  120. *
  121. * Start new help section
  122. *
  123. * See alse [code example][1]
  124. *
  125. * ##### Example
  126. *
  127. * formatter.startSection(actionGroup.title);
  128. * formatter.addText(actionGroup.description);
  129. * formatter.addArguments(actionGroup._groupActions);
  130. * formatter.endSection();
  131. *
  132. **/
  133. HelpFormatter.prototype.startSection = function (heading) {
  134. this._indent();
  135. var section = new Section(this._currentSection, heading);
  136. var func = section.formatHelp.bind(section);
  137. this._addItem(func, [this]);
  138. this._currentSection = section;
  139. };
  140. /**
  141. * HelpFormatter#endSection -> Void
  142. *
  143. * End help section
  144. *
  145. * ##### Example
  146. *
  147. * formatter.startSection(actionGroup.title);
  148. * formatter.addText(actionGroup.description);
  149. * formatter.addArguments(actionGroup._groupActions);
  150. * formatter.endSection();
  151. **/
  152. HelpFormatter.prototype.endSection = function () {
  153. this._currentSection = this._currentSection._parent;
  154. this._dedent();
  155. };
  156. /**
  157. * HelpFormatter#addText(text) -> Void
  158. * - text (string): plain text
  159. *
  160. * Add plain text into current section
  161. *
  162. * ##### Example
  163. *
  164. * formatter.startSection(actionGroup.title);
  165. * formatter.addText(actionGroup.description);
  166. * formatter.addArguments(actionGroup._groupActions);
  167. * formatter.endSection();
  168. *
  169. **/
  170. HelpFormatter.prototype.addText = function (text) {
  171. if (!!text && text !== $$.SUPPRESS) {
  172. this._addItem(this._formatText, [text]);
  173. }
  174. };
  175. /**
  176. * HelpFormatter#addUsage(usage, actions, groups, prefix) -> Void
  177. * - usage (string): usage text
  178. * - actions (array): actions list
  179. * - groups (array): groups list
  180. * - prefix (string): usage prefix
  181. *
  182. * Add usage data into current section
  183. *
  184. * ##### Example
  185. *
  186. * formatter.addUsage(this.usage, this._actions, []);
  187. * return formatter.formatHelp();
  188. *
  189. **/
  190. HelpFormatter.prototype.addUsage = function (usage, actions, groups, prefix) {
  191. if (usage !== $$.SUPPRESS) {
  192. this._addItem(this._formatUsage, [usage, actions, groups, prefix]);
  193. }
  194. };
  195. /**
  196. * HelpFormatter#addArgument(action) -> Void
  197. * - action (object): action
  198. *
  199. * Add argument into current section
  200. *
  201. * Single variant of [[HelpFormatter#addArguments]]
  202. **/
  203. HelpFormatter.prototype.addArgument = function (action) {
  204. if (action.help !== $$.SUPPRESS) {
  205. var self = this;
  206. // find all invocations
  207. var invocations = [this._formatActionInvocation(action)];
  208. var invocationLength = invocations[0].length;
  209. var actionLength;
  210. if (!!action._getSubactions) {
  211. this._indent();
  212. action._getSubactions().forEach(function (subaction) {
  213. var invocationNew = self._formatActionInvocation(subaction);
  214. invocations.push(invocationNew);
  215. invocationLength = Math.max(invocationLength, invocationNew.length);
  216. });
  217. this._dedent();
  218. }
  219. // update the maximum item length
  220. actionLength = invocationLength + this._currentIndent;
  221. this._actionMaxLength = Math.max(this._actionMaxLength, actionLength);
  222. // add the item to the list
  223. this._addItem(this._formatAction, [action]);
  224. }
  225. };
  226. /**
  227. * HelpFormatter#addArguments(actions) -> Void
  228. * - actions (array): actions list
  229. *
  230. * Mass add arguments into current section
  231. *
  232. * ##### Example
  233. *
  234. * formatter.startSection(actionGroup.title);
  235. * formatter.addText(actionGroup.description);
  236. * formatter.addArguments(actionGroup._groupActions);
  237. * formatter.endSection();
  238. *
  239. **/
  240. HelpFormatter.prototype.addArguments = function (actions) {
  241. var self = this;
  242. actions.forEach(function (action) {
  243. self.addArgument(action);
  244. });
  245. };
  246. //
  247. // Help-formatting methods
  248. //
  249. /**
  250. * HelpFormatter#formatHelp -> string
  251. *
  252. * Format help
  253. *
  254. * ##### Example
  255. *
  256. * formatter.addText(this.epilog);
  257. * return formatter.formatHelp();
  258. *
  259. **/
  260. HelpFormatter.prototype.formatHelp = function () {
  261. var help = this._rootSection.formatHelp(this);
  262. if (help) {
  263. help = help.replace(this._longBreakMatcher, $$.EOL + $$.EOL);
  264. help = _.str.strip(help, $$.EOL) + $$.EOL;
  265. }
  266. return help;
  267. };
  268. HelpFormatter.prototype._joinParts = function (partStrings) {
  269. return partStrings.filter(function (part) {
  270. return (!!part && part !== $$.SUPPRESS);
  271. }).join('');
  272. };
  273. HelpFormatter.prototype._formatUsage = function (usage, actions, groups, prefix) {
  274. if (!prefix && !_.isString(prefix)) {
  275. prefix = 'usage: ';
  276. }
  277. actions = actions || [];
  278. groups = groups || [];
  279. // if usage is specified, use that
  280. if (usage) {
  281. usage = _.str.sprintf(usage, {prog: this._prog});
  282. // if no optionals or positionals are available, usage is just prog
  283. } else if (!usage && actions.length === 0) {
  284. usage = this._prog;
  285. // if optionals and positionals are available, calculate usage
  286. } else if (!usage) {
  287. var prog = this._prog;
  288. var optionals = [];
  289. var positionals = [];
  290. var actionUsage;
  291. var textWidth;
  292. // split optionals from positionals
  293. actions.forEach(function (action) {
  294. if (action.isOptional()) {
  295. optionals.push(action);
  296. } else {
  297. positionals.push(action);
  298. }
  299. });
  300. // build full usage string
  301. actionUsage = this._formatActionsUsage([].concat(optionals, positionals), groups);
  302. usage = [prog, actionUsage].join(' ');
  303. // wrap the usage parts if it's too long
  304. textWidth = this._width - this._currentIndent;
  305. if ((prefix.length + usage.length) > textWidth) {
  306. // break usage into wrappable parts
  307. var regexpPart = new RegExp('\\(.*?\\)+|\\[.*?\\]+|\\S+', 'g');
  308. var optionalUsage = this._formatActionsUsage(optionals, groups);
  309. var positionalUsage = this._formatActionsUsage(positionals, groups);
  310. var optionalParts = optionalUsage.match(regexpPart);
  311. var positionalParts = positionalUsage.match(regexpPart) || [];
  312. if (optionalParts.join(' ') !== optionalUsage) {
  313. throw new Error('assert "optionalParts.join(\' \') === optionalUsage"');
  314. }
  315. if (positionalParts.join(' ') !== positionalUsage) {
  316. throw new Error('assert "positionalParts.join(\' \') === positionalUsage"');
  317. }
  318. // helper for wrapping lines
  319. var _getLines = function (parts, indent, prefix) {
  320. var lines = [];
  321. var line = [];
  322. var lineLength = !!prefix ? prefix.length - 1: indent.length - 1;
  323. parts.forEach(function (part) {
  324. if (lineLength + 1 + part.length > textWidth) {
  325. lines.push(indent + line.join(' '));
  326. line = [];
  327. lineLength = indent.length - 1;
  328. }
  329. line.push(part);
  330. lineLength += part.length + 1;
  331. });
  332. if (line) {
  333. lines.push(indent + line.join(' '));
  334. }
  335. if (prefix) {
  336. lines[0] = lines[0].substr(indent.length);
  337. }
  338. return lines;
  339. };
  340. var lines, indent, parts;
  341. // if prog is short, follow it with optionals or positionals
  342. if (prefix.length + prog.length <= 0.75 * textWidth) {
  343. indent = _.str.repeat(' ', (prefix.length + prog.length + 1));
  344. if (optionalParts) {
  345. lines = [].concat(
  346. _getLines([prog].concat(optionalParts), indent, prefix),
  347. _getLines(positionalParts, indent)
  348. );
  349. } else if (positionalParts) {
  350. lines = _getLines([prog].concat(positionalParts), indent, prefix);
  351. } else {
  352. lines = [prog];
  353. }
  354. // if prog is long, put it on its own line
  355. } else {
  356. indent = _.str.repeat(' ', prefix.length);
  357. parts = optionalParts + positionalParts;
  358. lines = _getLines(parts, indent);
  359. if (lines.length > 1) {
  360. lines = [].concat(
  361. _getLines(optionalParts, indent),
  362. _getLines(positionalParts, indent)
  363. );
  364. }
  365. lines = [prog] + lines;
  366. }
  367. // join lines into usage
  368. usage = lines.join($$.EOL);
  369. }
  370. }
  371. // prefix with 'usage:'
  372. return prefix + usage + $$.EOL + $$.EOL;
  373. };
  374. HelpFormatter.prototype._formatActionsUsage = function (actions, groups) {
  375. // find group indices and identify actions in groups
  376. var groupActions = [];
  377. var inserts = [];
  378. var self = this;
  379. groups.forEach(function (group) {
  380. var end;
  381. var i;
  382. var start = actions.indexOf(group._groupActions[0]);
  383. if (start >= 0) {
  384. end = start + group._groupActions.length;
  385. //if (actions.slice(start, end) === group._groupActions) {
  386. if (_.isEqual(actions.slice(start, end), group._groupActions)) {
  387. group._groupActions.forEach(function (action) {
  388. groupActions.push(action);
  389. });
  390. if (!group.required) {
  391. if (!!inserts[start]) {
  392. inserts[start] += ' [';
  393. }
  394. else {
  395. inserts[start] = '[';
  396. }
  397. inserts[end] = ']';
  398. } else {
  399. if (!!inserts[start]) {
  400. inserts[start] += ' (';
  401. }
  402. else {
  403. inserts[start] = '(';
  404. }
  405. inserts[end] = ')';
  406. }
  407. for (i = start + 1; i < end; i += 1) {
  408. inserts[i] = '|';
  409. }
  410. }
  411. }
  412. });
  413. // collect all actions format strings
  414. var parts = [];
  415. actions.forEach(function (action, actionIndex) {
  416. var part;
  417. var optionString;
  418. var argsDefault;
  419. var argsString;
  420. // suppressed arguments are marked with None
  421. // remove | separators for suppressed arguments
  422. if (action.help === $$.SUPPRESS) {
  423. parts.push(null);
  424. if (inserts[actionIndex] === '|') {
  425. inserts.splice(actionIndex, actionIndex);
  426. } else if (inserts[actionIndex + 1] === '|') {
  427. inserts.splice(actionIndex + 1, actionIndex + 1);
  428. }
  429. // produce all arg strings
  430. } else if (!action.isOptional()) {
  431. part = self._formatArgs(action, action.dest);
  432. // if it's in a group, strip the outer []
  433. if (groupActions.indexOf(action) >= 0) {
  434. if (part[0] === '[' && part[part.length - 1] === ']') {
  435. part = part.slice(1, -1);
  436. }
  437. }
  438. // add the action string to the list
  439. parts.push(part);
  440. // produce the first way to invoke the option in brackets
  441. } else {
  442. optionString = action.optionStrings[0];
  443. // if the Optional doesn't take a value, format is: -s or --long
  444. if (action.nargs === 0) {
  445. part = '' + optionString;
  446. // if the Optional takes a value, format is: -s ARGS or --long ARGS
  447. } else {
  448. argsDefault = action.dest.toUpperCase();
  449. argsString = self._formatArgs(action, argsDefault);
  450. part = optionString + ' ' + argsString;
  451. }
  452. // make it look optional if it's not required or in a group
  453. if (!action.required && groupActions.indexOf(action) < 0) {
  454. part = '[' + part + ']';
  455. }
  456. // add the action string to the list
  457. parts.push(part);
  458. }
  459. });
  460. // insert things at the necessary indices
  461. for (var i = inserts.length - 1; i >= 0; --i) {
  462. if (inserts[i] !== null) {
  463. parts.splice(i, 0, inserts[i]);
  464. }
  465. }
  466. // join all the action items with spaces
  467. var text = parts.filter(function (part) {
  468. return !!part;
  469. }).join(' ');
  470. // clean up separators for mutually exclusive groups
  471. text = text.replace(/([\[(]) /g, '$1'); // remove spaces
  472. text = text.replace(/ ([\])])/g, '$1');
  473. text = text.replace(/\[ *\]/g, ''); // remove empty groups
  474. text = text.replace(/\( *\)/g, '');
  475. text = text.replace(/\(([^|]*)\)/g, '$1'); // remove () from single action groups
  476. text = _.str.strip(text);
  477. // return the text
  478. return text;
  479. };
  480. HelpFormatter.prototype._formatText = function (text) {
  481. text = _.str.sprintf(text, {prog: this._prog});
  482. var textWidth = this._width - this._currentIndent;
  483. var indentIncriment = _.str.repeat(' ', this._currentIndent);
  484. return this._fillText(text, textWidth, indentIncriment) + $$.EOL + $$.EOL;
  485. };
  486. HelpFormatter.prototype._formatAction = function (action) {
  487. var self = this;
  488. var helpText;
  489. var helpLines;
  490. var parts;
  491. var indentFirst;
  492. // determine the required width and the entry label
  493. var helpPosition = Math.min(this._actionMaxLength + 2, this._maxHelpPosition);
  494. var helpWidth = this._width - helpPosition;
  495. var actionWidth = helpPosition - this._currentIndent - 2;
  496. var actionHeader = this._formatActionInvocation(action);
  497. // no help; start on same line and add a final newline
  498. if (!action.help) {
  499. actionHeader = _.str.repeat(' ', this._currentIndent) + actionHeader + $$.EOL;
  500. // short action name; start on the same line and pad two spaces
  501. } else if (actionHeader.length <= actionWidth) {
  502. actionHeader = _.str.repeat(' ', this._currentIndent) +
  503. actionHeader +
  504. ' ' +
  505. _.str.repeat(' ', actionWidth - actionHeader.length);
  506. indentFirst = 0;
  507. // long action name; start on the next line
  508. } else {
  509. actionHeader = _.str.repeat(' ', this._currentIndent) + actionHeader + $$.EOL;
  510. indentFirst = helpPosition;
  511. }
  512. // collect the pieces of the action help
  513. parts = [actionHeader];
  514. // if there was help for the action, add lines of help text
  515. if (!!action.help) {
  516. helpText = this._expandHelp(action);
  517. helpLines = this._splitLines(helpText, helpWidth);
  518. parts.push(_.str.repeat(' ', indentFirst) + helpLines[0] + $$.EOL);
  519. helpLines.slice(1).forEach(function (line) {
  520. parts.push(_.str.repeat(' ', helpPosition) + line + $$.EOL);
  521. });
  522. // or add a newline if the description doesn't end with one
  523. } else if (actionHeader.charAt(actionHeader.length - 1) !== $$.EOL) {
  524. parts.push($$.EOL);
  525. }
  526. // if there are any sub-actions, add their help as well
  527. if (!!action._getSubactions) {
  528. this._indent();
  529. action._getSubactions().forEach(function (subaction) {
  530. parts.push(self._formatAction(subaction));
  531. });
  532. this._dedent();
  533. }
  534. // return a single string
  535. return this._joinParts(parts);
  536. };
  537. HelpFormatter.prototype._formatActionInvocation = function (action) {
  538. if (!action.isOptional()) {
  539. var format_func = this._metavarFormatter(action, action.dest);
  540. var metavars = format_func(1);
  541. return metavars[0];
  542. } else {
  543. var parts = [];
  544. var argsDefault;
  545. var argsString;
  546. // if the Optional doesn't take a value, format is: -s, --long
  547. if (action.nargs === 0) {
  548. parts = parts.concat(action.optionStrings);
  549. // if the Optional takes a value, format is: -s ARGS, --long ARGS
  550. } else {
  551. argsDefault = action.dest.toUpperCase();
  552. argsString = this._formatArgs(action, argsDefault);
  553. action.optionStrings.forEach(function (optionString) {
  554. parts.push(optionString + ' ' + argsString);
  555. });
  556. }
  557. return parts.join(', ');
  558. }
  559. };
  560. HelpFormatter.prototype._metavarFormatter = function (action, metavarDefault) {
  561. var result;
  562. if (!!action.metavar || action.metavar === '') {
  563. result = action.metavar;
  564. } else if (!!action.choices) {
  565. var choices = action.choices;
  566. if (_.isString(choices)) {
  567. choices = choices.split('').join(', ');
  568. } else if (_.isArray(choices)) {
  569. choices = choices.join(',');
  570. }
  571. else
  572. {
  573. choices = _.keys(choices).join(',');
  574. }
  575. result = '{' + choices + '}';
  576. } else {
  577. result = metavarDefault;
  578. }
  579. return function (size) {
  580. if (Array.isArray(result)) {
  581. return result;
  582. } else {
  583. var metavars = [];
  584. for (var i = 0; i < size; i += 1) {
  585. metavars.push(result);
  586. }
  587. return metavars;
  588. }
  589. };
  590. };
  591. HelpFormatter.prototype._formatArgs = function (action, metavarDefault) {
  592. var result;
  593. var metavars;
  594. var buildMetavar = this._metavarFormatter(action, metavarDefault);
  595. switch (action.nargs) {
  596. case undefined:
  597. case null:
  598. metavars = buildMetavar(1);
  599. result = '' + metavars[0];
  600. break;
  601. case $$.OPTIONAL:
  602. metavars = buildMetavar(1);
  603. result = '[' + metavars[0] + ']';
  604. break;
  605. case $$.ZERO_OR_MORE:
  606. metavars = buildMetavar(2);
  607. result = '[' + metavars[0] + ' [' + metavars[1] + ' ...]]';
  608. break;
  609. case $$.ONE_OR_MORE:
  610. metavars = buildMetavar(2);
  611. result = '' + metavars[0] + ' [' + metavars[1] + ' ...]';
  612. break;
  613. case $$.REMAINDER:
  614. result = '...';
  615. break;
  616. case $$.PARSER:
  617. metavars = buildMetavar(1);
  618. result = metavars[0] + ' ...';
  619. break;
  620. default:
  621. metavars = buildMetavar(action.nargs);
  622. result = metavars.join(' ');
  623. }
  624. return result;
  625. };
  626. HelpFormatter.prototype._expandHelp = function (action) {
  627. var params = { prog: this._prog };
  628. Object.keys(action).forEach(function (actionProperty) {
  629. var actionValue = action[actionProperty];
  630. if (actionValue !== $$.SUPPRESS) {
  631. params[actionProperty] = actionValue;
  632. }
  633. });
  634. if (!!params.choices) {
  635. if (_.isString(params.choices)) {
  636. params.choices = params.choices.split('').join(', ');
  637. }
  638. else if (_.isArray(params.choices)) {
  639. params.choices = params.choices.join(', ');
  640. }
  641. else {
  642. params.choices = _.keys(params.choices).join(', ');
  643. }
  644. }
  645. return _.str.sprintf(this._getHelpString(action), params);
  646. };
  647. HelpFormatter.prototype._splitLines = function (text, width) {
  648. var lines = [];
  649. var delimiters = [" ", ".", ",", "!", "?"];
  650. var re = new RegExp('[' + delimiters.join('') + '][^' + delimiters.join('') + ']*$');
  651. text = text.replace(/[\n\|\t]/g, ' ');
  652. text = _.str.strip(text);
  653. text = text.replace(this._whitespaceMatcher, ' ');
  654. // Wraps the single paragraph in text (a string) so every line
  655. // is at most width characters long.
  656. text.split($$.EOL).forEach(function (line) {
  657. if (width >= line.length) {
  658. lines.push(line);
  659. return;
  660. }
  661. var wrapStart = 0;
  662. var wrapEnd = width;
  663. var delimiterIndex = 0;
  664. while (wrapEnd <= line.length) {
  665. if (wrapEnd !== line.length && delimiters.indexOf(line[wrapEnd] < -1)) {
  666. delimiterIndex = (re.exec(line.substring(wrapStart, wrapEnd)) || {}).index;
  667. wrapEnd = wrapStart + delimiterIndex + 1;
  668. }
  669. lines.push(line.substring(wrapStart, wrapEnd));
  670. wrapStart = wrapEnd;
  671. wrapEnd += width;
  672. }
  673. if (wrapStart < line.length) {
  674. lines.push(line.substring(wrapStart, wrapEnd));
  675. }
  676. });
  677. return lines;
  678. };
  679. HelpFormatter.prototype._fillText = function (text, width, indent) {
  680. var lines = this._splitLines(text, width);
  681. lines = lines.map(function (line) {
  682. return indent + line;
  683. });
  684. return lines.join($$.EOL);
  685. };
  686. HelpFormatter.prototype._getHelpString = function (action) {
  687. return action.help;
  688. };