diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-12-10 21:51:33 +0100 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-12-10 21:51:33 +0100 |
commit | 0469abd4a9c9270a1fdc962969e36e63699af8b4 (patch) | |
tree | f9864d4a4148621378958794cbbfdc2393733283 /node_modules/yargs-parser/index.js | |
parent | 6947e79bbc258f7bc96af424ddb71a511f0c15a3 (diff) |
upgrade dependencies
Diffstat (limited to 'node_modules/yargs-parser/index.js')
-rw-r--r-- | node_modules/yargs-parser/index.js | 56 |
1 files changed, 37 insertions, 19 deletions
diff --git a/node_modules/yargs-parser/index.js b/node_modules/yargs-parser/index.js index b71faf584..c06d93707 100644 --- a/node_modules/yargs-parser/index.js +++ b/node_modules/yargs-parser/index.js @@ -16,12 +16,16 @@ function parse (args, opts) { '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 }, 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) { @@ -42,6 +46,7 @@ function parse (args, opts) { coercions: {} } var negative = /^-[0-9]+(\.[0-9]+)?/ + var negatedBoolean = new RegExp('^--' + configuration['negation-prefix'] + '(.+)') ;[].concat(opts.array).filter(Boolean).forEach(function (key) { flags.arrays[key] = true @@ -138,8 +143,8 @@ 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. @@ -265,9 +270,7 @@ function parse (args, opts) { } } } else { - argv._.push( - flags.strings['_'] || !isNumber(arg) ? arg : Number(arg) - ) + argv._.push(maybeCoerceNumber('_', arg)) } } @@ -289,8 +292,10 @@ function parse (args, opts) { 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) }) // how many arguments should we consume, based @@ -341,10 +346,8 @@ 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']) { + addNewAlias(key, camelCase(key)) } var value = processValue(key, val) @@ -389,17 +392,23 @@ 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) { // 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 +423,14 @@ 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(parseInt(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) { @@ -558,7 +575,7 @@ function parse (args, opts) { var key = keys[keys.length - 1] - var isTypeArray = checkAllAliases(key, flags.arrays) + var isTypeArray = checkAllAliases(keys.join('.'), flags.arrays) var isValueArray = Array.isArray(value) var duplicate = configuration['duplicate-arguments-array'] @@ -595,7 +612,9 @@ 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) + if (flags.aliases[key].indexOf(c) === -1) { + flags.aliases[key].push(c) + } newAliases[c] = true } }) @@ -657,7 +676,6 @@ function parse (args, opts) { } function isNumber (x) { - if (!configuration['parse-numbers']) return false if (typeof x === 'number') return true if (/^0x[0-9a-f]+$/i.test(x)) return true return /^[-+]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/.test(x) |