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
Provenance
- Commit
da13783- Tree
36b3d45- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
fad6200
3 files changed+98 added−0 removed
added:examples/custom-help+32 added
1+#!/usr/bin/env node2+3+/**4+ * Module dependencies.5+ */6+7+var program = require('../');8+9+function list(val) {10+ return val.split(',').map(Number);11+}12+13+program14+ .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');18+19+// must be before .parse() since20+// node's emit() is immediate21+22+program.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+});29+30+program.parse(process.argv);31+32+console.log('stuff');modified:lib/commander.js+1 added
886 unmodified lines887 888 exports.option('-h, --help', 'output usage information');889 exports.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 lines85 console.log(' list: %j', program.list);86 console.log(' args: %j', program.args);87 ```88 89+## Custom help90+91+ 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.97+98+```js99+#!/usr/bin/env node100+101+/**102+ * Module dependencies.103+ */104+105+var program = require('../');106+107+function list(val) {108+ return val.split(',').map(Number);109+}110+111+program112+ .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');116+117+// must be before .parse() since118+// node's emit() is immediate119+120+program.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+});127+128+program.parse(process.argv);129+130+console.log('stuff');131+```132+133+yielding the following help output:134+135+```136+137+Usage: custom-help [options]138+139+Options:140+141+ -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 baz146+147+Examples:148+149+ $ custom-help --help150+ $ custom-help -h151+152+```153+154 ## .prompt(msg, fn)155 156 Single-line prompt:157 102 unmodified lines