meow
Advanced tools
Comparing version 3.3.0 to 3.4.0
26
index.js
'use strict'; | ||
var path = require('path'); | ||
var minimist = require('minimist'); | ||
var indentString = require('indent-string'); | ||
var objectAssign = require('object-assign'); | ||
var camelcaseKeys = require('camelcase-keys'); | ||
var trimNewlines = require('trim-newlines'); | ||
var redent = require('redent'); | ||
var readPkgUp = require('read-pkg-up'); | ||
var loudRejection = require('loud-rejection'); | ||
// needed to get the uncached parent | ||
// get the uncached parent | ||
delete require.cache[__filename]; | ||
@@ -13,4 +16,10 @@ var parentDir = path.dirname(module.parent.filename); | ||
module.exports = function (opts, minimistOpts) { | ||
loudRejection(); | ||
if (Array.isArray(opts) || typeof opts === 'string') { | ||
opts = {help: opts}; | ||
} | ||
opts = objectAssign({ | ||
pkg: './package.json', | ||
pkg: readPkgUp.sync({cwd: parentDir}).pkg, | ||
argv: process.argv.slice(2) | ||
@@ -25,6 +34,11 @@ }, opts); | ||
var argv = minimist(opts.argv, minimistOpts); | ||
var help = '\n' + indentString(pkg.description + (opts.help ? '\n\n' + opts.help : '\n'), ' '); | ||
var showHelp = function () { | ||
var help = redent(trimNewlines(opts.help), 2); | ||
process.title = pkg.bin ? Object.keys(pkg.bin)[0] : pkg.name; | ||
help = '\n ' + pkg.description + (help ? '\n\n' + help : '\n'); | ||
var showHelp = function (code) { | ||
console.log(help); | ||
process.exit(); | ||
process.exit(code || 0); | ||
}; | ||
@@ -31,0 +45,0 @@ |
{ | ||
"name": "meow", | ||
"version": "3.3.0", | ||
"version": "3.4.0", | ||
"description": "CLI app helper", | ||
@@ -16,3 +16,3 @@ "license": "MIT", | ||
"scripts": { | ||
"test": "node test.js" | ||
"test": "xo && ava" | ||
}, | ||
@@ -43,9 +43,14 @@ "files": [ | ||
"camelcase-keys": "^1.0.0", | ||
"indent-string": "^1.1.0", | ||
"minimist": "^1.1.0", | ||
"object-assign": "^3.0.0" | ||
"loud-rejection": "^1.0.0", | ||
"minimist": "^1.1.3", | ||
"object-assign": "^4.0.1", | ||
"read-pkg-up": "^1.0.1", | ||
"redent": "^1.0.0", | ||
"trim-newlines": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "0.0.4" | ||
"ava": "*", | ||
"indent-string": "^2.1.0", | ||
"xo": "*" | ||
} | ||
} |
@@ -14,2 +14,4 @@ # meow [![Build Status](https://travis-ci.org/sindresorhus/meow.svg?branch=master)](https://travis-ci.org/sindresorhus/meow) | ||
- Outputs description and supplied help text when `--help` | ||
- Makes unhandled rejected promises [fail loudly](https://github.com/sindresorhus/loud-rejection) instead of the default silent fail | ||
- Sets the process title to the binary name defined in package.json | ||
@@ -33,10 +35,19 @@ | ||
'use strict'; | ||
var meow = require('meow'); | ||
var fooApp = require('./'); | ||
const meow = require('meow'); | ||
const foo = require('./'); | ||
var cli = meow({ | ||
help: [ | ||
'Usage', | ||
' foo-app <input>' | ||
] | ||
const cli = meow(` | ||
Usage | ||
$ foo <input> | ||
Options | ||
-r, --rainbow Include a rainbow | ||
Examples | ||
$ foo unicorns --rainbow | ||
🌈 unicorns 🌈 | ||
`, { | ||
alias: { | ||
r: 'rainbow' | ||
} | ||
}); | ||
@@ -46,3 +57,3 @@ /* | ||
input: ['unicorns'], | ||
flags: {rainbowCake: true}, | ||
flags: {rainbow: true}, | ||
... | ||
@@ -52,3 +63,3 @@ } | ||
fooApp(cli.input[0], cli.flags); | ||
foo(cli.input[0], cli.flags); | ||
``` | ||
@@ -59,3 +70,3 @@ | ||
### meow(options, minimistOptions) | ||
### meow(options, [minimistOptions]) | ||
@@ -68,14 +79,21 @@ Returns an object with: | ||
- `help` *(object)* - The help text used with `--help` | ||
- `showHelp()` *(function)* - Show the help text and exit | ||
- `showHelp([code=0])` *(function)* - Show the help text and exit with `code` | ||
#### options | ||
Type: `object`, `array`, `string` | ||
Can either be a string/array that is the `help` or an options object. | ||
##### help | ||
Type: `array`, `string`, `boolean` | ||
Type: `string`, `boolean` | ||
The help text you want shown. | ||
If it's an array each item will be a line. | ||
The input is reindented and starting/ending newlines are trimmed which means you can use a [template literal](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/template_strings) without having to care about using the correct amount of indent. | ||
<del>If it's an array each item will be a line.</del> | ||
*(Still supported, but you should use a template literal instead.)* | ||
If you don't specify anything, it will still show the package.json `"description"`. | ||
@@ -88,3 +106,3 @@ | ||
Type: `string`, `boolean` | ||
Default: the package.json `"version"` property | ||
Default: The package.json `"version"` property | ||
@@ -98,5 +116,5 @@ Set a custom version output. | ||
Type: `string`, `object` | ||
Default: `package.json` | ||
Default: Closest package.json upwards | ||
Relative path to `package.json` or it as an object. | ||
Relative path to package.json or as an object. | ||
@@ -118,9 +136,22 @@ ##### argv | ||
## Tip | ||
## Promises | ||
Use [get-stdin](https://github.com/sindresorhus/get-stdin) if you need to accept input from stdin. | ||
Meow will make unhandled rejected promises [fail loudly](https://github.com/sindresorhus/loud-rejection) instead of the default silent fail. Meaning you don't have to manually `.catch()` promises used in your CLI. | ||
## Tips | ||
See [`chalk`](https://github.com/chalk/chalk) if you want to colorize the terminal output. | ||
See [`get-stdin`](https://github.com/sindresorhus/get-stdin) if you want to accept input from stdin. | ||
See [`update-notifier`](https://github.com/yeoman/update-notifier) if you want update notifications. | ||
See [`configstore`](https://github.com/yeoman/configstore) if you need to persist some data. | ||
[More useful CLI utilities.](https://github.com/sindresorhus/awesome-nodejs#command-line-utilities) | ||
## License | ||
MIT © [Sindre Sorhus](http://sindresorhus.com) |
7059
50
149
7
3
+ Addedloud-rejection@^1.0.0
+ Addedread-pkg-up@^1.0.1
+ Addedredent@^1.0.0
+ Addedtrim-newlines@^1.0.0
+ Addedarray-find-index@1.0.2(transitive)
+ Addedcurrently-unhandled@0.4.1(transitive)
+ Addederror-ex@1.3.2(transitive)
+ Addedfind-up@1.1.2(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedhosted-git-info@2.8.9(transitive)
+ Addedindent-string@2.1.0(transitive)
+ Addedis-arrayish@0.2.1(transitive)
+ Addedis-core-module@2.13.1(transitive)
+ Addedis-utf8@0.2.1(transitive)
+ Addedload-json-file@1.1.0(transitive)
+ Addedloud-rejection@1.6.0(transitive)
+ Addednormalize-package-data@2.5.0(transitive)
+ Addedobject-assign@4.1.1(transitive)
+ Addedparse-json@2.2.0(transitive)
+ Addedpath-exists@2.1.0(transitive)
+ Addedpath-parse@1.0.7(transitive)
+ Addedpath-type@1.1.0(transitive)
+ Addedpify@2.3.0(transitive)
+ Addedpinkie@2.0.4(transitive)
+ Addedpinkie-promise@2.0.1(transitive)
+ Addedread-pkg@1.1.0(transitive)
+ Addedread-pkg-up@1.0.1(transitive)
+ Addedredent@1.0.0(transitive)
+ Addedrepeating@2.0.1(transitive)
+ Addedresolve@1.22.8(transitive)
+ Addedsemver@5.7.2(transitive)
+ Addedsignal-exit@3.0.7(transitive)
+ Addedspdx-correct@3.2.0(transitive)
+ Addedspdx-exceptions@2.5.0(transitive)
+ Addedspdx-expression-parse@3.0.1(transitive)
+ Addedspdx-license-ids@3.0.17(transitive)
+ Addedstrip-bom@2.0.0(transitive)
+ Addedstrip-indent@1.0.1(transitive)
+ Addedsupports-preserve-symlinks-flag@1.0.0(transitive)
+ Addedtrim-newlines@1.0.0(transitive)
+ Addedvalidate-npm-package-license@3.0.4(transitive)
- Removedindent-string@^1.1.0
- Removedindent-string@1.2.2(transitive)
- Removedobject-assign@3.0.0(transitive)
- Removedrepeating@1.1.3(transitive)
Updatedminimist@^1.1.3
Updatedobject-assign@^4.0.1