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