a zip code crypto-currency system good for red ONLY

formatter.js 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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 sprintf = require('sprintf-js').sprintf;
  16. // Constants
  17. var c = require('../const');
  18. var $$ = require('../utils');
  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 !== c.SUPPRESS) {
  70. var currentIndent = formatter.currentIndent;
  71. heading = $$.repeat(' ', currentIndent) + this._heading + ':' + c.EOL;
  72. }
  73. // join the section-initialize newline, the heading and the help
  74. return formatter._joinParts([ c.EOL, heading, itemHelp, c.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(c.EOL + c.EOL + c.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 !== c.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 !== c.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 !== c.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, c.EOL + c.EOL);
  264. help = $$.trimChars(help, c.EOL) + c.EOL;
  265. }
  266. return help;
  267. };
  268. HelpFormatter.prototype._joinParts = function (partStrings) {
  269. return partStrings.filter(function (part) {
  270. return (part && part !== c.SUPPRESS);
  271. }).join('');
  272. };
  273. HelpFormatter.prototype._formatUsage = function (usage, actions, groups, prefix) {
  274. if (!prefix && typeof prefix !== 'string') {
  275. prefix = 'usage: ';
  276. }
  277. actions = actions || [];
  278. groups = groups || [];
  279. // if usage is specified, use that
  280. if (usage) {
  281. usage = 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. /*eslint-disable func-style*/ // node 0.10 compat
  320. var _getLines = function (parts, indent, prefix) {
  321. var lines = [];
  322. var line = [];
  323. var lineLength = prefix ? prefix.length - 1 : indent.length - 1;
  324. parts.forEach(function (part) {
  325. if (lineLength + 1 + part.length > textWidth) {
  326. lines.push(indent + line.join(' '));
  327. line = [];
  328. lineLength = indent.length - 1;
  329. }
  330. line.push(part);
  331. lineLength += part.length + 1;
  332. });
  333. if (line) {
  334. lines.push(indent + line.join(' '));
  335. }
  336. if (prefix) {
  337. lines[0] = lines[0].substr(indent.length);
  338. }
  339. return lines;
  340. };
  341. var lines, indent, parts;
  342. // if prog is short, follow it with optionals or positionals
  343. if (prefix.length + prog.length <= 0.75 * textWidth) {
  344. indent = $$.repeat(' ', (prefix.length + prog.length + 1));
  345. if (optionalParts) {
  346. lines = [].concat(
  347. _getLines([ prog ].concat(optionalParts), indent, prefix),
  348. _getLines(positionalParts, indent)
  349. );
  350. } else if (positionalParts) {
  351. lines = _getLines([ prog ].concat(positionalParts), indent, prefix);
  352. } else {
  353. lines = [ prog ];
  354. }
  355. // if prog is long, put it on its own line
  356. } else {
  357. indent = $$.repeat(' ', prefix.length);
  358. parts = optionalParts.concat(positionalParts);
  359. lines = _getLines(parts, indent);
  360. if (lines.length > 1) {
  361. lines = [].concat(
  362. _getLines(optionalParts, indent),
  363. _getLines(positionalParts, indent)
  364. );
  365. }
  366. lines = [ prog ].concat(lines);
  367. }
  368. // join lines into usage
  369. usage = lines.join(c.EOL);
  370. }
  371. }
  372. // prefix with 'usage:'
  373. return prefix + usage + c.EOL + c.EOL;
  374. };
  375. HelpFormatter.prototype._formatActionsUsage = function (actions, groups) {
  376. // find group indices and identify actions in groups
  377. var groupActions = [];
  378. var inserts = [];
  379. var self = this;
  380. groups.forEach(function (group) {
  381. var end;
  382. var i;
  383. var start = actions.indexOf(group._groupActions[0]);
  384. if (start >= 0) {
  385. end = start + group._groupActions.length;
  386. //if (actions.slice(start, end) === group._groupActions) {
  387. if ($$.arrayEqual(actions.slice(start, end), group._groupActions)) {
  388. group._groupActions.forEach(function (action) {
  389. groupActions.push(action);
  390. });
  391. if (!group.required) {
  392. if (inserts[start]) {
  393. inserts[start] += ' [';
  394. } else {
  395. inserts[start] = '[';
  396. }
  397. inserts[end] = ']';
  398. } else {
  399. if (inserts[start]) {
  400. inserts[start] += ' (';
  401. } else {
  402. inserts[start] = '(';
  403. }
  404. inserts[end] = ')';
  405. }
  406. for (i = start + 1; i < end; i += 1) {
  407. inserts[i] = '|';
  408. }
  409. }
  410. }
  411. });
  412. // collect all actions format strings
  413. var parts = [];
  414. actions.forEach(function (action, actionIndex) {
  415. var part;
  416. var optionString;
  417. var argsDefault;
  418. var argsString;
  419. // suppressed arguments are marked with None
  420. // remove | separators for suppressed arguments
  421. if (action.help === c.SUPPRESS) {
  422. parts.push(null);
  423. if (inserts[actionIndex] === '|') {
  424. inserts.splice(actionIndex, actionIndex);
  425. } else if (inserts[actionIndex + 1] === '|') {
  426. inserts.splice(actionIndex + 1, actionIndex + 1);
  427. }
  428. // produce all arg strings
  429. } else if (!action.isOptional()) {
  430. part = self._formatArgs(action, action.dest);
  431. // if it's in a group, strip the outer []
  432. if (groupActions.indexOf(action) >= 0) {
  433. if (part[0] === '[' && part[part.length - 1] === ']') {
  434. part = part.slice(1, -1);
  435. }
  436. }
  437. // add the action string to the list
  438. parts.push(part);
  439. // produce the first way to invoke the option in brackets
  440. } else {
  441. optionString = action.optionStrings[0];
  442. // if the Optional doesn't take a value, format is: -s or --long
  443. if (action.nargs === 0) {
  444. part = '' + optionString;
  445. // if the Optional takes a value, format is: -s ARGS or --long ARGS
  446. } else {
  447. argsDefault = action.dest.toUpperCase();
  448. argsString = self._formatArgs(action, argsDefault);
  449. part = optionString + ' ' + argsString;
  450. }
  451. // make it look optional if it's not required or in a group
  452. if (!action.required && groupActions.indexOf(action) < 0) {
  453. part = '[' + part + ']';
  454. }
  455. // add the action string to the list
  456. parts.push(part);
  457. }
  458. });
  459. // insert things at the necessary indices
  460. for (var i = inserts.length - 1; i >= 0; --i) {
  461. if (inserts[i] !== null) {
  462. parts.splice(i, 0, inserts[i]);
  463. }
  464. }
  465. // join all the action items with spaces
  466. var text = parts.filter(function (part) {
  467. return !!part;
  468. }).join(' ');
  469. // clean up separators for mutually exclusive groups
  470. text = text.replace(/([\[(]) /g, '$1'); // remove spaces
  471. text = text.replace(/ ([\])])/g, '$1');
  472. text = text.replace(/\[ *\]/g, ''); // remove empty groups
  473. text = text.replace(/\( *\)/g, '');
  474. text = text.replace(/\(([^|]*)\)/g, '$1'); // remove () from single action groups
  475. text = text.trim();
  476. // return the text
  477. return text;
  478. };
  479. HelpFormatter.prototype._formatText = function (text) {
  480. text = sprintf(text, { prog: this._prog });
  481. var textWidth = this._width - this._currentIndent;
  482. var indentIncriment = $$.repeat(' ', this._currentIndent);
  483. return this._fillText(text, textWidth, indentIncriment) + c.EOL + c.EOL;
  484. };
  485. HelpFormatter.prototype._formatAction = function (action) {
  486. var self = this;
  487. var helpText;
  488. var helpLines;
  489. var parts;
  490. var indentFirst;
  491. // determine the required width and the entry label
  492. var helpPosition = Math.min(this._actionMaxLength + 2, this._maxHelpPosition);
  493. var helpWidth = this._width - helpPosition;
  494. var actionWidth = helpPosition - this._currentIndent - 2;
  495. var actionHeader = this._formatActionInvocation(action);
  496. // no help; start on same line and add a final newline
  497. if (!action.help) {
  498. actionHeader = $$.repeat(' ', this._currentIndent) + actionHeader + c.EOL;
  499. // short action name; start on the same line and pad two spaces
  500. } else if (actionHeader.length <= actionWidth) {
  501. actionHeader = $$.repeat(' ', this._currentIndent) +
  502. actionHeader +
  503. ' ' +
  504. $$.repeat(' ', actionWidth - actionHeader.length);
  505. indentFirst = 0;
  506. // long action name; start on the next line
  507. } else {
  508. actionHeader = $$.repeat(' ', this._currentIndent) + actionHeader + c.EOL;
  509. indentFirst = helpPosition;
  510. }
  511. // collect the pieces of the action help
  512. parts = [ actionHeader ];
  513. // if there was help for the action, add lines of help text
  514. if (action.help) {
  515. helpText = this._expandHelp(action);
  516. helpLines = this._splitLines(helpText, helpWidth);
  517. parts.push($$.repeat(' ', indentFirst) + helpLines[0] + c.EOL);
  518. helpLines.slice(1).forEach(function (line) {
  519. parts.push($$.repeat(' ', helpPosition) + line + c.EOL);
  520. });
  521. // or add a newline if the description doesn't end with one
  522. } else if (actionHeader.charAt(actionHeader.length - 1) !== c.EOL) {
  523. parts.push(c.EOL);
  524. }
  525. // if there are any sub-actions, add their help as well
  526. if (action._getSubactions) {
  527. this._indent();
  528. action._getSubactions().forEach(function (subaction) {
  529. parts.push(self._formatAction(subaction));
  530. });
  531. this._dedent();
  532. }
  533. // return a single string
  534. return this._joinParts(parts);
  535. };
  536. HelpFormatter.prototype._formatActionInvocation = function (action) {
  537. if (!action.isOptional()) {
  538. var format_func = this._metavarFormatter(action, action.dest);
  539. var metavars = format_func(1);
  540. return metavars[0];
  541. }
  542. var parts = [];
  543. var argsDefault;
  544. var argsString;
  545. // if the Optional doesn't take a value, format is: -s, --long
  546. if (action.nargs === 0) {
  547. parts = parts.concat(action.optionStrings);
  548. // if the Optional takes a value, format is: -s ARGS, --long ARGS
  549. } else {
  550. argsDefault = action.dest.toUpperCase();
  551. argsString = this._formatArgs(action, argsDefault);
  552. action.optionStrings.forEach(function (optionString) {
  553. parts.push(optionString + ' ' + argsString);
  554. });
  555. }
  556. return parts.join(', ');
  557. };
  558. HelpFormatter.prototype._metavarFormatter = function (action, metavarDefault) {
  559. var result;
  560. if (action.metavar || action.metavar === '') {
  561. result = action.metavar;
  562. } else if (action.choices) {
  563. var choices = action.choices;
  564. if (typeof choices === 'string') {
  565. choices = choices.split('').join(', ');
  566. } else if (Array.isArray(choices)) {
  567. choices = choices.join(',');
  568. } else {
  569. choices = Object.keys(choices).join(',');
  570. }
  571. result = '{' + choices + '}';
  572. } else {
  573. result = metavarDefault;
  574. }
  575. return function (size) {
  576. if (Array.isArray(result)) {
  577. return result;
  578. }
  579. var metavars = [];
  580. for (var i = 0; i < size; i += 1) {
  581. metavars.push(result);
  582. }
  583. return metavars;
  584. };
  585. };
  586. HelpFormatter.prototype._formatArgs = function (action, metavarDefault) {
  587. var result;
  588. var metavars;
  589. var buildMetavar = this._metavarFormatter(action, metavarDefault);
  590. switch (action.nargs) {
  591. /*eslint-disable no-undefined*/
  592. case undefined:
  593. case null:
  594. metavars = buildMetavar(1);
  595. result = '' + metavars[0];
  596. break;
  597. case c.OPTIONAL:
  598. metavars = buildMetavar(1);
  599. result = '[' + metavars[0] + ']';
  600. break;
  601. case c.ZERO_OR_MORE:
  602. metavars = buildMetavar(2);
  603. result = '[' + metavars[0] + ' [' + metavars[1] + ' ...]]';
  604. break;
  605. case c.ONE_OR_MORE:
  606. metavars = buildMetavar(2);
  607. result = '' + metavars[0] + ' [' + metavars[1] + ' ...]';
  608. break;
  609. case c.REMAINDER:
  610. result = '...';
  611. break;
  612. case c.PARSER:
  613. metavars = buildMetavar(1);
  614. result = metavars[0] + ' ...';
  615. break;
  616. default:
  617. metavars = buildMetavar(action.nargs);
  618. result = metavars.join(' ');
  619. }
  620. return result;
  621. };
  622. HelpFormatter.prototype._expandHelp = function (action) {
  623. var params = { prog: this._prog };
  624. Object.keys(action).forEach(function (actionProperty) {
  625. var actionValue = action[actionProperty];
  626. if (actionValue !== c.SUPPRESS) {
  627. params[actionProperty] = actionValue;
  628. }
  629. });
  630. if (params.choices) {
  631. if (typeof params.choices === 'string') {
  632. params.choices = params.choices.split('').join(', ');
  633. } else if (Array.isArray(params.choices)) {
  634. params.choices = params.choices.join(', ');
  635. } else {
  636. params.choices = Object.keys(params.choices).join(', ');
  637. }
  638. }
  639. return sprintf(this._getHelpString(action), params);
  640. };
  641. HelpFormatter.prototype._splitLines = function (text, width) {
  642. var lines = [];
  643. var delimiters = [ ' ', '.', ',', '!', '?' ];
  644. var re = new RegExp('[' + delimiters.join('') + '][^' + delimiters.join('') + ']*$');
  645. text = text.replace(/[\n\|\t]/g, ' ');
  646. text = text.trim();
  647. text = text.replace(this._whitespaceMatcher, ' ');
  648. // Wraps the single paragraph in text (a string) so every line
  649. // is at most width characters long.
  650. text.split(c.EOL).forEach(function (line) {
  651. if (width >= line.length) {
  652. lines.push(line);
  653. return;
  654. }
  655. var wrapStart = 0;
  656. var wrapEnd = width;
  657. var delimiterIndex = 0;
  658. while (wrapEnd <= line.length) {
  659. if (wrapEnd !== line.length && delimiters.indexOf(line[wrapEnd] < -1)) {
  660. delimiterIndex = (re.exec(line.substring(wrapStart, wrapEnd)) || {}).index;
  661. wrapEnd = wrapStart + delimiterIndex + 1;
  662. }
  663. lines.push(line.substring(wrapStart, wrapEnd));
  664. wrapStart = wrapEnd;
  665. wrapEnd += width;
  666. }
  667. if (wrapStart < line.length) {
  668. lines.push(line.substring(wrapStart, wrapEnd));
  669. }
  670. });
  671. return lines;
  672. };
  673. HelpFormatter.prototype._fillText = function (text, width, indent) {
  674. var lines = this._splitLines(text, width);
  675. lines = lines.map(function (line) {
  676. return indent + line;
  677. });
  678. return lines.join(c.EOL);
  679. };
  680. HelpFormatter.prototype._getHelpString = function (action) {
  681. return action.help;
  682. };