Skip to content
Myr

Added support for custom --help output

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

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent fad6200 · Browse files · 3 files+98 added−0 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+98 added−0 removed

  • other:+98 added−0 removed

3 files+98 added−0 removed

added:examples/custom-help+32 added
1#!/usr/bin/env node23/**4 * Module dependencies.5 */67var program = require('../');89function list(val) {10 return val.split(',').map(Number);11}1213program14 .version('0.0.1')15 .option('-f, --foo', 'enable some foo')16 .option('-b, --bar', 'enable some bar')17 .option('-B, --baz', 'enable some baz');1819// must be before .parse() since20// node's emit() is immediate2122program.on('--help', function(){23 console.log(' Examples:');24 console.log('');25 console.log(' $ custom-help --help');26 console.log(' $ custom-help -h');27 console.log('');28});2930program.parse(process.argv);3132console.log('stuff');
modified:lib/commander.js+1 added
886 unmodified lines
887888exports.option('-h, --help', 'output usage information');889exports.on('help', function(){890 process.stdout.write(this.helpInformation());891 exports.emit('--help');892 process.exit(0);893});
modified:Readme.md+65 added
84 unmodified lines
85console.log(' list: %j', program.list);86console.log(' args: %j', program.args);87```8889## Custom help9091 You can display arbitrary `-h, --help` information92 by listening for "--help". Commander will automatically93 exit once you are done so that the remainder of your program94 does not execute causing undesired behaviours, for example95 in the following executable "stuff" will not output when96 `--help` is used.9798```js99#!/usr/bin/env node100101/**102 * Module dependencies.103 */104105var program = require('../');106107function list(val) {108 return val.split(',').map(Number);109}110111program112 .version('0.0.1')113 .option('-f, --foo', 'enable some foo')114 .option('-b, --bar', 'enable some bar')115 .option('-B, --baz', 'enable some baz');116117// must be before .parse() since118// node's emit() is immediate119120program.on('--help', function(){121 console.log(' Examples:');122 console.log('');123 console.log(' $ custom-help --help');124 console.log(' $ custom-help -h');125 console.log('');126});127128program.parse(process.argv);129130console.log('stuff');131```132133yielding the following help output:134135```136137Usage: custom-help [options]138139Options:140141 -h, --help output usage information142 -v, --version output the version number143 -f, --foo enable some foo144 -b, --bar enable some bar145 -B, --baz enable some baz146147Examples:148149 $ custom-help --help150 $ custom-help -h151152```153154## .prompt(msg, fn)155156 Single-line prompt:157
102 unmodified lines