Skip to content
Myr

add callback to allow filtering or post-processing of help text

Before adoption: this commit came in with the history the project adopted, so no landing records it.

added a callback to allow auto-generated help text to be filtered or post-processed. for example, i want to apply colors to the text before it is displayed in the console. add callback to allow filtering or post-processing of help text fix brace alignment in example make brace alignment in example consistent with other code add callback to allow filtering or post-processing of help text added a callback to allow auto-generated help text to be filtered or post-processed. for example, i want to apply colors to the text before it is displayed in the console. add callback to allow filtering or post-processing of help text fix brace alignment in example make brace alignment in example consistent with other code

authored by djulien · committed by SomeKittens · parent 0af40a5 · Browse files · 3 files+16 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+16 added−8 removed

  • other:+16 added−8 removed

3 files+16 added−8 removed

modified:index.js+5 added-4 removed
1015 unmodified lines
1016 *1017 * @api public1018 */10191020Command.prototype.outputHelp = function() {1021 process.stdout.write(this.helpInformation());1020Command.prototype.outputHelp = function(cb) {1021 if (!cb) cb = function(passthru) { return passthru; } //supply benign callback if none specified1022 process.stdout.write(cb(this.helpInformation()));1023 this.emit('--help');1024};10251026/**
1 unmodified line
1028 *1029 * @api public1030 */10311031Command.prototype.help = function() {1032 this.outputHelp();1032Command.prototype.help = function(cb) {1033 this.outputHelp(cb);1034 process.exit();1035};10361037/**
72 unmodified lines
modified:package.json+1 added-1 removed
modified:Readme.md+10 added-3 removed
266 unmodified lines
267 $ custom-help -h268269```270271## .outputHelp()271## .outputHelp(cb)272273Output help information without exiting.274Optional callback cb allows post-processing of help text before it is displayed.275276If you want to display help by default (e.g. if no command was provided), you can use something like:277278```js279var program = require('commander');280var colors = require('colors');281282program283 .version('0.0.1')284 .command('getstream [url]', 'get stream URL')285 .parse(process.argv);286287 if (!process.argv.slice(2).length) {286 program.outputHelp();288 program.outputHelp(make_red);289 }290291function make_red(txt) {292 return colors.red(txt); //display the help text in red on the console293}294```295290## .help()296## .help(cb)297298 Output help information and exit immediately.299 Optional callback cb allows post-processing of help text before it is displayed.300301## Examples302303```js
48 unmodified lines