Socket
Socket
Sign inDemoInstall

command-line-tool

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

command-line-tool - npm Package Compare versions

Comparing version 0.2.1 to 0.3.0

es5/command-line-tool.js

162

lib/command-line-tool.js
'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)

&copy; 2015 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).
&copy; 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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc