Skip to content

Commit

Permalink
feat(help): Showing help message on -h or --help
Browse files Browse the repository at this point in the history
closes #1
  • Loading branch information
bahmutov committed Dec 3, 2015
1 parent 92d9936 commit 79b031c
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 14 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var options = {
noExit: true | false, // simple-bin-help by default calls process.exit
help: 'help string', // to display if invalid arguments
minArguments: n, // min number of arguments to check
pkg: packageObject, // package object or path for better message
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
};
Expand All @@ -48,9 +48,13 @@ require('simple-bin-help')(options);
If `noExit` is true, the call simply shows the error message if number of arguments is
invalid and returns a boolean result.

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.
* Includes and calls the [update-notifier]() module by default.
* If passed `-h` or `--help` option, shows the help message.

### Small print

Expand Down
34 changes: 34 additions & 0 deletions examples/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const simpleBinHelp = require('..')

var options = {
noExit: true,
help: 'basic example help',
minArguments: 3,
pkg: {}
}
console.log('min arguments 3, calling with empty package')
simpleBinHelp(options)

options = {
noExit: true,
help: 'has package object',
minArguments: 3,
pkg: {
name: 'basic-example',
version: '1.0.0-dev'
}
}
console.log('min arguments 3, calling with package')
simpleBinHelp(options)

options = {
noExit: true,
help: 'basic example, path to real package.json',
minArguments: 3,
packagePath: __dirname + '/../package.json',
onFail: function () {
console.log('fail callback, show extra info')
}
}
console.log('real package.json path')
simpleBinHelp(options)
44 changes: 34 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@

const updateNotifier = require('update-notifier')

function isHelp (arg) {
return arg === '-h' ||
arg === '--help'
}

function hasHelpArgument (args) {
return args.some(isHelp)
}

function noArguments (minLength, args) {
console.assert(Array.isArray(args), 'missing arguments')
return args.length < minLength
}

function getPackage (options) {
var pkg
var pkg = options.pkg || options.package

if (options.package) {
pkg = options.package
} else if (options.packagePath) {
if (!pkg && options.packagePath) {
pkg = require(options.packagePath)
}

Expand All @@ -26,7 +33,13 @@ function showHelp (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) {
Expand All @@ -43,32 +56,43 @@ function showHelp (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
}

if (hasHelpArgument(cliArguments)) {
showHelp(options)
finish(options)
return true
}

var pkg = getPackage(options)
if (pkg) {
if (pkg && pkg.name && pkg.version) {
updateNotifier({ pkg: pkg }).notify()
}

var minArguments = options.minArguments ||
options.min ||
options.n

if (noArguments(minArguments, cliArguments)) {
showHelp(options)

if (typeof options.onFail === 'function') {
options.onFail()
}

if (options.noExit) {
return false
}
process.exit(0)
return finish(options)
}

return true
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"commit": "git-issues && commit-wizard",
"issues": "git-issues",
"size": "t=\"$(npm pack .)\"; wc -c \"${t}\"; tar tvf \"${t}\"; rm \"${t}\";",
"lint": "standard *.js spec/*.js"
"lint": "standard *.js spec/*.js examples/*.js",
"basic": "node examples/basic.js"
},
"files": [
"index.js"
Expand Down Expand Up @@ -48,7 +49,8 @@
"commit-msg": "validate-commit-msg",
"pre-commit": [
"npm run lint",
"npm test"
"npm test",
"npm run basic"
],
"pre-push": [
"npm run size"
Expand Down
16 changes: 16 additions & 0 deletions spec/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,20 @@ describe('simple bin help', function () {
la(!simpleHelp(options, cliArguments), 'not enough arguments')
la(called, 'onFail called')
})

it('can show help with -h', function () {
var options = {
noExit: true
}
var cliArguments = ['-h']
la(simpleHelp(options, cliArguments))
})

it('can show help with --help', function () {
var options = {
noExit: true
}
var cliArguments = ['--help']
la(simpleHelp(options, cliArguments))
})
})

0 comments on commit 79b031c

Please sign in to comment.