Anything you wanted to do with --help and --version:
- checks these arguments in
process.argv, so you don't have to; - extracts
versionfrom yourpackage.jsonfile; - returns functions that give you both
--helpand--version, in case you need to handle them differently; - returns the function that prints
--helpand exits with the given code; - highly configurable: you can set
process.argv,stdoutandstderrstreams, and evenprocess.exitfunction — this means testing won't be a problem.
#!/usr/bin/env node
var Cli = require('help-version');
var cli = Cli('Usage: my-cat [file]');
cli.version()
//=> "v0.1.0"
if (process.argv.length != 3) {
// Show help and exit with code 1.
cli.help(1);
}
fs.createReadStream(process.argv[2])
.pipe(process.stdout);Catches --help and --version automatically.
$ ./cat.js --help
Usage: my-cat [file]
$ ./cat.js --version
v0.1.0
$ ./cat.js file.txt
contents of file.txt
helpText{String | Function} — help text to print on--help, or function to produce it.
Checks opts.argv for --help or --version.
-
If
--helpis found, printshelpTexttoopts.stdoutand callsopts.exit. -
If
--versionis found, prints app version (determined from theversionfield from your localpackage.json) toopts.stdoutand callsopts.exit.
Returns object with two (bound) methods: cli.help([code], [stream]) and cli.version([code], [stream]).
| Option | Default |
|---|---|
argv |
process.argv.slice(2) |
exit([code]) |
process.exit |
stdout |
process.stdout |
stderr |
process.stderr |
With no arguments, returns the help string.
With one or two arguments, writes it to stream instead and exits (via opts.exit) with code. stream defaults to opts.stdout if code==0 and opts.stderr otherwise.
With no arguments, returns the version string.
With one or two arguments, writes it to the stream instead and exits (via opts.exit) with code. stream defaults to opts.stdout if code==0 and opts.stderr otherwise.
npm install help-version
MIT

