simple-bin-help
Advanced tools
Comparing version 1.2.0 to 1.3.0
97
index.js
@@ -1,31 +0,55 @@ | ||
function noArguments(minLength, args) { | ||
console.assert(Array.isArray(args), 'missing arguments'); | ||
return args.length < minLength; | ||
'use strict' | ||
const updateNotifier = require('update-notifier') | ||
function isHelp (arg) { | ||
return arg === '-h' || | ||
arg === '--help' | ||
} | ||
function showHelp(options) { | ||
var helpMessage = options.help || options.helpMessage; | ||
function hasHelpArgument (args) { | ||
return args.some(isHelp) | ||
} | ||
var pkg; | ||
if (options.package) { | ||
pkg = options.package; | ||
} else if (options.packagePath) { | ||
pkg = require(options.packagePath); | ||
function noArguments (minLength, args) { | ||
console.assert(Array.isArray(args), 'missing arguments') | ||
return args.length < minLength | ||
} | ||
function getPackage (options) { | ||
var pkg = options.pkg || options.package | ||
if (!pkg && options.packagePath) { | ||
pkg = require(options.packagePath) | ||
} | ||
var pkgInfo; | ||
return pkg | ||
} | ||
function showHelp (options) { | ||
var helpMessage = options.help || options.helpMessage | ||
var pkg = getPackage(options) | ||
var pkgInfo | ||
if (pkg) { | ||
pkgInfo = pkg.name + '@' + pkg.version + '\n - ' + pkg.description; | ||
pkgInfo = pkg.name ? pkg.name : '' | ||
if (pkg.version) { | ||
pkgInfo += '@' + pkg.version | ||
} | ||
if (pkg.description) { | ||
pkgInfo += '\n - ' + pkg.description | ||
} | ||
} | ||
if (pkgInfo) { | ||
console.log(pkgInfo); | ||
console.log(pkgInfo) | ||
} | ||
if (helpMessage) { | ||
console.log(helpMessage); | ||
console.log(helpMessage) | ||
} | ||
if (!pkgInfo && !helpMessage) { | ||
console.log('Incorrect CLI arguments'); | ||
console.log('Incorrect CLI arguments') | ||
if (pkg && pkg.name) { | ||
console.log('Check out instructions for this module using `npm home ' + pkg.name + '`'); | ||
console.log('Check out instructions for this module using `npm home ' + pkg.name + '`') | ||
} | ||
@@ -35,23 +59,44 @@ } | ||
function simpleBinHelp(options, cliArguments) { | ||
console.assert(options, 'missing options'); | ||
function finish (options) { | ||
if (options.noExit) { | ||
return false | ||
} | ||
process.exit(0) | ||
} | ||
function simpleBinHelp (options, cliArguments) { | ||
console.assert(options, 'missing options') | ||
if (!cliArguments) { | ||
cliArguments = process.argv; | ||
cliArguments = process.argv | ||
} | ||
if (hasHelpArgument(cliArguments)) { | ||
showHelp(options) | ||
finish(options) | ||
return true | ||
} | ||
var pkg = getPackage(options) | ||
if (pkg && pkg.name && pkg.version) { | ||
updateNotifier({ pkg: pkg }).notify() | ||
} | ||
var minArguments = options.minArguments || | ||
options.min || | ||
options.n; | ||
options.n | ||
if (noArguments(minArguments, cliArguments)) { | ||
showHelp(options); | ||
if (options.noExit) { | ||
return false; | ||
showHelp(options) | ||
if (typeof options.onFail === 'function') { | ||
options.onFail() | ||
} | ||
process.exit(0); | ||
return finish(options) | ||
} | ||
return true; | ||
return true | ||
} | ||
module.exports = simpleBinHelp; | ||
module.exports = simpleBinHelp |
@@ -5,9 +5,11 @@ { | ||
"main": "index.js", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"scripts": { | ||
"test": "mocha spec", | ||
"test": "mocha --harmony spec", | ||
"semantic-release": "semantic-release pre && npm publish && semantic-release post", | ||
"commit": "git-issues && commit-wizard", | ||
"issues": "git-issues", | ||
"size": "t=\"$(npm pack .)\"; wc -c \"${t}\"; tar tvf \"${t}\"; rm \"${t}\";" | ||
"size": "t=\"$(npm pack .)\"; wc -c \"${t}\"; tar tvf \"${t}\"; rm \"${t}\";", | ||
"lint": "standard *.js spec/*.js examples/*.js", | ||
"basic": "node --harmony examples/basic.js" | ||
}, | ||
@@ -43,3 +45,4 @@ "files": [ | ||
"pre-git": "1.4.0", | ||
"semantic-release": "4.3.5" | ||
"semantic-release": "4.3.5", | ||
"standard": "5.4.1" | ||
}, | ||
@@ -50,3 +53,5 @@ "config": { | ||
"pre-commit": [ | ||
"npm test" | ||
"npm run lint", | ||
"npm test", | ||
"npm run basic" | ||
], | ||
@@ -59,3 +64,6 @@ "pre-push": [ | ||
} | ||
}, | ||
"dependencies": { | ||
"update-notifier": "0.5.0" | ||
} | ||
} |
@@ -11,2 +11,3 @@ # simple-bin-help | ||
[![semantic-release][semantic-image] ][semantic-url] | ||
[![standard](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/) | ||
@@ -41,4 +42,5 @@ ## Install | ||
minArguments: n, // min number of arguments to check | ||
pkg: packageObject, // package object or path for better message | ||
packagePath: 'path/to/package.json' | ||
pkg: packageObject, // package (or pkg) object or path for better message | ||
packagePath: 'path/to/package.json', | ||
onFail: fn // user-supplied function to be called after help has been shown | ||
}; | ||
@@ -51,2 +53,10 @@ require('simple-bin-help')(options); | ||
For more examples, see [examples/basic.js](examples/basic.js) file that calls the method | ||
with several permutations. | ||
## Bonus features | ||
* Includes and calls the [update-notifier]() module by default. | ||
* If passed `-h` or `--help` option, shows the help message. | ||
### Small print | ||
@@ -53,0 +63,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7761
79
109
1
7
+ Addedupdate-notifier@0.5.0
+ Addedansi-regex@2.1.1(transitive)
+ Addedansi-styles@2.2.1(transitive)
+ Addedchalk@1.1.3(transitive)
+ Addedconfigstore@1.4.0(transitive)
+ Addedcore-util-is@1.0.3(transitive)
+ Addeddeep-extend@0.6.0(transitive)
+ Addedduplexify@3.7.1(transitive)
+ Addedend-of-stream@1.4.4(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedgot@3.3.1(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedhas-ansi@2.0.0(transitive)
+ Addedimurmurhash@0.1.4(transitive)
+ Addedinfinity-agent@2.0.3(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedini@1.3.8(transitive)
+ Addedis-finite@1.1.0(transitive)
+ Addedis-npm@1.0.0(transitive)
+ Addedis-redirect@1.0.0(transitive)
+ Addedis-stream@1.1.0(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedlatest-version@1.0.1(transitive)
+ Addedlowercase-keys@1.0.1(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedmkdirp@0.5.6(transitive)
+ Addednested-error-stacks@1.0.2(transitive)
+ Addedobject-assign@3.0.04.1.1(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedos-homedir@1.0.2(transitive)
+ Addedos-tmpdir@1.0.2(transitive)
+ Addedosenv@0.1.5(transitive)
+ Addedpackage-json@1.2.0(transitive)
+ Addedpinkie@2.0.4(transitive)
+ Addedpinkie-promise@2.0.1(transitive)
+ Addedprepend-http@1.0.4(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedrc@1.2.8(transitive)
+ Addedread-all-stream@3.1.0(transitive)
+ Addedreadable-stream@2.3.8(transitive)
+ Addedregistry-url@3.1.0(transitive)
+ Addedrepeating@1.1.3(transitive)
+ Addedsafe-buffer@5.1.2(transitive)
+ Addedsemver@5.7.2(transitive)
+ Addedsemver-diff@2.1.0(transitive)
+ Addedslide@1.1.6(transitive)
+ Addedstream-shift@1.0.3(transitive)
+ Addedstring-length@1.0.1(transitive)
+ Addedstring_decoder@1.1.1(transitive)
+ Addedstrip-ansi@3.0.1(transitive)
+ Addedstrip-json-comments@2.0.1(transitive)
+ Addedsupports-color@2.0.0(transitive)
+ Addedtimed-out@2.0.0(transitive)
+ Addedupdate-notifier@0.5.0(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addeduuid@2.0.3(transitive)
+ Addedwrappy@1.0.2(transitive)
+ Addedwrite-file-atomic@1.3.4(transitive)
+ Addedxdg-basedir@2.0.0(transitive)