Skip to content

Commit

Permalink
Added support for custom --help output
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Aug 24, 2011
1 parent fad6200 commit da13783
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
65 changes: 65 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,71 @@ console.log(' list: %j', program.list);
console.log(' args: %j', program.args);
```

## Custom help

You can display arbitrary `-h, --help` information
by listening for "--help". Commander will automatically
exit once you are done so that the remainder of your program
does not execute causing undesired behaviours, for example
in the following executable "stuff" will not output when
`--help` is used.

```js
#!/usr/bin/env node

/**
* Module dependencies.
*/

var program = require('../');

function list(val) {
return val.split(',').map(Number);
}

program
.version('0.0.1')
.option('-f, --foo', 'enable some foo')
.option('-b, --bar', 'enable some bar')
.option('-B, --baz', 'enable some baz');

// must be before .parse() since
// node's emit() is immediate

program.on('--help', function(){
console.log(' Examples:');
console.log('');
console.log(' $ custom-help --help');
console.log(' $ custom-help -h');
console.log('');
});

program.parse(process.argv);

console.log('stuff');
```

yielding the following help output:

```
Usage: custom-help [options]
Options:
-h, --help output usage information
-v, --version output the version number
-f, --foo enable some foo
-b, --bar enable some bar
-B, --baz enable some baz
Examples:
$ custom-help --help
$ custom-help -h
```

## .prompt(msg, fn)

Single-line prompt:
Expand Down
32 changes: 32 additions & 0 deletions examples/custom-help
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var program = require('../');

function list(val) {
return val.split(',').map(Number);
}

program
.version('0.0.1')
.option('-f, --foo', 'enable some foo')
.option('-b, --bar', 'enable some bar')
.option('-B, --baz', 'enable some baz');

// must be before .parse() since
// node's emit() is immediate

program.on('--help', function(){
console.log(' Examples:');
console.log('');
console.log(' $ custom-help --help');
console.log(' $ custom-help -h');
console.log('');
});

program.parse(process.argv);

console.log('stuff');
1 change: 1 addition & 0 deletions lib/commander.js
Original file line number Diff line number Diff line change
Expand Up @@ -888,5 +888,6 @@ function pad(str, width) {
exports.option('-h, --help', 'output usage information');
exports.on('help', function(){
process.stdout.write(this.helpInformation());
exports.emit('--help');
process.exit(0);
});

0 comments on commit da13783

Please sign in to comment.