command-line-tool
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -10,3 +10,2 @@ 'use strict'; | ||
var t = require('typical'); | ||
var where = require('test-value').where; | ||
@@ -16,4 +15,2 @@ var CommandLineTool = function () { | ||
_classCallCheck(this, CommandLineTool); | ||
this.usage = null; | ||
} | ||
@@ -31,48 +28,37 @@ | ||
key: 'printError', | ||
value: function printError(err) { | ||
console.error(ansi.format(t.isString(err) ? err : err.stack, 'red')); | ||
value: function printError(message) { | ||
arrayify(message).forEach(function (msg) { | ||
console.error(ansi.format(msg, 'red')); | ||
}); | ||
} | ||
}, { | ||
key: 'halt', | ||
value: function halt(err) { | ||
var _ref = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var _ref$exitCode = _ref.exitCode; | ||
var exitCode = _ref$exitCode === undefined ? 1 : _ref$exitCode; | ||
if (err.code === 'EPIPE') { | ||
process.exit(0); | ||
} else { | ||
this.printError(err); | ||
process.exit(exitCode); | ||
} | ||
value: function halt(err, options) { | ||
options = Object.assign({ exitCode: 1 }, options); | ||
if (err) { | ||
if (err.code === 'EPIPE') { | ||
process.exit(0); | ||
} else { | ||
this.printError(t.isString(err) ? err : options.stack ? err.stack : err.message, options); | ||
} | ||
} | ||
process.exit(options.exitCode); | ||
} | ||
}, { | ||
key: 'getOptions', | ||
value: function getOptions(definitions, usageSections) { | ||
key: 'getCli', | ||
value: function getCli(definitions, usageSections, argv) { | ||
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 commandLineUsage = require('command-line-usage'); | ||
this.usage = commandLineUsage(usageSections); | ||
var usage = usageSections ? commandLineUsage(usageSections) : ''; | ||
var options = void 0; | ||
try { | ||
options = commandLineArgs(definitions); | ||
options = commandLineArgs(definitions, argv); | ||
} catch (err) { | ||
this.printError(err.message); | ||
console.error(this.usage); | ||
this.printError(err.name === 'UNKNOWN_OPTION' ? err.message : err); | ||
console.error(usage); | ||
this.halt(); | ||
} | ||
if (options.help || options._all && options._all.help) { | ||
this.stop(this.usage); | ||
} | ||
return options; | ||
return { options: options, usage: usage }; | ||
} | ||
@@ -79,0 +65,0 @@ }]); |
@@ -5,3 +5,2 @@ 'use strict' | ||
const t = require('typical') | ||
const where = require('test-value').where | ||
@@ -12,4 +11,2 @@ /** | ||
* @module command-line-tool | ||
* @example | ||
* const CommandLineTool = require('command-line-tool') | ||
*/ | ||
@@ -20,13 +17,11 @@ | ||
* @typicalname tool | ||
* @example | ||
* const tool = require('command-line-tool') | ||
*/ | ||
class CommandLineTool { | ||
constructor () { | ||
this.usage = null | ||
} | ||
/** | ||
* Stop the process with the supplied exit code. | ||
* Print the supplied messages then stop the process (no 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 [message] {string|string[]} - One or more messages to be written to stderr before exiting. May contain `ansi.format` markup. | ||
*/ | ||
@@ -41,18 +36,16 @@ stop (message) { | ||
/** | ||
* Display an error message | ||
* Prints one or more strings in red to stderr. | ||
* | ||
* @param {string | Error} - the error message or instance | ||
* @param [options] {object} | ||
* @param [options.stack] {boolean} - defaults to false | ||
* @param {string | string[]} - input message(s) | ||
*/ | ||
printError (err, options) { | ||
options = options || {} | ||
const msg = t.isString(err) ? err : options.stack ? err.stack : err.message | ||
console.error(ansi.format(msg, 'red')) | ||
printError (message) { | ||
arrayify(message).forEach(function (msg) { | ||
console.error(ansi.format(msg, 'red')) | ||
}) | ||
} | ||
/** | ||
* Stop the process with an error message | ||
* Stop the process with an error message. | ||
* | ||
* @param {string | Error} - the error message or instance | ||
* @param [err] {Error} - the error instance | ||
* @param [options] {object} | ||
@@ -68,3 +61,3 @@ * @param [options.exitCode] {number} - defaults to 1 | ||
} else { | ||
this.printError(err, options) | ||
this.printError(t.isString(err) ? err : options.stack ? err.stack : err.message, options) | ||
} | ||
@@ -76,5 +69,9 @@ } | ||
/** | ||
* | ||
* Parse the command-line options. | ||
* @param definitions {OptionDefinitions[]} - to be passed to command-line-args | ||
* @param [usageSections] {section[]} - to be passed to command-line-usage | ||
* @param [argv] {string[]} - If supplied, this `argv` array is parsed instead of `process.argv`. | ||
* @returns {object} | ||
*/ | ||
getOptions (definitions, usageSections) { | ||
getCli (definitions, usageSections, argv) { | ||
definitions = arrayify(definitions) | ||
@@ -84,12 +81,12 @@ const commandLineArgs = require('command-line-args') | ||
this.usage = commandLineUsage(usageSections) | ||
const usage = usageSections ? commandLineUsage(usageSections) : '' | ||
let options | ||
try { | ||
options = commandLineArgs(definitions) | ||
options = commandLineArgs(definitions, argv) | ||
} catch (err) { | ||
this.printError(err.name === 'UNKNOWN_OPTION' ? err.message : err) | ||
console.error(this.usage) | ||
console.error(usage) | ||
this.halt() | ||
} | ||
return options | ||
return { options, usage } | ||
} | ||
@@ -96,0 +93,0 @@ } |
{ | ||
"name": "command-line-tool", | ||
"author": "Lloyd Brookes <75pound@gmail.com>", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "command-line-tool", | ||
"repository": "https://github.com/75lb/command-line-tool.git", | ||
"license": "MIT", | ||
"main": "./lib/command-line-tool.js", | ||
"main": "index", | ||
"keywords": [], | ||
@@ -23,3 +23,3 @@ "engines": { | ||
"command-line-usage": "^3.0.3", | ||
"test-value": "^2.0.0", | ||
"feature-detect-es6": "^1.3.0", | ||
"typical": "^2.4.2" | ||
@@ -26,0 +26,0 @@ }, |
[![view on npm](http://img.shields.io/npm/v/command-line-tool.svg)](https://www.npmjs.org/package/command-line-tool) | ||
[![npm module downloads per month](http://img.shields.io/npm/dm/command-line-tool.svg)](https://www.npmjs.org/package/command-line-tool) | ||
[![npm module downloads](http://img.shields.io/npm/dt/command-line-tool.svg)](https://www.npmjs.org/package/command-line-tool) | ||
[![Build Status](https://travis-ci.org/75lb/command-line-tool.svg?branch=master)](https://travis-ci.org/75lb/command-line-tool) | ||
@@ -8,25 +8,70 @@ [![Dependency Status](https://david-dm.org/75lb/command-line-tool.svg)](https://david-dm.org/75lb/command-line-tool) | ||
<a name="module_command-line-tool"></a> | ||
## command-line-tool | ||
Some conventional operations used in command-line tools. | ||
**Example** | ||
```js | ||
const tool = require('command-line-tool') | ||
``` | ||
<a name="module_command-line-tool.stop"></a> | ||
### tool.stop(exitCode, [options]) | ||
Stop the process with the supplied exit code. | ||
**Kind**: static method of <code>[command-line-tool](#module_command-line-tool)</code> | ||
* [command-line-tool](#module_command-line-tool) | ||
* [CommandLineTool](#exp_module_command-line-tool--CommandLineTool) ⏏ | ||
* [.stop([message])](#module_command-line-tool--CommandLineTool+stop) | ||
* [.printError(message)](#module_command-line-tool--CommandLineTool+printError) | ||
* [.halt([err], [options])](#module_command-line-tool--CommandLineTool+halt) | ||
* [.getCli(definitions, [usageSections], [argv])](#module_command-line-tool--CommandLineTool+getCli) ⇒ <code>object</code> | ||
<a name="exp_module_command-line-tool--CommandLineTool"></a> | ||
### CommandLineTool ⏏ | ||
**Kind**: Exported class | ||
<a name="module_command-line-tool--CommandLineTool+stop"></a> | ||
#### tool.stop([message]) | ||
Print the supplied messages then stop the process (no exit code). | ||
**Kind**: instance method of <code>[CommandLineTool](#exp_module_command-line-tool--CommandLineTool)</code> | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| exitCode | <code>number</code> | the exit code | | ||
| [message] | <code>string</code> | <code>Array.<string></code> | One or more messages to be written to stderr before exiting. May contain `ansi.format` markup. | | ||
<a name="module_command-line-tool--CommandLineTool+printError"></a> | ||
#### tool.printError(message) | ||
Prints one or more strings in red to stderr. | ||
**Kind**: instance method of <code>[CommandLineTool](#exp_module_command-line-tool--CommandLineTool)</code> | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| message | <code>string</code> | <code>Array.<string></code> | input message(s) | | ||
<a name="module_command-line-tool--CommandLineTool+halt"></a> | ||
#### tool.halt([err], [options]) | ||
Stop the process with an error message. | ||
**Kind**: instance method of <code>[CommandLineTool](#exp_module_command-line-tool--CommandLineTool)</code> | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| [err] | <code>Error</code> | the error instance | | ||
| [options] | <code>object</code> | | | ||
| [options.message] | <code>string</code> | <code>Array.<string></code> | One or more messages to be written to stderr before exiting. If the exit code is greater than 0 they will be formatted red. | | ||
| [options.usage] | <code>string</code> | Usage guidance, written to stderr without additional formatting. | | ||
| [options.exitCode] | <code>number</code> | defaults to 1 | | ||
| [options.stack] | <code>boolean</code> | defaults to false | | ||
<a name="module_command-line-tool--CommandLineTool+getCli"></a> | ||
#### tool.getCli(definitions, [usageSections], [argv]) ⇒ <code>object</code> | ||
Parse the command-line options. | ||
**Kind**: instance method of <code>[CommandLineTool](#exp_module_command-line-tool--CommandLineTool)</code> | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| definitions | <code>Array.<OptionDefinitions></code> | to be passed to command-line-args | | ||
| [usageSections] | <code>Array.<section></code> | to be passed to command-line-usage | | ||
| [argv] | <code>Array.<string></code> | If supplied, this `argv` array is parsed instead of `process.argv`. | | ||
* * * | ||
© 2015-16 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown). |
var test = require('tape') | ||
var lib = require('../') | ||
var Tool = require('../') | ||
var tool = new Tool() | ||
test('first', function(t){ | ||
test('.getCli', function(t){ | ||
var definitions = [ | ||
{ name: 'yeah', type: String } | ||
] | ||
var sections = [ | ||
{ header: 'Yeah', content: 'Test' } | ||
] | ||
var cli = tool.getCli(definitions, sections, [ '--yeah', 'test' ]) | ||
t.deepEqual(cli.options, { yeah: 'test' }) | ||
t.ok(/Test/.test(cli.usage)) | ||
t.end() | ||
}) |
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
11967
9
161
77
+ Addedfeature-detect-es6@^1.3.0
- Removedtest-value@^2.0.0