help-version
Advanced tools
Comparing version 0.2.0 to 1.0.0
58
index.js
'use strict'; | ||
var callsites = require('callsites'), | ||
findRoot = require('find-root'); | ||
var getVersion = require('./lib/get-version'), | ||
findOption = require('./lib/find-option'); | ||
var path = require('path'); | ||
var callsites = require('callsites'); | ||
@@ -14,3 +14,3 @@ | ||
} | ||
(stream || process.stdout).write(data); | ||
(stream || this.stdout).write(data); | ||
}; | ||
@@ -25,37 +25,41 @@ | ||
if (stream == null) { | ||
stream = !code ? process.stdout : process.stderr; | ||
stream = !code ? this.stdout : this.stderr; | ||
} | ||
write(data, stream); | ||
process.exit(code); | ||
}; | ||
write.call(this, data, stream); | ||
this.exit(code); | ||
}.bind(this); | ||
}; | ||
var getVersion = function (caller) { | ||
var pkgPath = path.join(findRoot(caller), 'package.json'); | ||
return require(pkgPath).version; | ||
}; | ||
module.exports = function (help, opts) { | ||
opts = opts || {}; | ||
// Copy to a separate object to accomodate for passing `process` object with | ||
// getters. | ||
var options = { | ||
argv: opts.argv || process.argv.slice(2), | ||
exit: opts.exit || process.exit, | ||
stdout: opts.stdout || process.stdout, | ||
stderr: opts.stderr || process.stderr | ||
}; | ||
module.exports = function (help, version) { | ||
if (version == null) { | ||
var caller = callsites()[1].getFileName(); | ||
version = 'v' + getVersion(caller); | ||
} | ||
var caller = callsites()[1].getFileName(); | ||
var version = 'v' + getVersion(caller); | ||
var argv = process.argv.slice(2); | ||
switch (findOption(options.argv, ['--help', '--version'])) { | ||
case '--help': | ||
write.call(options, help); | ||
options.exit(); | ||
break; | ||
if (argv == '--help') { | ||
write(help); | ||
process.exit(); | ||
case '--version': | ||
write.call(options, version); | ||
options.exit(); | ||
break; | ||
} | ||
if (argv == '--version') { | ||
write(version); | ||
process.exit(); | ||
} | ||
return { | ||
help: maybeExit(help), | ||
version: maybeExit(version) | ||
help: maybeExit.call(options, help), | ||
version: maybeExit.call(options, version) | ||
}; | ||
}; |
{ | ||
"name": "help-version", | ||
"version": "0.2.0", | ||
"version": "1.0.0", | ||
"description": "Easily handle --help and --version arguments in your CLI application", | ||
@@ -11,3 +11,4 @@ "author": "Eugene Sharygin <eush77@gmail.com>", | ||
"files": [ | ||
"index.js" | ||
"index.js", | ||
"lib/" | ||
], | ||
@@ -28,3 +29,6 @@ "homepage": "https://github.com/eush77/help-version", | ||
"argument", | ||
"usage" | ||
"usage", | ||
"options", | ||
"application", | ||
"program" | ||
], | ||
@@ -38,4 +42,5 @@ "dependencies": { | ||
"minimist": "^1.1.1", | ||
"tape": "^4.0.0" | ||
"tape": "^4.0.0", | ||
"through2": "^2.0.0" | ||
} | ||
} |
@@ -22,3 +22,4 @@ [![npm](https://nodei.co/npm/help-version.png)](https://nodei.co/npm/help-version/) | ||
function usage() { | ||
return 'Usage: my-app [file]'; | ||
// Makes use of function declarations hoisting. | ||
return 'Usage: my-cat [file]'; | ||
} | ||
@@ -29,3 +30,10 @@ | ||
console.log('main thing'); | ||
if (process.argv.length != 3) { | ||
// Shows help and exits with code 1. | ||
helpVersion.help(1); | ||
} | ||
// Main thing. | ||
fs.createReadStream(process.argv[2]) | ||
.pipe(process.stdout); | ||
``` | ||
@@ -36,8 +44,8 @@ | ||
``` | ||
$ ./app.js --help | ||
Usage: my-app [file] | ||
$ ./app.js --version | ||
$ ./cat.js --help | ||
Usage: my-cat [file] | ||
$ ./cat.js --version | ||
v0.1.0 | ||
$ ./app.js | ||
main thing | ||
$ ./cat.js file.txt | ||
contents of file.txt | ||
``` | ||
@@ -47,18 +55,31 @@ | ||
### `helpVersion = require('help-version')(help, [version])` | ||
### `helpVersion = require('help-version')(helpText, [opts])` | ||
Checks `process.argv` for `--help` or `--version`, prints `help` or `version` if found one. | ||
Checks `opts.argv` for `--help` or `--version`. | ||
`version` defaults to `version` field from your local `package.json`. | ||
1. If `--help` is found, prints `helpText` to `opts.stdout` and calls `opts.exit`. | ||
2. If `--version` is found, prints app version (determined from the `version` field from your local `package.json`) to `opts.stdout` and calls `opts.exit`. | ||
Returns object with two (bound) methods: `helpVersion.help([code], [stream])` and `helpVersion.version([code], [stream])`. | ||
| Option | Default | | ||
| :------------: | :---------------------: | | ||
| `argv` | `process.argv.slice(2)` | | ||
| `exit([code])` | `process.exit` | | ||
| `stdout` | `process.stdout` | | ||
| `stderr` | `process.stderr` | | ||
### `helpVersion.help([code], [stream])` | ||
With no arguments, returns the `help` string. | ||
With no arguments, returns the help string. | ||
With one or two arguments, writes `help` to the `stream` and exits with `code`. `stream` defaults to `process.stdout` if `code==0` and `process.stderr` otherwise. | ||
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. | ||
### `helpVersion.version([code], [stream])` | ||
Returns `version` string or writes it to `stream` and exits. | ||
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. | ||
## Install | ||
@@ -65,0 +86,0 @@ |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
6459
6
76
0
90
4