command-line-tool
Advanced tools
Comparing version 0.2.1 to 0.3.0
'use strict' | ||
var arrayify = require('array-back') | ||
var ansi = require('ansi-escape-sequences') | ||
var t = require('typical') | ||
var where = require('test-value').cb | ||
const arrayify = require('array-back') | ||
const ansi = require('ansi-escape-sequences') | ||
const t = require('typical') | ||
const where = require('test-value').where | ||
@@ -11,89 +11,89 @@ /** | ||
* @module command-line-tool | ||
* @typicalname tool | ||
* @example | ||
* const tool = require('command-line-tool') | ||
* const CommandLineTool = require('command-line-tool') | ||
*/ | ||
exports.stop = stop | ||
exports.error = error | ||
exports.halt = halt | ||
exports.options = options | ||
exports.usage = '' | ||
/** | ||
* Stop the process with the supplied exit code. | ||
* | ||
* @param {number} - the exit code | ||
* @param [options] {object} | ||
* @param [options.message] {string|string[]} - One or more messages to be written to stderr before exiting. If the exit code is greater than 0 they will be formatted red. | ||
* @param [options.usage] {string} - Usage guidance, written to stderr without additional formatting. | ||
* @static | ||
* @alias module:command-line-tool | ||
* @typicalname tool | ||
*/ | ||
function stop (message, options) { | ||
options = options || {} | ||
arrayify(message).forEach(function (msg) { | ||
console.error(ansi.format(msg)) | ||
}) | ||
class CommandLineTool { | ||
constructor () { | ||
this.usage = null | ||
} | ||
/** | ||
* Stop the process with the supplied exit code. | ||
* | ||
* @param {number} - the exit code | ||
* @param [options] {object} | ||
* @param [options.message] {string|string[]} - One or more messages to be written to stderr before exiting. If the exit code is greater than 0 they will be formatted red. | ||
*/ | ||
stop (message) { | ||
arrayify(message).forEach(function (msg) { | ||
console.error(ansi.format(msg)) | ||
}) | ||
process.exit(0) | ||
} | ||
if (options.displayUsage) console.error(exports.usage) | ||
process.exit(0) | ||
} | ||
/** | ||
* Display an error message | ||
* | ||
* @param {string | Error} - the error message or instance | ||
* @param [options] {object} | ||
*/ | ||
printError (err) { | ||
/* detect whether operational or programmer error, and display stack if latter */ | ||
console.error(ansi.format(t.isString(err) ? err : err.stack, 'red')) | ||
} | ||
/** | ||
* Display an error message | ||
* | ||
* @param {string | Error} - the error message or instance | ||
* @param [options] {object} | ||
* @static | ||
*/ | ||
function error (err, options) { | ||
options = options || {} | ||
if (!t.isDefined(options.halt)) options.halt = true | ||
if (!t.isDefined(options.exitCode)) options.exitCode = 1 | ||
/** | ||
* Stop the process with an error message | ||
* | ||
* @param {string | Error} - the error message or instance | ||
* @param [options] {object} | ||
* @param [options.exitCode] {number} - defaults to 1 | ||
*/ | ||
halt (err, { exitCode = 1 } = {}) { | ||
if (err) { | ||
if (err.code === 'EPIPE') { | ||
process.exit(0) /* no big deal */ | ||
} else { | ||
this.printError(err) | ||
} | ||
} | ||
process.exit(exitCode) | ||
} | ||
if (t.isString(err)) err = new Error(err) | ||
/** | ||
* | ||
*/ | ||
getOptions (definitions, usageSections) { | ||
definitions = arrayify(definitions) | ||
if (!definitions.some(where({ name: 'help' }))) { | ||
definitions.push({ | ||
name: 'help', | ||
alias: 'h', | ||
type: Boolean, | ||
description: 'Print usage information.' | ||
}) | ||
} | ||
const commandLineArgs = require('command-line-args') | ||
const commandLineUsage = require('command-line-usage') | ||
/* detect whether operational or programmer error, and display stack if latter */ | ||
console.error(ansi.format(err.stack, 'red')) | ||
this.usage = commandLineUsage(usageSections) | ||
let options | ||
try { | ||
options = commandLineArgs(definitions) | ||
} catch (err) { | ||
this.printError(err.name === 'UNKNOWN_OPTION' ? err.message : err) | ||
console.error(this.usage) | ||
this.halt() | ||
} | ||
if (options.help || (options._all && options._all.help)) { | ||
this.stop(this.usage) | ||
} | ||
return options | ||
} | ||
} | ||
/** | ||
* Stop the process with an error message | ||
* | ||
* @param {string | Error} - the error message or instance | ||
* @param [options] {object} | ||
* @param [options.usage] {string} - Usage guidance, written to stderr without additional formatting. | ||
* @param [options.exitCode] {number} - defaults to 1 | ||
* @param [options.halt=true] {boolean} - halt on error | ||
* @static | ||
*/ | ||
function halt (err, options) { | ||
options = options || {} | ||
if (!t.isDefined(options.halt)) options.halt = true | ||
if (!t.isDefined(options.exitCode)) options.exitCode = 1 | ||
error(err) | ||
if (err.code === 'EPIPE') process.exit(0) /* no big deal */ | ||
if (options.usage) console.error(options.usage) | ||
process.exit(options.exitCode) | ||
} | ||
function options (definitions, usageOptions) { | ||
definitions = arrayify(definitions) | ||
if (!definitions.some(where({ name: 'help '}))) { | ||
definitions.push({ | ||
name: 'help', | ||
alias: 'h', | ||
type: Boolean, | ||
description: 'Print usage information' | ||
}) | ||
} | ||
var commandLineArgs = require('command-line-args') | ||
var cli = commandLineArgs(definitions) | ||
exports.usage = cli.getUsage(usageOptions) | ||
var opts = cli.parse() | ||
if (opts.help) { | ||
stop(null, { displayUsage: true }) | ||
} | ||
return opts | ||
} | ||
module.exports = CommandLineTool |
{ | ||
"name": "command-line-tool", | ||
"author": "Lloyd Brookes <75pound@gmail.com>", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "command-line-tool", | ||
@@ -15,15 +15,23 @@ "repository": "https://github.com/75lb/command-line-tool.git", | ||
"test": "tape test/*.js", | ||
"docs": "jsdoc2md -t jsdoc2md/README.hbs lib/*.js > README.md; echo" | ||
"docs": "jsdoc2md -t jsdoc2md/README.hbs lib/*.js > README.md; echo", | ||
"es5": "babel --presets babel-preset-es2015 --no-comments lib --out-dir es5" | ||
}, | ||
"dependencies": { | ||
"ansi-escape-sequences": "^2.2.2", | ||
"array-back": "^1.0.2", | ||
"command-line-args": "^2.1.6", | ||
"test-value": "^1.1.0", | ||
"array-back": "^1.0.3", | ||
"command-line-args": "^3.0.0", | ||
"command-line-usage": "^3.0.1", | ||
"test-value": "^2.0.0", | ||
"typical": "^2.4.2" | ||
}, | ||
"devDependencies": { | ||
"jsdoc-to-markdown": "^1.3.3", | ||
"tape": "^4.4.0" | ||
"babel-preset-es2015": "^6.9.0", | ||
"jsdoc-to-markdown": "^1.3.6", | ||
"tape": "^4.5.1" | ||
}, | ||
"standard": { | ||
"ignore": [ | ||
"es5" | ||
] | ||
} | ||
} |
@@ -31,2 +31,2 @@ [![view on npm](http://img.shields.io/npm/v/command-line-tool.svg)](https://www.npmjs.org/package/command-line-tool) | ||
© 2015 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown). | ||
© 2015-16 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown). |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
9789
7
165
6
3
+ Addedcommand-line-usage@^3.0.1
+ Addedansi-escape-sequences@3.0.0(transitive)
+ Addedcommand-line-args@3.0.5(transitive)
+ Addedcommand-line-usage@3.0.8(transitive)
+ Addedreduce-flatten@1.0.1(transitive)
+ Addedtable-layout@0.3.0(transitive)
+ Addedwordwrapjs@2.0.0(transitive)
- Removedcollect-all@1.0.4(transitive)
- Removedcollect-json@1.0.9(transitive)
- Removedcolumn-layout@2.1.4(transitive)
- Removedcommand-line-args@2.1.6(transitive)
- Removedcommand-line-usage@2.0.5(transitive)
- Removedobject-get@2.1.1(transitive)
- Removedobject-tools@2.0.6(transitive)
- Removedstream-via@1.0.4(transitive)
- Removedtest-value@1.1.0(transitive)
- Removedwordwrapjs@1.2.1(transitive)
Updatedarray-back@^1.0.3
Updatedcommand-line-args@^3.0.0
Updatedtest-value@^2.0.0