Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/austingebauer/devise.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/yargs-parser/index.js')
-rw-r--r--node_modules/yargs-parser/index.js311
1 files changed, 234 insertions, 77 deletions
diff --git a/node_modules/yargs-parser/index.js b/node_modules/yargs-parser/index.js
index b71faf5..f9ee824 100644
--- a/node_modules/yargs-parser/index.js
+++ b/node_modules/yargs-parser/index.js
@@ -1,4 +1,5 @@
var camelCase = require('camelcase')
+var decamelize = require('decamelize')
var path = require('path')
var tokenizeArgString = require('./lib/tokenize-arg-string')
var util = require('util')
@@ -8,25 +9,33 @@ function parse (args, opts) {
// allow a string argument to be passed in rather
// than an argv array.
args = tokenizeArgString(args)
+
// aliases might have transitive relationships, normalize this.
var aliases = combineAliases(opts.alias || {})
- var configuration = assign({
+ var configuration = Object.assign({
'short-option-groups': true,
'camel-case-expansion': true,
'dot-notation': true,
'parse-numbers': true,
'boolean-negation': true,
+ 'negation-prefix': 'no-',
'duplicate-arguments-array': true,
- 'flatten-duplicate-arrays': true
+ 'flatten-duplicate-arrays': true,
+ 'populate--': false,
+ 'combine-arrays': false,
+ 'set-placeholder-key': false,
+ 'halt-at-non-option': false,
+ 'strip-aliased': false,
+ 'strip-dashed': false
}, opts.configuration)
var defaults = opts.default || {}
var configObjects = opts.configObjects || []
var envPrefix = opts.envPrefix
+ var notFlagsOption = configuration['populate--']
+ var notFlagsArgv = notFlagsOption ? '--' : '_'
var newAliases = {}
// allow a i18n handler to be passed in, default to a fake one (util.format).
- var __ = opts.__ || function (str) {
- return util.format.apply(util, Array.prototype.slice.call(arguments))
- }
+ var __ = opts.__ || util.format
var error = null
var flags = {
aliases: {},
@@ -39,40 +48,66 @@ function parse (args, opts) {
configs: {},
defaulted: {},
nargs: {},
- coercions: {}
+ coercions: {},
+ keys: []
}
var negative = /^-[0-9]+(\.[0-9]+)?/
+ var negatedBoolean = new RegExp('^--' + configuration['negation-prefix'] + '(.+)')
+
+ ;[].concat(opts.array).filter(Boolean).forEach(function (opt) {
+ var key = opt.key || opt
+
+ // assign to flags[bools|strings|numbers]
+ const assignment = Object.keys(opt).map(function (key) {
+ return ({
+ boolean: 'bools',
+ string: 'strings',
+ number: 'numbers'
+ })[key]
+ }).filter(Boolean).pop()
+
+ // assign key to be coerced
+ if (assignment) {
+ flags[assignment][key] = true
+ }
- ;[].concat(opts.array).filter(Boolean).forEach(function (key) {
flags.arrays[key] = true
+ flags.keys.push(key)
})
;[].concat(opts.boolean).filter(Boolean).forEach(function (key) {
flags.bools[key] = true
+ flags.keys.push(key)
})
;[].concat(opts.string).filter(Boolean).forEach(function (key) {
flags.strings[key] = true
+ flags.keys.push(key)
})
;[].concat(opts.number).filter(Boolean).forEach(function (key) {
flags.numbers[key] = true
+ flags.keys.push(key)
})
;[].concat(opts.count).filter(Boolean).forEach(function (key) {
flags.counts[key] = true
+ flags.keys.push(key)
})
;[].concat(opts.normalize).filter(Boolean).forEach(function (key) {
flags.normalize[key] = true
+ flags.keys.push(key)
})
Object.keys(opts.narg || {}).forEach(function (k) {
flags.nargs[k] = opts.narg[k]
+ flags.keys.push(k)
})
Object.keys(opts.coerce || {}).forEach(function (k) {
flags.coercions[k] = opts.coerce[k]
+ flags.keys.push(k)
})
if (Array.isArray(opts.config) || typeof opts.config === 'string') {
@@ -99,15 +134,13 @@ function parse (args, opts) {
var argv = { _: [] }
Object.keys(flags.bools).forEach(function (key) {
- setArg(key, !(key in defaults) ? false : defaults[key])
- setDefaulted(key)
+ if (Object.prototype.hasOwnProperty.call(defaults, key)) {
+ setArg(key, defaults[key])
+ setDefaulted(key)
+ }
})
var notFlags = []
- if (args.indexOf('--') !== -1) {
- notFlags = args.slice(args.indexOf('--') + 1)
- args = args.slice(0, args.indexOf('--'))
- }
for (var i = 0; i < args.length; i++) {
var arg = args[i]
@@ -118,7 +151,7 @@ function parse (args, opts) {
var next
var value
- // -- seperated by =
+ // -- separated by =
if (arg.match(/^--.+=/) || (
!configuration['short-option-groups'] && arg.match(/^-.+=/)
)) {
@@ -138,13 +171,13 @@ function parse (args, opts) {
} else {
setArg(m[1], m[2])
}
- } else if (arg.match(/^--no-.+/) && configuration['boolean-negation']) {
- key = arg.match(/^--no-(.+)/)[1]
+ } else if (arg.match(negatedBoolean) && configuration['boolean-negation']) {
+ key = arg.match(negatedBoolean)[1]
setArg(key, false)
// -- seperated by space.
} else if (arg.match(/^--.+/) || (
- !configuration['short-option-groups'] && arg.match(/^-.+/)
+ !configuration['short-option-groups'] && arg.match(/^-[^-]+/)
)) {
key = arg.match(/^--?(.+)/)[1]
@@ -155,7 +188,7 @@ function parse (args, opts) {
} else if (checkAllAliases(key, flags.arrays) && args.length > i + 1) {
i = eatArray(i, key, args)
} else {
- next = args[i + 1]
+ next = flags.nargs[key] === 0 ? undefined : args[i + 1]
if (next !== undefined && (!next.match(/^-/) ||
next.match(negative)) &&
@@ -167,7 +200,7 @@ function parse (args, opts) {
setArg(key, next)
i++
} else {
- setArg(key, defaultForType(guessType(key, flags)))
+ setArg(key, defaultValue(key))
}
}
@@ -187,7 +220,7 @@ function parse (args, opts) {
setArg(key, next)
i++
} else {
- setArg(key, defaultForType(guessType(key, flags)))
+ setArg(key, defaultValue(key))
}
} else if (arg.match(/^-[^-]+/) && !arg.match(negative)) {
letters = arg.slice(1, -1).split('')
@@ -234,7 +267,7 @@ function parse (args, opts) {
broken = true
break
} else {
- setArg(letters[j], defaultForType(guessType(letters[j], flags)))
+ setArg(letters[j], defaultValue(letters[j]))
}
}
@@ -260,14 +293,18 @@ function parse (args, opts) {
setArg(key, next)
i++
} else {
- setArg(key, defaultForType(guessType(key, flags)))
+ setArg(key, defaultValue(key))
}
}
}
+ } else if (arg === '--') {
+ notFlags = args.slice(i + 1)
+ break
+ } else if (configuration['halt-at-non-option']) {
+ notFlags = args.slice(i)
+ break
} else {
- argv._.push(
- flags.strings['_'] || !isNumber(arg) ? arg : Number(arg)
- )
+ argv._.push(maybeCoerceNumber('_', arg))
}
}
@@ -283,28 +320,58 @@ function parse (args, opts) {
setConfigObjects()
applyDefaultsAndAliases(argv, flags.aliases, defaults)
applyCoercions(argv)
+ if (configuration['set-placeholder-key']) setPlaceholderKeys(argv)
// for any counts either not in args or without an explicit default, set to 0
Object.keys(flags.counts).forEach(function (key) {
if (!hasKey(argv, key.split('.'))) setArg(key, 0)
})
+ // '--' defaults to undefined.
+ if (notFlagsOption && notFlags.length) argv[notFlagsArgv] = []
notFlags.forEach(function (key) {
- argv._.push(key)
+ argv[notFlagsArgv].push(key)
})
+ if (configuration['camel-case-expansion'] && configuration['strip-dashed']) {
+ Object.keys(argv).filter(key => key !== '--' && key.includes('-')).forEach(key => {
+ delete argv[key]
+ })
+ }
+
+ if (configuration['strip-aliased']) {
+ // XXX Switch to [].concat(...Object.values(aliases)) once node.js 6 is dropped
+ ;[].concat(...Object.keys(aliases).map(k => aliases[k])).forEach(alias => {
+ if (configuration['camel-case-expansion']) {
+ delete argv[alias.split('.').map(prop => camelCase(prop)).join('.')]
+ }
+
+ delete argv[alias]
+ })
+ }
+
// how many arguments should we consume, based
// on the nargs option?
function eatNargs (i, key, args) {
- var toEat = checkAllAliases(key, flags.nargs)
+ var ii
+ const toEat = checkAllAliases(key, flags.nargs)
+
+ // nargs will not consume flag arguments, e.g., -abc, --foo,
+ // and terminates when one is observed.
+ var available = 0
+ for (ii = i + 1; ii < args.length; ii++) {
+ if (!args[ii].match(/^-[^0-9]/)) available++
+ else break
+ }
- if (args.length - (i + 1) < toEat) error = Error(__('Not enough arguments following: %s', key))
+ if (available < toEat) error = Error(__('Not enough arguments following: %s', key))
- for (var ii = i + 1; ii < (toEat + i + 1); ii++) {
+ const consumed = Math.min(available, toEat)
+ for (ii = i + 1; ii < (consumed + i + 1); ii++) {
setArg(key, args[ii])
}
- return (i + toEat)
+ return (i + consumed)
}
// if an option is an array, eat all non-hyphenated arguments
@@ -341,10 +408,11 @@ function parse (args, opts) {
function setArg (key, val) {
unsetDefaulted(key)
- if (/-/.test(key) && !(flags.aliases[key] && flags.aliases[key].length) && configuration['camel-case-expansion']) {
- var c = camelCase(key)
- flags.aliases[key] = [c]
- newAliases[c] = true
+ if (/-/.test(key) && configuration['camel-case-expansion']) {
+ var alias = key.split('.').map(function (prop) {
+ return camelCase(prop)
+ }).join('.')
+ addNewAlias(key, alias)
}
var value = processValue(key, val)
@@ -353,7 +421,7 @@ function parse (args, opts) {
setKey(argv, splitKey, value)
// handle populating aliases of the full key
- if (flags.aliases[key]) {
+ if (flags.aliases[key] && flags.aliases[key].forEach) {
flags.aliases[key].forEach(function (x) {
x = x.split('.')
setKey(argv, x, value)
@@ -389,17 +457,31 @@ function parse (args, opts) {
}
}
+ function addNewAlias (key, alias) {
+ if (!(flags.aliases[key] && flags.aliases[key].length)) {
+ flags.aliases[key] = [alias]
+ newAliases[alias] = true
+ }
+ if (!(flags.aliases[alias] && flags.aliases[alias].length)) {
+ addNewAlias(alias, key)
+ }
+ }
+
function processValue (key, val) {
+ // strings may be quoted, clean this up as we assign values.
+ if (typeof val === 'string' &&
+ (val[0] === "'" || val[0] === '"') &&
+ val[val.length - 1] === val[0]
+ ) {
+ val = val.substring(1, val.length - 1)
+ }
+
// handle parsing boolean arguments --foo=true --bar false.
if (checkAllAliases(key, flags.bools) || checkAllAliases(key, flags.counts)) {
if (typeof val === 'string') val = val === 'true'
}
- var value = val
- if (!checkAllAliases(key, flags.strings) && !checkAllAliases(key, flags.coercions)) {
- if (isNumber(val)) value = Number(val)
- if (!isUndefined(val) && !isNumber(val) && checkAllAliases(key, flags.numbers)) value = NaN
- }
+ var value = maybeCoerceNumber(key, val)
// increment a count given as arg (either no value or value parsed as boolean)
if (checkAllAliases(key, flags.counts) && (isUndefined(value) || typeof value === 'boolean')) {
@@ -414,6 +496,16 @@ function parse (args, opts) {
return value
}
+ function maybeCoerceNumber (key, value) {
+ if (!checkAllAliases(key, flags.strings) && !checkAllAliases(key, flags.coercions)) {
+ const shouldCoerceNumber = isNumber(value) && configuration['parse-numbers'] && (
+ Number.isSafeInteger(Math.floor(value))
+ )
+ if (shouldCoerceNumber || (!isUndefined(value) && checkAllAliases(key, flags.numbers))) value = Number(value)
+ }
+ return value
+ }
+
// set args from config.json file, this should be
// applied last so that defaults can be applied.
function setConfig (argv) {
@@ -462,13 +554,13 @@ function parse (args, opts) {
// if the value is an inner object and we have dot-notation
// enabled, treat inner objects in config the same as
// heavily nested dot notations (foo.bar.apple).
- if (typeof value === 'object' && !Array.isArray(value) && configuration['dot-notation']) {
+ if (typeof value === 'object' && value !== null && !Array.isArray(value) && configuration['dot-notation']) {
// if the value is an object but not an array, check nested object
setConfigObject(value, fullKey)
} else {
// setting arguments via CLI takes precedence over
// values within the config file.
- if (!hasKey(argv, fullKey.split('.')) || (flags.defaulted[fullKey])) {
+ if (!hasKey(argv, fullKey.split('.')) || (flags.defaulted[fullKey]) || (flags.arrays[fullKey] && configuration['combine-arrays'])) {
setArg(fullKey, value)
}
}
@@ -506,18 +598,33 @@ function parse (args, opts) {
function applyCoercions (argv) {
var coerce
+ var applied = {}
Object.keys(argv).forEach(function (key) {
- coerce = checkAllAliases(key, flags.coercions)
- if (typeof coerce === 'function') {
- try {
- argv[key] = coerce(argv[key])
- } catch (err) {
- error = err
+ if (!applied.hasOwnProperty(key)) { // If we haven't already coerced this option via one of its aliases
+ coerce = checkAllAliases(key, flags.coercions)
+ if (typeof coerce === 'function') {
+ try {
+ var value = coerce(argv[key])
+ ;([].concat(flags.aliases[key] || [], key)).forEach(ali => {
+ applied[ali] = argv[ali] = value
+ })
+ } catch (err) {
+ error = err
+ }
}
}
})
}
+ function setPlaceholderKeys (argv) {
+ flags.keys.forEach((key) => {
+ // don't set placeholder keys for dot notation options 'foo.bar'.
+ if (~key.indexOf('.')) return
+ if (typeof argv[key] === 'undefined') argv[key] = undefined
+ })
+ return argv
+ }
+
function applyDefaultsAndAliases (obj, aliases, defaults) {
Object.keys(defaults).forEach(function (key) {
if (!hasKey(obj, key.split('.'))) {
@@ -551,22 +658,51 @@ function parse (args, opts) {
if (!configuration['dot-notation']) keys = [keys.join('.')]
- keys.slice(0, -1).forEach(function (key) {
- if (o[key] === undefined) o[key] = {}
- o = o[key]
+ keys.slice(0, -1).forEach(function (key, index) {
+ // TODO(bcoe): in the next major version of yargs, switch to
+ // Object.create(null) for dot notation:
+ key = sanitizeKey(key)
+
+ if (typeof o === 'object' && o[key] === undefined) {
+ o[key] = {}
+ }
+
+ if (typeof o[key] !== 'object' || Array.isArray(o[key])) {
+ // ensure that o[key] is an array, and that the last item is an empty object.
+ if (Array.isArray(o[key])) {
+ o[key].push({})
+ } else {
+ o[key] = [o[key], {}]
+ }
+
+ // we want to update the empty object at the end of the o[key] array, so set o to that object
+ o = o[key][o[key].length - 1]
+ } else {
+ o = o[key]
+ }
})
- var key = keys[keys.length - 1]
+ // TODO(bcoe): in the next major version of yargs, switch to
+ // Object.create(null) for dot notation:
+ const key = sanitizeKey(keys[keys.length - 1])
- var isTypeArray = checkAllAliases(key, flags.arrays)
- var isValueArray = Array.isArray(value)
- var duplicate = configuration['duplicate-arguments-array']
+ const isTypeArray = checkAllAliases(keys.join('.'), flags.arrays)
+ const isValueArray = Array.isArray(value)
+ let duplicate = configuration['duplicate-arguments-array']
+
+ // nargs has higher priority than duplicate
+ if (!duplicate && checkAllAliases(key, flags.nargs)) {
+ duplicate = true
+ if ((!isUndefined(o[key]) && flags.nargs[key] === 1) || (Array.isArray(o[key]) && o[key].length === flags.nargs[key])) {
+ o[key] = undefined
+ }
+ }
if (value === increment) {
o[key] = increment(o[key])
} else if (Array.isArray(o[key])) {
if (duplicate && isTypeArray && isValueArray) {
- o[key] = configuration['flatten-duplicate-arrays'] ? o[key].concat(value) : [o[key]].concat([value])
+ o[key] = configuration['flatten-duplicate-arrays'] ? o[key].concat(value) : (Array.isArray(o[key][0]) ? o[key] : [o[key]]).concat([value])
} else if (!duplicate && Boolean(isTypeArray) === Boolean(isValueArray)) {
o[key] = value
} else {
@@ -582,8 +718,8 @@ function parse (args, opts) {
}
// extend the aliases list with inferred aliases.
- function extendAliases () {
- Array.prototype.slice.call(arguments).forEach(function (obj) {
+ function extendAliases (...args) {
+ args.forEach(function (obj) {
Object.keys(obj || {}).forEach(function (key) {
// short-circuit if we've already added a key
// to the aliases array, for example it might
@@ -595,8 +731,20 @@ function parse (args, opts) {
flags.aliases[key].concat(key).forEach(function (x) {
if (/-/.test(x) && configuration['camel-case-expansion']) {
var c = camelCase(x)
- flags.aliases[key].push(c)
- newAliases[c] = true
+ if (c !== key && flags.aliases[key].indexOf(c) === -1) {
+ flags.aliases[key].push(c)
+ newAliases[c] = true
+ }
+ }
+ })
+ // For "--optionName", also set argv['option-name']
+ flags.aliases[key].concat(key).forEach(function (x) {
+ if (x.length > 1 && /[A-Z]/.test(x) && configuration['camel-case-expansion']) {
+ var c = decamelize(x, '-')
+ if (c !== key && flags.aliases[key].indexOf(c) === -1) {
+ flags.aliases[key].push(c)
+ newAliases[c] = true
+ }
}
})
flags.aliases[key].forEach(function (x) {
@@ -632,6 +780,18 @@ function parse (args, opts) {
})
}
+ // make a best effor to pick a default value
+ // for an option based on name and type.
+ function defaultValue (key) {
+ if (!checkAllAliases(key, flags.bools) &&
+ !checkAllAliases(key, flags.counts) &&
+ `${key}` in defaults) {
+ return defaults[key]
+ } else {
+ return defaultForType(guessType(key))
+ }
+ }
+
// return a default value, given the type of a flag.,
// e.g., key of type 'string' will default to '', rather than 'true'.
function defaultForType (type) {
@@ -646,7 +806,7 @@ function parse (args, opts) {
}
// given a flag, enforce a default type.
- function guessType (key, flags) {
+ function guessType (key) {
var type = 'boolean'
if (checkAllAliases(key, flags.strings)) type = 'string'
@@ -657,10 +817,14 @@ function parse (args, opts) {
}
function isNumber (x) {
- if (!configuration['parse-numbers']) return false
+ if (x === null || x === undefined) return false
+ // if loaded from config, may already be a number.
if (typeof x === 'number') return true
+ // hexadecimal.
if (/^0x[0-9a-f]+$/i.test(x)) return true
- return /^[-+]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/.test(x)
+ // don't treat 0123 as a number; as it drops the leading '0'.
+ if (x.length > 1 && x[0] === '0') return false
+ return /^[-]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/.test(x)
}
function isUndefined (num) {
@@ -723,20 +887,6 @@ function combineAliases (aliases) {
return combined
}
-function assign (defaults, configuration) {
- var o = {}
- configuration = configuration || {}
-
- Object.keys(defaults).forEach(function (k) {
- o[k] = defaults[k]
- })
- Object.keys(configuration).forEach(function (k) {
- o[k] = configuration[k]
- })
-
- return o
-}
-
// this function should only be called when a count is given as an arg
// it is NOT called to set a default value
// thus we can start the count at 1 instead of 0
@@ -756,4 +906,11 @@ Parser.detailed = function (args, opts) {
return parse(args.slice(), opts)
}
+// TODO(bcoe): in the next major version of yargs, switch to
+// Object.create(null) for dot notation:
+function sanitizeKey (key) {
+ if (key === '__proto__') return '___proto___'
+ return key
+}
+
module.exports = Parser