minimist-options
Advanced tools
+14
-8
@@ -23,2 +23,3 @@ 'use strict'; | ||
| const passthroughOptions = ['stopEarly', 'unknown', '--']; | ||
| const availableTypes = ['string', 'boolean', 'number', 'array']; | ||
@@ -52,13 +53,10 @@ module.exports = options => { | ||
| const props = value; | ||
| const {type} = props; | ||
| if (props.type) { | ||
| const type = props.type; | ||
| if (type === 'string') { | ||
| push(result, 'string', key); | ||
| if (type) { | ||
| if (!availableTypes.includes(type)) { | ||
| throw new TypeError(`Expected "${key}" to be one of ["string", "boolean", "number", "array"], got ${type}`); | ||
| } | ||
| if (type === 'boolean') { | ||
| push(result, 'boolean', key); | ||
| } | ||
| push(result, type, key); | ||
| } | ||
@@ -73,2 +71,10 @@ | ||
| if ({}.hasOwnProperty.call(props, 'default')) { | ||
| if (type === 'array' && !Array.isArray(props.default)) { | ||
| throw new TypeError(`Expected "${key}" default value to be array, got ${typeof props.default}`); | ||
| } | ||
| if (type && type !== 'array' && typeof props.default !== type) { // eslint-disable-line valid-typeof | ||
| throw new TypeError(`Expected "${key}" default value to be ${type}, got ${typeof props.default}`); | ||
| } | ||
| insert(result, 'default', key, props.default); | ||
@@ -75,0 +81,0 @@ } |
+4
-4
| { | ||
| "name": "minimist-options", | ||
| "version": "3.0.2", | ||
| "version": "4.0.0", | ||
| "description": "Pretty options for minimist", | ||
@@ -17,3 +17,3 @@ "repository": "vadimdemedes/minimist-options", | ||
| "engines": { | ||
| "node": ">= 4" | ||
| "node": ">= 6" | ||
| }, | ||
@@ -28,5 +28,5 @@ "files": [ | ||
| "devDependencies": { | ||
| "ava": "^0.18.2", | ||
| "xo": "^0.18.2" | ||
| "ava": "^1.0.1", | ||
| "xo": "^0.23.0" | ||
| } | ||
| } |
+23
-5
| # minimist-options [](https://travis-ci.org/vadimdemedes/minimist-options) | ||
| > Write options for [minimist](https://npmjs.org/package/minimist) in a comfortable way. | ||
| > Support string, boolean, number and array options. | ||
@@ -30,9 +31,21 @@ ## Installation | ||
| score: { | ||
| type: 'number', | ||
| alias: 's', | ||
| default: 0 | ||
| }, | ||
| arr: { | ||
| type: 'array', | ||
| alias: 'a', | ||
| default: [] | ||
| }, | ||
| published: 'boolean', | ||
| // special option for positional arguments (`_` in minimist) | ||
| // Special option for positional arguments (`_` in minimist) | ||
| arguments: 'string' | ||
| }); | ||
| const args = minimist(options); | ||
| const args = minimist(process.argv.slice(2), options); | ||
| ``` | ||
@@ -47,2 +60,4 @@ | ||
| string: ['name', '_'], | ||
| number: ['score'], | ||
| array: ['arr'], | ||
| boolean: ['force', 'published'], | ||
@@ -52,11 +67,14 @@ alias: { | ||
| f: 'force', | ||
| o: 'force' | ||
| s: 'score', | ||
| a: 'arr' | ||
| }, | ||
| default: { | ||
| name: 'john', | ||
| f: false | ||
| f: false, | ||
| score: 0, | ||
| arr: [] | ||
| } | ||
| }; | ||
| const args = minimist(options); | ||
| const args = minimist(process.argv.slice(2), options); | ||
| ``` | ||
@@ -63,0 +81,0 @@ |
4842
19.17%62
8.77%82
28.13%