added_formatters.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. 'use strict';
  2. var util = require('util');
  3. // Constants
  4. var c = require('../const');
  5. var $$ = require('../utils');
  6. var HelpFormatter = require('./formatter.js');
  7. /**
  8. * new RawDescriptionHelpFormatter(options)
  9. * new ArgumentParser({formatterClass: argparse.RawDescriptionHelpFormatter, ...})
  10. *
  11. * Help message formatter which adds default values to argument help.
  12. *
  13. * Only the name of this class is considered a public API. All the methods
  14. * provided by the class are considered an implementation detail.
  15. **/
  16. function ArgumentDefaultsHelpFormatter(options) {
  17. HelpFormatter.call(this, options);
  18. }
  19. util.inherits(ArgumentDefaultsHelpFormatter, HelpFormatter);
  20. ArgumentDefaultsHelpFormatter.prototype._getHelpString = function (action) {
  21. var help = action.help;
  22. if (action.help.indexOf('%(defaultValue)s') === -1) {
  23. if (action.defaultValue !== c.SUPPRESS) {
  24. var defaulting_nargs = [ c.OPTIONAL, c.ZERO_OR_MORE ];
  25. if (action.isOptional() || (defaulting_nargs.indexOf(action.nargs) >= 0)) {
  26. help += ' (default: %(defaultValue)s)';
  27. }
  28. }
  29. }
  30. return help;
  31. };
  32. module.exports.ArgumentDefaultsHelpFormatter = ArgumentDefaultsHelpFormatter;
  33. /**
  34. * new RawDescriptionHelpFormatter(options)
  35. * new ArgumentParser({formatterClass: argparse.RawDescriptionHelpFormatter, ...})
  36. *
  37. * Help message formatter which retains any formatting in descriptions.
  38. *
  39. * Only the name of this class is considered a public API. All the methods
  40. * provided by the class are considered an implementation detail.
  41. **/
  42. function RawDescriptionHelpFormatter(options) {
  43. HelpFormatter.call(this, options);
  44. }
  45. util.inherits(RawDescriptionHelpFormatter, HelpFormatter);
  46. RawDescriptionHelpFormatter.prototype._fillText = function (text, width, indent) {
  47. var lines = text.split('\n');
  48. lines = lines.map(function (line) {
  49. return $$.trimEnd(indent + line);
  50. });
  51. return lines.join('\n');
  52. };
  53. module.exports.RawDescriptionHelpFormatter = RawDescriptionHelpFormatter;
  54. /**
  55. * new RawTextHelpFormatter(options)
  56. * new ArgumentParser({formatterClass: argparse.RawTextHelpFormatter, ...})
  57. *
  58. * Help message formatter which retains formatting of all help text.
  59. *
  60. * Only the name of this class is considered a public API. All the methods
  61. * provided by the class are considered an implementation detail.
  62. **/
  63. function RawTextHelpFormatter(options) {
  64. RawDescriptionHelpFormatter.call(this, options);
  65. }
  66. util.inherits(RawTextHelpFormatter, RawDescriptionHelpFormatter);
  67. RawTextHelpFormatter.prototype._splitLines = function (text) {
  68. return text.split('\n');
  69. };
  70. module.exports.RawTextHelpFormatter = RawTextHelpFormatter;