123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448 |
- var camelCase = require('camelcase'),
- path = require('path')
- function increment (orig) {
- return orig !== undefined ? orig + 1 : 0
- }
- module.exports = function (args, opts) {
- if (!opts) opts = {}
- var flags = { arrays: {}, bools: {}, strings: {}, counts: {}, normalize: {}, configs: {} }
- ;[].concat(opts['array']).filter(Boolean).forEach(function (key) {
- flags.arrays[key] = true
- })
- ;[].concat(opts['boolean']).filter(Boolean).forEach(function (key) {
- flags.bools[key] = true
- })
- ;[].concat(opts.string).filter(Boolean).forEach(function (key) {
- flags.strings[key] = true
- })
- ;[].concat(opts.count).filter(Boolean).forEach(function (key) {
- flags.counts[key] = true
- })
- ;[].concat(opts.normalize).filter(Boolean).forEach(function (key) {
- flags.normalize[key] = true
- })
- ;[].concat(opts.config).filter(Boolean).forEach(function (key) {
- flags.configs[key] = true
- })
- var aliases = {},
- newAliases = {}
- extendAliases(opts.key)
- extendAliases(opts.alias)
- var defaults = opts['default'] || {}
- Object.keys(defaults).forEach(function (key) {
- if (/-/.test(key) && !opts.alias[key]) {
- aliases[key] = aliases[key] || []
- }
- (aliases[key] || []).forEach(function (alias) {
- defaults[alias] = defaults[key]
- })
- })
- var argv = { _: [] }
- Object.keys(flags.bools).forEach(function (key) {
- setArg(key, !(key in defaults) ? false : defaults[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],
- broken,
- key,
- letters,
- m,
- next,
- value
-
- if (arg.match(/^--.+=/)) {
-
-
-
- m = arg.match(/^--([^=]+)=([\s\S]*)$/)
-
- if (checkAllAliases(m[1], opts.narg)) {
- args.splice(i + 1, m[1], m[2])
- i = eatNargs(i, m[1], args)
-
- } else if (checkAllAliases(m[1], flags.arrays) && args.length > i + 1) {
- args.splice(i + 1, m[1], m[2])
- i = eatArray(i, m[1], args)
- } else {
- setArg(m[1], m[2])
- }
- } else if (arg.match(/^--no-.+/)) {
- key = arg.match(/^--no-(.+)/)[1]
- setArg(key, false)
-
- } else if (arg.match(/^--.+/)) {
- key = arg.match(/^--(.+)/)[1]
-
- if (checkAllAliases(key, opts.narg)) {
- i = eatNargs(i, key, args)
-
- } else if (checkAllAliases(key, flags.arrays) && args.length > i + 1) {
- i = eatArray(i, key, args)
- } else {
- next = args[i + 1]
- if (next !== undefined && !next.match(/^-/)
- && !checkAllAliases(key, flags.bools)
- && !checkAllAliases(key, flags.counts)) {
- setArg(key, next)
- i++
- } else if (/^(true|false)$/.test(next)) {
- setArg(key, next)
- i++
- } else {
- setArg(key, defaultForType(guessType(key, flags)))
- }
- }
-
- } else if (arg.match(/^-.\..+=/)) {
- m = arg.match(/^-([^=]+)=([\s\S]*)$/)
- setArg(m[1], m[2])
-
- } else if (arg.match(/^-.\..+/)) {
- next = args[i + 1]
- key = arg.match(/^-(.\..+)/)[1]
- if (next !== undefined && !next.match(/^-/)
- && !checkAllAliases(key, flags.bools)
- && !checkAllAliases(key, flags.counts)) {
- setArg(key, next)
- i++
- } else {
- setArg(key, defaultForType(guessType(key, flags)))
- }
- } else if (arg.match(/^-[^-]+/)) {
- letters = arg.slice(1, -1).split('')
- broken = false
- for (var j = 0; j < letters.length; j++) {
- next = arg.slice(j + 2)
- if (letters[j + 1] && letters[j + 1] === '=') {
- value = arg.slice(j + 3)
- key = letters[j]
-
- if (checkAllAliases(letters[j], opts.narg)) {
- args.splice(i + 1, 0, value)
- i = eatNargs(i, key, args)
-
- } else if (checkAllAliases(key, flags.arrays) && args.length > i + 1) {
- args.splice(i + 1, 0, value)
- i = eatArray(i, key, args)
- } else {
- setArg(key, value)
- }
- broken = true
- break
- }
- if (next === '-') {
- setArg(letters[j], next)
- continue
- }
- if (/[A-Za-z]/.test(letters[j])
- && /-?\d+(\.\d*)?(e-?\d+)?$/.test(next)) {
- setArg(letters[j], next)
- broken = true
- break
- }
- if (letters[j + 1] && letters[j + 1].match(/\W/)) {
- setArg(letters[j], arg.slice(j + 2))
- broken = true
- break
- } else {
- setArg(letters[j], defaultForType(guessType(letters[j], flags)))
- }
- }
- key = arg.slice(-1)[0]
- if (!broken && key !== '-') {
-
- if (checkAllAliases(key, opts.narg)) {
- i = eatNargs(i, key, args)
-
- } else if (checkAllAliases(key, flags.arrays) && args.length > i + 1) {
- i = eatArray(i, key, args)
- } else {
- if (args[i + 1] && !/^(-|--)[^-]/.test(args[i + 1])
- && !checkAllAliases(key, flags.bools)
- && !checkAllAliases(key, flags.counts)) {
- setArg(key, args[i + 1])
- i++
- } else if (args[i + 1] && /true|false/.test(args[i + 1])) {
- setArg(key, args[i + 1])
- i++
- } else {
- setArg(key, defaultForType(guessType(key, flags)))
- }
- }
- }
- } else {
- argv._.push(
- flags.strings['_'] || !isNumber(arg) ? arg : Number(arg)
- )
- }
- }
- setConfig(argv)
- applyDefaultsAndAliases(argv, aliases, defaults)
- Object.keys(flags.counts).forEach(function (key) {
- setArg(key, defaults[key])
- })
- notFlags.forEach(function (key) {
- argv._.push(key)
- })
-
-
- function eatNargs (i, key, args) {
- var toEat = checkAllAliases(key, opts.narg)
- if (args.length - (i + 1) < toEat) throw Error('not enough arguments following: ' + key)
- for (var ii = i + 1; ii < (toEat + i + 1); ii++) {
- setArg(key, args[ii])
- }
- return (i + toEat)
- }
-
-
-
- function eatArray (i, key, args) {
- for (var ii = i + 1; ii < args.length; ii++) {
- if (/^-/.test(args[ii])) break
- i = ii
- setArg(key, args[ii])
- }
- return i
- }
- function setArg (key, val) {
-
- if (checkAllAliases(key, flags.bools) || checkAllAliases(key, flags.counts)) {
- if (typeof val === 'string') val = val === 'true'
- }
- if (/-/.test(key) && !(aliases[key] && aliases[key].length)) {
- var c = camelCase(key)
- aliases[key] = [c]
- newAliases[c] = true
- }
- var value = !checkAllAliases(key, flags.strings) && isNumber(val) ? Number(val) : val
- if (checkAllAliases(key, flags.counts)) {
- value = increment
- }
- var splitKey = key.split('.')
- setKey(argv, splitKey, value)
- ;(aliases[splitKey[0]] || []).forEach(function (x) {
- x = x.split('.')
-
-
- if (splitKey.length > 1) {
- var a = [].concat(splitKey)
- a.shift()
- x = x.concat(a)
- }
- setKey(argv, x, value)
- })
- var keys = [key].concat(aliases[key] || [])
- for (var i = 0, l = keys.length; i < l; i++) {
- if (flags.normalize[keys[i]]) {
- keys.forEach(function (key) {
- argv.__defineSetter__(key, function (v) {
- val = path.normalize(v)
- })
- argv.__defineGetter__(key, function () {
- return typeof val === 'string' ?
- path.normalize(val) : val
- })
- })
- break
- }
- }
- }
-
-
- function setConfig (argv) {
- var configLookup = {}
-
-
- applyDefaultsAndAliases(configLookup, aliases, defaults)
- Object.keys(flags.configs).forEach(function (configKey) {
- var configPath = argv[configKey] || configLookup[configKey]
- if (configPath) {
- try {
- var config = require(path.resolve(process.cwd(), configPath))
- Object.keys(config).forEach(function (key) {
-
-
- if (argv[key] === undefined) {
- delete argv[key]
- setArg(key, config[key])
- }
- })
- } catch (ex) {
- throw Error('invalid json config file: ' + configPath)
- }
- }
- })
- }
- function applyDefaultsAndAliases (obj, aliases, defaults) {
- Object.keys(defaults).forEach(function (key) {
- if (!hasKey(obj, key.split('.'))) {
- setKey(obj, key.split('.'), defaults[key])
- ;(aliases[key] || []).forEach(function (x) {
- setKey(obj, x.split('.'), defaults[key])
- })
- }
- })
- }
- function hasKey (obj, keys) {
- var o = obj
- keys.slice(0, -1).forEach(function (key) {
- o = (o[key] || {})
- })
- var key = keys[keys.length - 1]
- return key in o
- }
- function setKey (obj, keys, value) {
- var o = obj
- keys.slice(0, -1).forEach(function (key) {
- if (o[key] === undefined) o[key] = {}
- o = o[key]
- })
- var key = keys[keys.length - 1]
- if (value === increment) {
- o[key] = increment(o[key])
- } else if (o[key] === undefined && checkAllAliases(key, flags.arrays)) {
- o[key] = Array.isArray(value) ? value : [value]
- } else if (o[key] === undefined || typeof o[key] === 'boolean') {
- o[key] = value
- } else if (Array.isArray(o[key])) {
- o[key].push(value)
- } else {
- o[key] = [ o[key], value ]
- }
- }
-
- function extendAliases (obj) {
- Object.keys(obj || {}).forEach(function (key) {
- aliases[key] = [].concat(opts.alias[key] || [])
-
- aliases[key].concat(key).forEach(function (x) {
- if (/-/.test(x)) {
- var c = camelCase(x)
- aliases[key].push(c)
- newAliases[c] = true
- }
- })
- aliases[key].forEach(function (x) {
- aliases[x] = [key].concat(aliases[key].filter(function (y) {
- return x !== y
- }))
- })
- })
- }
-
- function checkAllAliases (key, flag) {
- var isSet = false,
- toCheck = [].concat(aliases[key] || [], key)
- toCheck.forEach(function (key) {
- if (flag[key]) isSet = flag[key]
- })
- return isSet
- }
-
-
- function defaultForType (type) {
- var def = {
- boolean: true,
- string: '',
- array: []
- }
- return def[type]
- }
-
- function guessType (key, flags) {
- var type = 'boolean'
- if (flags.strings && flags.strings[key]) type = 'string'
- else if (flags.arrays && flags.arrays[key]) type = 'array'
- return type
- }
- function isNumber (x) {
- if (typeof x === 'number') return true
- if (/^0x[0-9a-f]+$/i.test(x)) return true
- return /^[-+]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/.test(x)
- }
- return {
- argv: argv,
- aliases: aliases,
- newAliases: newAliases
- }
- }
|