Comparing version 2.0.6 to 2.1.0
43
index.js
@@ -0,13 +1,16 @@ | ||
"use strict" | ||
const EMPTY = [] | ||
const SHORTSPLIT = /$|[!-@\[-`{-~].*/g | ||
const EMPTY = [] | ||
const isArray = Array.isArray | ||
function array(any) { | ||
return Array.isArray(any) ? any : [any] | ||
var toArray = function(any) { | ||
return isArray(any) ? any : [any] | ||
} | ||
function aliases(aliases) { | ||
var parseAlias = function(aliases) { | ||
var out = {} | ||
for (var key in aliases) { | ||
var alias = (out[key] = array(aliases[key])) | ||
var alias = (out[key] = toArray(aliases[key])) | ||
@@ -28,3 +31,3 @@ for (var i = 0, len = alias.length; i < len; i++) { | ||
function booleans(aliases, booleans) { | ||
var parseBoolean = function(aliases, booleans) { | ||
var out = {} | ||
@@ -52,3 +55,3 @@ | ||
function defaults(aliases, defaults) { | ||
var parseDefault = function(aliases, defaults) { | ||
var out = {} | ||
@@ -76,3 +79,3 @@ | ||
function set(out, key, value, aliases, unknown) { | ||
var write = function(out, key, value, aliases, unknown) { | ||
var curr = out[key] | ||
@@ -82,7 +85,7 @@ var alias = aliases[key] | ||
if (known || unknown === undefined || false !== unknown(key)) { | ||
if (known || unknown === undefined || unknown(key) !== false) { | ||
if (curr === undefined) { | ||
out[key] = value | ||
} else { | ||
if (Array.isArray(curr)) { | ||
if (isArray(curr)) { | ||
curr.push(value) | ||
@@ -102,7 +105,7 @@ } else { | ||
module.exports = function(argv, opts) { | ||
var getopts = function(argv, opts) { | ||
var unknown = (opts = opts || {}).unknown | ||
var alias = aliases(opts.alias) | ||
var values = defaults(alias, opts.default) | ||
var bools = booleans(alias, opts.boolean) | ||
var aliases = parseAlias(opts.alias) | ||
var values = parseDefault(aliases, opts.default) | ||
var bools = parseBoolean(aliases, opts.boolean) | ||
var out = { _: [] } | ||
@@ -124,9 +127,9 @@ | ||
if (0 <= end) { | ||
set(out, arg.slice(2, end), arg.slice(end + 1), alias, unknown) | ||
write(out, arg.slice(2, end), arg.slice(end + 1), aliases, unknown) | ||
} else { | ||
if ("n" === arg[2] && "o" === arg[3] && "-" === arg[4]) { | ||
set(out, arg.slice(5), false, alias, unknown) | ||
write(out, arg.slice(5), false, aliases, unknown) | ||
} else { | ||
var key = arg.slice(2) | ||
set( | ||
write( | ||
out, | ||
@@ -138,3 +141,3 @@ key, | ||
argv[(i = k)], | ||
alias, | ||
aliases, | ||
unknown | ||
@@ -156,3 +159,3 @@ ) | ||
for (k = 1; k < end; ) { | ||
set(out, arg[k], ++k !== end || value, alias, unknown) | ||
write(out, arg[k], ++k !== end || value, aliases, unknown) | ||
} | ||
@@ -171,1 +174,3 @@ } | ||
} | ||
module.exports = getopts |
{ | ||
"name": "getopts", | ||
"description": "Node.js CLI options parser.", | ||
"version": "2.0.6", | ||
"version": "2.1.0", | ||
"main": "index.js", | ||
"types": "getopts.d.ts", | ||
"license": "MIT", | ||
@@ -10,18 +11,21 @@ "repository": "jorgebucaran/getopts", | ||
"files": [ | ||
"index.js" | ||
"index.js", | ||
"getopts.d.ts" | ||
], | ||
"keywords": [ | ||
"getopts", | ||
"cli", | ||
"argv", | ||
"cli-parser", | ||
"getopts" | ||
"cli-parser" | ||
], | ||
"scripts": { | ||
"test": "nyc -r lcov tape test/*.js && nyc report", | ||
"ts": "tsc test/ts/index.ts", | ||
"test": "nyc -r lcov tape test/*.js && nyc report && tsc -p test/ts", | ||
"release": "npm test && git commit -am $npm_package_version && git tag $npm_package_version && git push origin master && git push --tags && npm publish" | ||
}, | ||
"devDependencies": { | ||
"nyc": "^11.6.0", | ||
"tape": "^4.9.0" | ||
"nyc": "^12.0.2", | ||
"tape": "^4.9.1", | ||
"typescript": "^2.9.2" | ||
} | ||
} |
@@ -7,5 +7,5 @@ # Getopts | ||
Getopts is a Node.js CLI options parser. It's designed according to the [Utility Conventions](http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html) so that your programs behave like typical UNIX utilities effortlessly — without sacrificing developer experience. | ||
Getopts is a Node.js CLI options parser. It's designed according to the [Utility Conventions](http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html) so that your programs behave like typical UNIX utilities effortlessly and without sacrificing developer experience. | ||
Need for speed? Getopts is optimized for runtime performance and runs 10 to 20 times faster than alternatives according to our [benchmarks](/bench). | ||
Need for speed? Getopts is 10x to 20x faster than the alternatives. See the [benchmarks](/bench). | ||
@@ -20,3 +20,3 @@ ## Installation | ||
Use getopts to parse the arguments passed into your program from the command line. | ||
Use getopts to parse the arguments passed to your program from the command line. | ||
@@ -101,3 +101,3 @@ <pre> | ||
A function that runs for every unknown option. Return `false` to dismiss the option. | ||
A function to run for every unknown option. Return `false` to dismiss the option. | ||
@@ -104,0 +104,0 @@ ```js |
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
9116
5
163
3