LINXD-1029: Add relative path to simulator
This commit is contained in:
95
node_modules/yargs/lib/completion.js
generated
vendored
95
node_modules/yargs/lib/completion.js
generated
vendored
@ -1,32 +1,36 @@
|
||||
var fs = require('fs')
|
||||
var path = require('path')
|
||||
'use strict'
|
||||
const path = require('path')
|
||||
|
||||
// add bash completions to your
|
||||
// yargs-powered applications.
|
||||
module.exports = function (yargs, usage) {
|
||||
var self = {
|
||||
module.exports = function completion (yargs, usage, command) {
|
||||
const self = {
|
||||
completionKey: 'get-yargs-completions'
|
||||
}
|
||||
|
||||
const zshShell = (process.env.SHELL && process.env.SHELL.indexOf('zsh') !== -1) ||
|
||||
(process.env.ZSH_NAME && process.env.ZSH_NAME.indexOf('zsh') !== -1)
|
||||
// get a list of completion commands.
|
||||
self.getCompletion = function (done) {
|
||||
var completions = []
|
||||
var current = process.argv[process.argv.length - 1]
|
||||
var previous = process.argv.slice(process.argv.indexOf('--' + self.completionKey) + 1)
|
||||
var argv = yargs.parse(previous)
|
||||
// 'args' is the array of strings from the line to be completed
|
||||
self.getCompletion = function getCompletion (args, done) {
|
||||
const completions = []
|
||||
const current = args.length ? args[args.length - 1] : ''
|
||||
const argv = yargs.parse(args, true)
|
||||
const aliases = yargs.parsed.aliases
|
||||
const parentCommands = yargs.getContext().commands
|
||||
|
||||
// a custom completion function can be provided
|
||||
// to completion().
|
||||
if (completionFunction) {
|
||||
if (completionFunction.length < 3) {
|
||||
var result = completionFunction(current, argv)
|
||||
const result = completionFunction(current, argv)
|
||||
|
||||
// promise based completion function.
|
||||
if (typeof result.then === 'function') {
|
||||
return result.then(function (list) {
|
||||
process.nextTick(function () { done(list) })
|
||||
}).catch(function (err) {
|
||||
process.nextTick(function () { throw err })
|
||||
return result.then((list) => {
|
||||
process.nextTick(() => { done(list) })
|
||||
}).catch((err) => {
|
||||
process.nextTick(() => { throw err })
|
||||
})
|
||||
}
|
||||
|
||||
@ -34,30 +38,52 @@ module.exports = function (yargs, usage) {
|
||||
return done(result)
|
||||
} else {
|
||||
// asynchronous completion function
|
||||
return completionFunction(current, argv, function (completions) {
|
||||
return completionFunction(current, argv, (completions) => {
|
||||
done(completions)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
var handlers = yargs.getCommandHandlers()
|
||||
for (var i = 0, ii = previous.length; i < ii; ++i) {
|
||||
if (handlers[previous[i]]) {
|
||||
return handlers[previous[i]](yargs.reset())
|
||||
const handlers = command.getCommandHandlers()
|
||||
for (let i = 0, ii = args.length; i < ii; ++i) {
|
||||
if (handlers[args[i]] && handlers[args[i]].builder) {
|
||||
const builder = handlers[args[i]].builder
|
||||
if (typeof builder === 'function') {
|
||||
const y = yargs.reset()
|
||||
builder(y)
|
||||
return y.argv
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!current.match(/^-/)) {
|
||||
usage.getCommands().forEach(function (command) {
|
||||
if (previous.indexOf(command[0]) === -1) {
|
||||
completions.push(command[0])
|
||||
if (!current.match(/^-/) && parentCommands[parentCommands.length - 1] !== current) {
|
||||
usage.getCommands().forEach((usageCommand) => {
|
||||
const commandName = command.parseCommand(usageCommand[0]).cmd
|
||||
if (args.indexOf(commandName) === -1) {
|
||||
if (!zshShell) {
|
||||
completions.push(commandName)
|
||||
} else {
|
||||
const desc = usageCommand[1] || ''
|
||||
completions.push(commandName.replace(/:/g, '\\:') + ':' + desc)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (current.match(/^-/)) {
|
||||
Object.keys(yargs.getOptions().key).forEach(function (key) {
|
||||
completions.push('--' + key)
|
||||
if (current.match(/^-/) || (current === '' && completions.length === 0)) {
|
||||
const descs = usage.getDescriptions()
|
||||
Object.keys(yargs.getOptions().key).forEach((key) => {
|
||||
// If the key and its aliases aren't in 'args', add the key to 'completions'
|
||||
const keyAndAliases = [key].concat(aliases[key] || [])
|
||||
const notInArgs = keyAndAliases.every(val => args.indexOf(`--${val}`) === -1)
|
||||
if (notInArgs) {
|
||||
if (!zshShell) {
|
||||
completions.push(`--${key}`)
|
||||
} else {
|
||||
const desc = descs[key] || ''
|
||||
completions.push(`--${key.replace(/:/g, '\\:')}:${desc.replace('__yargsString__:', '')}`)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@ -65,25 +91,24 @@ module.exports = function (yargs, usage) {
|
||||
}
|
||||
|
||||
// generate the completion script to add to your .bashrc.
|
||||
self.generateCompletionScript = function ($0) {
|
||||
var script = fs.readFileSync(
|
||||
path.resolve(__dirname, '../completion.sh.hbs'),
|
||||
'utf-8'
|
||||
)
|
||||
var name = path.basename($0)
|
||||
self.generateCompletionScript = function generateCompletionScript ($0, cmd) {
|
||||
const templates = require('./completion-templates')
|
||||
let script = zshShell ? templates.completionZshTemplate : templates.completionShTemplate
|
||||
const name = path.basename($0)
|
||||
|
||||
// add ./to applications not yet installed as bin.
|
||||
if ($0.match(/\.js$/)) $0 = './' + $0
|
||||
if ($0.match(/\.js$/)) $0 = `./${$0}`
|
||||
|
||||
script = script.replace(/{{app_name}}/g, name)
|
||||
script = script.replace(/{{completion_command}}/g, cmd)
|
||||
return script.replace(/{{app_path}}/g, $0)
|
||||
}
|
||||
|
||||
// register a function to perform your own custom
|
||||
// completions., this function can be either
|
||||
// synchrnous or asynchronous.
|
||||
var completionFunction = null
|
||||
self.registerFunction = function (fn) {
|
||||
let completionFunction = null
|
||||
self.registerFunction = (fn) => {
|
||||
completionFunction = fn
|
||||
}
|
||||
|
||||
|
520
node_modules/yargs/lib/parser.js
generated
vendored
520
node_modules/yargs/lib/parser.js
generated
vendored
@ -1,520 +0,0 @@
|
||||
// fancy-pants parsing of argv, originally forked
|
||||
// from minimist: https://www.npmjs.com/package/minimist
|
||||
var camelCase = require('camelcase')
|
||||
var path = require('path')
|
||||
|
||||
function increment (orig) {
|
||||
return orig !== undefined ? orig + 1 : 0
|
||||
}
|
||||
|
||||
module.exports = function (args, opts, y18n) {
|
||||
if (!opts) opts = {}
|
||||
|
||||
var __ = y18n.__
|
||||
var error = null
|
||||
var flags = { arrays: {}, bools: {}, strings: {}, counts: {}, normalize: {}, configs: {}, defaulted: {} }
|
||||
|
||||
;[].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
|
||||
})
|
||||
|
||||
Object.keys(opts.config).forEach(function (k) {
|
||||
flags.configs[k] = opts.config[k]
|
||||
})
|
||||
|
||||
var aliases = {}
|
||||
var newAliases = {}
|
||||
|
||||
extendAliases(opts.key)
|
||||
extendAliases(opts.alias)
|
||||
extendAliases(opts.default)
|
||||
|
||||
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])
|
||||
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]
|
||||
var broken
|
||||
var key
|
||||
var letters
|
||||
var m
|
||||
var next
|
||||
var value
|
||||
|
||||
// -- seperated by =
|
||||
if (arg.match(/^--.+=/)) {
|
||||
// Using [\s\S] instead of . because js doesn't support the
|
||||
// 'dotall' regex modifier. See:
|
||||
// http://stackoverflow.com/a/1068308/13216
|
||||
m = arg.match(/^--([^=]+)=([\s\S]*)$/)
|
||||
|
||||
// nargs format = '--f=monkey washing cat'
|
||||
if (checkAllAliases(m[1], opts.narg)) {
|
||||
args.splice(i + 1, m[1], m[2])
|
||||
i = eatNargs(i, m[1], args)
|
||||
// arrays format = '--f=a b c'
|
||||
} 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)
|
||||
|
||||
// -- seperated by space.
|
||||
} else if (arg.match(/^--.+/)) {
|
||||
key = arg.match(/^--(.+)/)[1]
|
||||
|
||||
// nargs format = '--foo a b c'
|
||||
if (checkAllAliases(key, opts.narg)) {
|
||||
i = eatNargs(i, key, args)
|
||||
// array format = '--foo a b c'
|
||||
} 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)))
|
||||
}
|
||||
}
|
||||
|
||||
// dot-notation flag seperated by '='.
|
||||
} else if (arg.match(/^-.\..+=/)) {
|
||||
m = arg.match(/^-([^=]+)=([\s\S]*)$/)
|
||||
setArg(m[1], m[2])
|
||||
|
||||
// dot-notation flag seperated by space.
|
||||
} 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]
|
||||
|
||||
// nargs format = '-f=monkey washing cat'
|
||||
if (checkAllAliases(letters[j], opts.narg)) {
|
||||
args.splice(i + 1, 0, value)
|
||||
i = eatNargs(i, key, args)
|
||||
// array format = '-f=a b c'
|
||||
} 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 !== '-') {
|
||||
// nargs format = '-f a b c'
|
||||
if (checkAllAliases(key, opts.narg)) {
|
||||
i = eatNargs(i, key, args)
|
||||
// array format = '-f a b c'
|
||||
} 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)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// order of precedence:
|
||||
// 1. command line arg
|
||||
// 2. value from config file
|
||||
// 3. value from env var
|
||||
// 4. configured default value
|
||||
applyEnvVars(opts, argv, true) // special case: check env vars that point to config file
|
||||
setConfig(argv)
|
||||
applyEnvVars(opts, argv, false)
|
||||
applyDefaultsAndAliases(argv, aliases, defaults)
|
||||
|
||||
Object.keys(flags.counts).forEach(function (key) {
|
||||
setArg(key, defaults[key])
|
||||
})
|
||||
|
||||
notFlags.forEach(function (key) {
|
||||
argv._.push(key)
|
||||
})
|
||||
|
||||
// how many arguments should we consume, based
|
||||
// on the nargs option?
|
||||
function eatNargs (i, key, args) {
|
||||
var toEat = checkAllAliases(key, opts.narg)
|
||||
|
||||
if (args.length - (i + 1) < toEat) error = Error(__('Not enough arguments following: %s', key))
|
||||
|
||||
for (var ii = i + 1; ii < (toEat + i + 1); ii++) {
|
||||
setArg(key, args[ii])
|
||||
}
|
||||
|
||||
return (i + toEat)
|
||||
}
|
||||
|
||||
// if an option is an array, eat all non-hyphenated arguments
|
||||
// following it... YUM!
|
||||
// e.g., --foo apple banana cat becomes ["apple", "banana", "cat"]
|
||||
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) {
|
||||
unsetDefaulted(key)
|
||||
|
||||
// handle parsing boolean arguments --foo=true --bar false.
|
||||
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)
|
||||
|
||||
// alias references an inner-value within
|
||||
// a dot-notation object. see #279.
|
||||
if (~key.indexOf('.') && aliases[key]) {
|
||||
aliases[key].forEach(function (x) {
|
||||
x = x.split('.')
|
||||
setKey(argv, x, value)
|
||||
})
|
||||
}
|
||||
|
||||
;(aliases[splitKey[0]] || []).forEach(function (x) {
|
||||
x = x.split('.')
|
||||
|
||||
// handle populating dot notation for both
|
||||
// the key and its aliases.
|
||||
if (splitKey.length > 1) {
|
||||
var a = [].concat(splitKey)
|
||||
a.shift() // nuke the old key.
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// set args from config.json file, this should be
|
||||
// applied last so that defaults can be applied.
|
||||
function setConfig (argv) {
|
||||
var configLookup = {}
|
||||
|
||||
// expand defaults/aliases, in-case any happen to reference
|
||||
// the config.json file.
|
||||
applyDefaultsAndAliases(configLookup, aliases, defaults)
|
||||
|
||||
Object.keys(flags.configs).forEach(function (configKey) {
|
||||
var configPath = argv[configKey] || configLookup[configKey]
|
||||
if (configPath) {
|
||||
try {
|
||||
var config = null
|
||||
var resolvedConfigPath = path.resolve(process.cwd(), configPath)
|
||||
|
||||
if (typeof flags.configs[configKey] === 'function') {
|
||||
try {
|
||||
config = flags.configs[configKey](resolvedConfigPath)
|
||||
} catch (e) {
|
||||
config = e
|
||||
}
|
||||
if (config instanceof Error) {
|
||||
error = config
|
||||
return
|
||||
}
|
||||
} else {
|
||||
config = require(resolvedConfigPath)
|
||||
}
|
||||
|
||||
Object.keys(config).forEach(function (key) {
|
||||
// setting arguments via CLI takes precedence over
|
||||
// values within the config file.
|
||||
if (argv[key] === undefined || (flags.defaulted[key])) {
|
||||
delete argv[key]
|
||||
setArg(key, config[key])
|
||||
}
|
||||
})
|
||||
} catch (ex) {
|
||||
if (argv[configKey]) error = Error(__('Invalid JSON config file: %s', configPath))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function applyEnvVars (opts, argv, configOnly) {
|
||||
if (typeof opts.envPrefix === 'undefined') return
|
||||
|
||||
var prefix = typeof opts.envPrefix === 'string' ? opts.envPrefix : ''
|
||||
Object.keys(process.env).forEach(function (envVar) {
|
||||
if (prefix === '' || envVar.lastIndexOf(prefix, 0) === 0) {
|
||||
var key = camelCase(envVar.substring(prefix.length))
|
||||
if (((configOnly && flags.configs[key]) || !configOnly) && (!(key in argv) || flags.defaulted[key])) {
|
||||
setArg(key, process.env[envVar])
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
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) {
|
||||
if (hasKey(obj, x.split('.'))) return
|
||||
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]
|
||||
|
||||
if (typeof o !== 'object') return false
|
||||
else 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 ]
|
||||
}
|
||||
}
|
||||
|
||||
// extend the aliases list with inferred aliases.
|
||||
function extendAliases (obj) {
|
||||
Object.keys(obj || {}).forEach(function (key) {
|
||||
// short-circuit if we've already added a key
|
||||
// to the aliases array, for example it might
|
||||
// exist in both 'opts.default' and 'opts.key'.
|
||||
if (aliases[key]) return
|
||||
|
||||
aliases[key] = [].concat(opts.alias[key] || [])
|
||||
// For "--option-name", also set argv.optionName
|
||||
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
|
||||
}))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// check if a flag is set for any of a key's aliases.
|
||||
function checkAllAliases (key, flag) {
|
||||
var isSet = false
|
||||
var toCheck = [].concat(aliases[key] || [], key)
|
||||
|
||||
toCheck.forEach(function (key) {
|
||||
if (flag[key]) isSet = flag[key]
|
||||
})
|
||||
|
||||
return isSet
|
||||
}
|
||||
|
||||
function setDefaulted (key) {
|
||||
[].concat(aliases[key] || [], key).forEach(function (k) {
|
||||
flags.defaulted[k] = true
|
||||
})
|
||||
}
|
||||
|
||||
function unsetDefaulted (key) {
|
||||
[].concat(aliases[key] || [], key).forEach(function (k) {
|
||||
delete flags.defaulted[k]
|
||||
})
|
||||
}
|
||||
|
||||
// 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) {
|
||||
var def = {
|
||||
boolean: true,
|
||||
string: '',
|
||||
array: []
|
||||
}
|
||||
|
||||
return def[type]
|
||||
}
|
||||
|
||||
// given a flag, enforce a default 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,
|
||||
error: error,
|
||||
newAliases: newAliases
|
||||
}
|
||||
}
|
32
node_modules/yargs/lib/tokenize-arg-string.js
generated
vendored
32
node_modules/yargs/lib/tokenize-arg-string.js
generated
vendored
@ -1,32 +0,0 @@
|
||||
// take an un-split argv string and tokenize it.
|
||||
module.exports = function (argString) {
|
||||
var i = 0
|
||||
var c = null
|
||||
var opening = null
|
||||
var args = []
|
||||
|
||||
for (var ii = 0; ii < argString.length; ii++) {
|
||||
c = argString.charAt(ii)
|
||||
|
||||
// split on spaces unless we're in quotes.
|
||||
if (c === ' ' && !opening) {
|
||||
i++
|
||||
continue
|
||||
}
|
||||
|
||||
// don't split the string if we're in matching
|
||||
// opening or closing single and double quotes.
|
||||
if (c === opening) {
|
||||
opening = null
|
||||
continue
|
||||
} else if ((c === "'" || c === '"') && !opening) {
|
||||
opening = c
|
||||
continue
|
||||
}
|
||||
|
||||
if (!args[i]) args[i] = ''
|
||||
args[i] += c
|
||||
}
|
||||
|
||||
return args
|
||||
}
|
472
node_modules/yargs/lib/usage.js
generated
vendored
472
node_modules/yargs/lib/usage.js
generated
vendored
@ -1,23 +1,26 @@
|
||||
'use strict'
|
||||
// this file handles outputting usage instructions,
|
||||
// failures, etc. keeps logging in one place.
|
||||
var cliui = require('cliui')
|
||||
var decamelize = require('decamelize')
|
||||
var stringWidth = require('string-width')
|
||||
var wsize = require('window-size')
|
||||
const decamelize = require('decamelize')
|
||||
const stringWidth = require('string-width')
|
||||
const objFilter = require('./obj-filter')
|
||||
const path = require('path')
|
||||
const setBlocking = require('set-blocking')
|
||||
const YError = require('./yerror')
|
||||
|
||||
module.exports = function (yargs, y18n) {
|
||||
var __ = y18n.__
|
||||
var self = {}
|
||||
module.exports = function usage (yargs, y18n) {
|
||||
const __ = y18n.__
|
||||
const self = {}
|
||||
|
||||
// methods for ouputting/building failure message.
|
||||
var fails = []
|
||||
self.failFn = function (f) {
|
||||
const fails = []
|
||||
self.failFn = function failFn (f) {
|
||||
fails.push(f)
|
||||
}
|
||||
|
||||
var failMessage = null
|
||||
var showHelpOnFail = true
|
||||
self.showHelpOnFail = function (enabled, message) {
|
||||
let failMessage = null
|
||||
let showHelpOnFail = true
|
||||
self.showHelpOnFail = function showHelpOnFailFn (enabled, message) {
|
||||
if (typeof enabled === 'string') {
|
||||
message = enabled
|
||||
enabled = true
|
||||
@ -29,104 +32,170 @@ module.exports = function (yargs, y18n) {
|
||||
return self
|
||||
}
|
||||
|
||||
var failureOutput = false
|
||||
self.fail = function (msg) {
|
||||
let failureOutput = false
|
||||
self.fail = function fail (msg, err) {
|
||||
const logger = yargs._getLoggerInstance()
|
||||
|
||||
if (fails.length) {
|
||||
fails.forEach(function (f) {
|
||||
f(msg)
|
||||
})
|
||||
for (let i = fails.length - 1; i >= 0; --i) {
|
||||
fails[i](msg, err, self)
|
||||
}
|
||||
} else {
|
||||
if (yargs.getExitProcess()) setBlocking(true)
|
||||
|
||||
// don't output failure message more than once
|
||||
if (!failureOutput) {
|
||||
failureOutput = true
|
||||
if (showHelpOnFail) yargs.showHelp('error')
|
||||
if (msg) console.error(msg)
|
||||
if (showHelpOnFail) {
|
||||
yargs.showHelp('error')
|
||||
logger.error()
|
||||
}
|
||||
if (msg || err) logger.error(msg || err)
|
||||
if (failMessage) {
|
||||
if (msg) console.error('')
|
||||
console.error(failMessage)
|
||||
if (msg || err) logger.error('')
|
||||
logger.error(failMessage)
|
||||
}
|
||||
}
|
||||
|
||||
err = err || new YError(msg)
|
||||
if (yargs.getExitProcess()) {
|
||||
process.exit(1)
|
||||
return yargs.exit(1)
|
||||
} else if (yargs._hasParseCallback()) {
|
||||
return yargs.exit(1, err)
|
||||
} else {
|
||||
throw new Error(msg)
|
||||
throw err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// methods for ouputting/building help (usage) message.
|
||||
var usage
|
||||
self.usage = function (msg) {
|
||||
usage = msg
|
||||
let usages = []
|
||||
let usageDisabled = false
|
||||
self.usage = (msg, description) => {
|
||||
if (msg === null) {
|
||||
usageDisabled = true
|
||||
usages = []
|
||||
return
|
||||
}
|
||||
usageDisabled = false
|
||||
usages.push([msg, description || ''])
|
||||
return self
|
||||
}
|
||||
self.getUsage = () => {
|
||||
return usages
|
||||
}
|
||||
self.getUsageDisabled = () => {
|
||||
return usageDisabled
|
||||
}
|
||||
|
||||
var examples = []
|
||||
self.example = function (cmd, description) {
|
||||
self.getPositionalGroupName = () => {
|
||||
return __('Positionals:')
|
||||
}
|
||||
|
||||
let examples = []
|
||||
self.example = (cmd, description) => {
|
||||
examples.push([cmd, description || ''])
|
||||
}
|
||||
|
||||
var commands = []
|
||||
self.command = function (cmd, description) {
|
||||
commands.push([cmd, description || ''])
|
||||
}
|
||||
self.getCommands = function () {
|
||||
return commands
|
||||
let commands = []
|
||||
self.command = function command (cmd, description, isDefault, aliases) {
|
||||
// the last default wins, so cancel out any previously set default
|
||||
if (isDefault) {
|
||||
commands = commands.map((cmdArray) => {
|
||||
cmdArray[2] = false
|
||||
return cmdArray
|
||||
})
|
||||
}
|
||||
commands.push([cmd, description || '', isDefault, aliases])
|
||||
}
|
||||
self.getCommands = () => commands
|
||||
|
||||
var descriptions = {}
|
||||
self.describe = function (key, desc) {
|
||||
let descriptions = {}
|
||||
self.describe = function describe (key, desc) {
|
||||
if (typeof key === 'object') {
|
||||
Object.keys(key).forEach(function (k) {
|
||||
Object.keys(key).forEach((k) => {
|
||||
self.describe(k, key[k])
|
||||
})
|
||||
} else {
|
||||
descriptions[key] = desc
|
||||
}
|
||||
}
|
||||
self.getDescriptions = function () {
|
||||
return descriptions
|
||||
self.getDescriptions = () => descriptions
|
||||
|
||||
let epilogs = []
|
||||
self.epilog = (msg) => {
|
||||
epilogs.push(msg)
|
||||
}
|
||||
|
||||
var epilog
|
||||
self.epilog = function (msg) {
|
||||
epilog = msg
|
||||
}
|
||||
|
||||
var wrap = windowWidth()
|
||||
self.wrap = function (cols) {
|
||||
let wrapSet = false
|
||||
let wrap
|
||||
self.wrap = (cols) => {
|
||||
wrapSet = true
|
||||
wrap = cols
|
||||
}
|
||||
|
||||
var deferY18nLookupPrefix = '__yargsString__:'
|
||||
self.deferY18nLookup = function (str) {
|
||||
return deferY18nLookupPrefix + str
|
||||
function getWrap () {
|
||||
if (!wrapSet) {
|
||||
wrap = windowWidth()
|
||||
wrapSet = true
|
||||
}
|
||||
|
||||
return wrap
|
||||
}
|
||||
|
||||
var defaultGroup = 'Options:'
|
||||
self.help = function () {
|
||||
const deferY18nLookupPrefix = '__yargsString__:'
|
||||
self.deferY18nLookup = str => deferY18nLookupPrefix + str
|
||||
|
||||
const defaultGroup = 'Options:'
|
||||
self.help = function help () {
|
||||
if (cachedHelpMessage) return cachedHelpMessage
|
||||
normalizeAliases()
|
||||
|
||||
var demanded = yargs.getDemanded()
|
||||
var groups = yargs.getGroups()
|
||||
var options = yargs.getOptions()
|
||||
var keys = Object.keys(
|
||||
Object.keys(descriptions)
|
||||
.concat(Object.keys(demanded))
|
||||
.concat(Object.keys(options.default))
|
||||
.reduce(function (acc, key) {
|
||||
if (key !== '_') acc[key] = true
|
||||
return acc
|
||||
}, {})
|
||||
)
|
||||
var ui = cliui({
|
||||
width: wrap,
|
||||
wrap: !!wrap
|
||||
// handle old demanded API
|
||||
const base$0 = yargs.customScriptName ? yargs.$0 : path.basename(yargs.$0)
|
||||
const demandedOptions = yargs.getDemandedOptions()
|
||||
const demandedCommands = yargs.getDemandedCommands()
|
||||
const groups = yargs.getGroups()
|
||||
const options = yargs.getOptions()
|
||||
|
||||
let keys = []
|
||||
keys = keys.concat(Object.keys(descriptions))
|
||||
keys = keys.concat(Object.keys(demandedOptions))
|
||||
keys = keys.concat(Object.keys(demandedCommands))
|
||||
keys = keys.concat(Object.keys(options.default))
|
||||
keys = keys.filter(filterHiddenOptions)
|
||||
keys = Object.keys(keys.reduce((acc, key) => {
|
||||
if (key !== '_') acc[key] = true
|
||||
return acc
|
||||
}, {}))
|
||||
|
||||
const theWrap = getWrap()
|
||||
const ui = require('cliui')({
|
||||
width: theWrap,
|
||||
wrap: !!theWrap
|
||||
})
|
||||
|
||||
// the usage string.
|
||||
if (usage) {
|
||||
var u = usage.replace(/\$0/g, yargs.$0)
|
||||
ui.div(u + '\n')
|
||||
if (!usageDisabled) {
|
||||
if (usages.length) {
|
||||
// user-defined usage.
|
||||
usages.forEach((usage) => {
|
||||
ui.div(`${usage[0].replace(/\$0/g, base$0)}`)
|
||||
if (usage[1]) {
|
||||
ui.div({ text: `${usage[1]}`, padding: [1, 0, 0, 0] })
|
||||
}
|
||||
})
|
||||
ui.div()
|
||||
} else if (commands.length) {
|
||||
let u = null
|
||||
// demonstrate how commands are used.
|
||||
if (demandedCommands._) {
|
||||
u = `${base$0} <${__('command')}>\n`
|
||||
} else {
|
||||
u = `${base$0} [${__('command')}]\n`
|
||||
}
|
||||
ui.div(`${u}`)
|
||||
}
|
||||
}
|
||||
|
||||
// your application's commands, i.e., non-option
|
||||
@ -134,11 +203,33 @@ module.exports = function (yargs, y18n) {
|
||||
if (commands.length) {
|
||||
ui.div(__('Commands:'))
|
||||
|
||||
commands.forEach(function (command) {
|
||||
ui.div(
|
||||
{text: command[0], padding: [0, 2, 0, 2], width: maxWidth(commands) + 4},
|
||||
{text: command[1]}
|
||||
const context = yargs.getContext()
|
||||
const parentCommands = context.commands.length ? `${context.commands.join(' ')} ` : ''
|
||||
|
||||
if (yargs.getParserConfiguration()['sort-commands'] === true) {
|
||||
commands = commands.sort((a, b) => a[0].localeCompare(b[0]))
|
||||
}
|
||||
|
||||
commands.forEach((command) => {
|
||||
const commandString = `${base$0} ${parentCommands}${command[0].replace(/^\$0 ?/, '')}` // drop $0 from default commands.
|
||||
ui.span(
|
||||
{
|
||||
text: commandString,
|
||||
padding: [0, 2, 0, 2],
|
||||
width: maxWidth(commands, theWrap, `${base$0}${parentCommands}`) + 4
|
||||
},
|
||||
{ text: command[1] }
|
||||
)
|
||||
const hints = []
|
||||
if (command[2]) hints.push(`[${__('default:').slice(0, -1)}]`) // TODO hacking around i18n here
|
||||
if (command[3] && command[3].length) {
|
||||
hints.push(`[${__('aliases:')} ${command[3].join(', ')}]`)
|
||||
}
|
||||
if (hints.length) {
|
||||
ui.div({ text: hints.join(' '), padding: [0, 0, 0, 2], align: 'right' })
|
||||
} else {
|
||||
ui.div()
|
||||
}
|
||||
})
|
||||
|
||||
ui.div()
|
||||
@ -146,14 +237,10 @@ module.exports = function (yargs, y18n) {
|
||||
|
||||
// perform some cleanup on the keys array, making it
|
||||
// only include top-level keys not their aliases.
|
||||
var aliasKeys = (Object.keys(options.alias) || [])
|
||||
const aliasKeys = (Object.keys(options.alias) || [])
|
||||
.concat(Object.keys(yargs.parsed.newAliases) || [])
|
||||
|
||||
keys = keys.filter(function (key) {
|
||||
return !yargs.parsed.newAliases[key] && aliasKeys.every(function (alias) {
|
||||
return (options.alias[alias] || []).indexOf(key) === -1
|
||||
})
|
||||
})
|
||||
keys = keys.filter(key => !yargs.parsed.newAliases[key] && aliasKeys.every(alias => (options.alias[alias] || []).indexOf(key) === -1))
|
||||
|
||||
// populate 'Options:' group with any keys that have not
|
||||
// explicitly had a group set.
|
||||
@ -161,59 +248,65 @@ module.exports = function (yargs, y18n) {
|
||||
addUngroupedKeys(keys, options.alias, groups)
|
||||
|
||||
// display 'Options:' table along with any custom tables:
|
||||
Object.keys(groups).forEach(function (groupName) {
|
||||
Object.keys(groups).forEach((groupName) => {
|
||||
if (!groups[groupName].length) return
|
||||
|
||||
ui.div(__(groupName))
|
||||
|
||||
// if we've grouped the key 'f', but 'f' aliases 'foobar',
|
||||
// normalizedKeys should contain only 'foobar'.
|
||||
var normalizedKeys = groups[groupName].map(function (key) {
|
||||
const normalizedKeys = groups[groupName].filter(filterHiddenOptions).map((key) => {
|
||||
if (~aliasKeys.indexOf(key)) return key
|
||||
for (var i = 0, aliasKey; (aliasKey = aliasKeys[i]) !== undefined; i++) {
|
||||
for (let i = 0, aliasKey; (aliasKey = aliasKeys[i]) !== undefined; i++) {
|
||||
if (~(options.alias[aliasKey] || []).indexOf(key)) return aliasKey
|
||||
}
|
||||
return key
|
||||
})
|
||||
|
||||
if (normalizedKeys.length < 1) return
|
||||
|
||||
ui.div(__(groupName))
|
||||
|
||||
// actually generate the switches string --foo, -f, --bar.
|
||||
var switches = normalizedKeys.reduce(function (acc, key) {
|
||||
const switches = normalizedKeys.reduce((acc, key) => {
|
||||
acc[key] = [ key ].concat(options.alias[key] || [])
|
||||
.map(function (sw) {
|
||||
return (sw.length > 1 ? '--' : '-') + sw
|
||||
.map(sw => {
|
||||
// for the special positional group don't
|
||||
// add '--' or '-' prefix.
|
||||
if (groupName === self.getPositionalGroupName()) return sw
|
||||
else return (sw.length > 1 ? '--' : '-') + sw
|
||||
})
|
||||
.join(', ')
|
||||
|
||||
return acc
|
||||
}, {})
|
||||
|
||||
normalizedKeys.forEach(function (key) {
|
||||
var kswitch = switches[key]
|
||||
var desc = descriptions[key] || ''
|
||||
var type = null
|
||||
normalizedKeys.forEach((key) => {
|
||||
const kswitch = switches[key]
|
||||
let desc = descriptions[key] || ''
|
||||
let type = null
|
||||
|
||||
if (~desc.lastIndexOf(deferY18nLookupPrefix)) desc = __(desc.substring(deferY18nLookupPrefix.length))
|
||||
|
||||
if (~options.boolean.indexOf(key)) type = '[' + __('boolean') + ']'
|
||||
if (~options.count.indexOf(key)) type = '[' + __('count') + ']'
|
||||
if (~options.string.indexOf(key)) type = '[' + __('string') + ']'
|
||||
if (~options.normalize.indexOf(key)) type = '[' + __('string') + ']'
|
||||
if (~options.array.indexOf(key)) type = '[' + __('array') + ']'
|
||||
if (~options.boolean.indexOf(key)) type = `[${__('boolean')}]`
|
||||
if (~options.count.indexOf(key)) type = `[${__('count')}]`
|
||||
if (~options.string.indexOf(key)) type = `[${__('string')}]`
|
||||
if (~options.normalize.indexOf(key)) type = `[${__('string')}]`
|
||||
if (~options.array.indexOf(key)) type = `[${__('array')}]`
|
||||
if (~options.number.indexOf(key)) type = `[${__('number')}]`
|
||||
|
||||
var extra = [
|
||||
const extra = [
|
||||
type,
|
||||
demanded[key] ? '[' + __('required') + ']' : null,
|
||||
options.choices && options.choices[key] ? '[' + __('choices:') + ' ' +
|
||||
self.stringifiedValues(options.choices[key]) + ']' : null,
|
||||
(key in demandedOptions) ? `[${__('required')}]` : null,
|
||||
options.choices && options.choices[key] ? `[${__('choices:')} ${
|
||||
self.stringifiedValues(options.choices[key])}]` : null,
|
||||
defaultString(options.default[key], options.defaultDescription[key])
|
||||
].filter(Boolean).join(' ')
|
||||
|
||||
ui.span(
|
||||
{text: kswitch, padding: [0, 2, 0, 2], width: maxWidth(switches) + 4},
|
||||
{ text: kswitch, padding: [0, 2, 0, 2], width: maxWidth(switches, theWrap) + 4 },
|
||||
desc
|
||||
)
|
||||
|
||||
if (extra) ui.div({text: extra, padding: [0, 0, 0, 2], align: 'right'})
|
||||
if (extra) ui.div({ text: extra, padding: [0, 0, 0, 2], align: 'right' })
|
||||
else ui.div()
|
||||
})
|
||||
|
||||
@ -224,49 +317,65 @@ module.exports = function (yargs, y18n) {
|
||||
if (examples.length) {
|
||||
ui.div(__('Examples:'))
|
||||
|
||||
examples.forEach(function (example) {
|
||||
example[0] = example[0].replace(/\$0/g, yargs.$0)
|
||||
examples.forEach((example) => {
|
||||
example[0] = example[0].replace(/\$0/g, base$0)
|
||||
})
|
||||
|
||||
examples.forEach(function (example) {
|
||||
ui.div(
|
||||
{text: example[0], padding: [0, 2, 0, 2], width: maxWidth(examples) + 4},
|
||||
example[1]
|
||||
)
|
||||
examples.forEach((example) => {
|
||||
if (example[1] === '') {
|
||||
ui.div(
|
||||
{
|
||||
text: example[0],
|
||||
padding: [0, 2, 0, 2]
|
||||
}
|
||||
)
|
||||
} else {
|
||||
ui.div(
|
||||
{
|
||||
text: example[0],
|
||||
padding: [0, 2, 0, 2],
|
||||
width: maxWidth(examples, theWrap) + 4
|
||||
}, {
|
||||
text: example[1]
|
||||
}
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
ui.div()
|
||||
}
|
||||
|
||||
// the usage string.
|
||||
if (epilog) {
|
||||
var e = epilog.replace(/\$0/g, yargs.$0)
|
||||
ui.div(e + '\n')
|
||||
if (epilogs.length > 0) {
|
||||
const e = epilogs.map(epilog => epilog.replace(/\$0/g, base$0)).join('\n')
|
||||
ui.div(`${e}\n`)
|
||||
}
|
||||
|
||||
return ui.toString()
|
||||
// Remove the trailing white spaces
|
||||
return ui.toString().replace(/\s*$/, '')
|
||||
}
|
||||
|
||||
// return the maximum width of a string
|
||||
// in the left-hand column of a table.
|
||||
function maxWidth (table) {
|
||||
var width = 0
|
||||
function maxWidth (table, theWrap, modifier) {
|
||||
let width = 0
|
||||
|
||||
// table might be of the form [leftColumn],
|
||||
// or {key: leftColumn}}
|
||||
// or {key: leftColumn}
|
||||
if (!Array.isArray(table)) {
|
||||
table = Object.keys(table).map(function (key) {
|
||||
return [table[key]]
|
||||
})
|
||||
table = Object.keys(table).map(key => [table[key]])
|
||||
}
|
||||
|
||||
table.forEach(function (v) {
|
||||
width = Math.max(stringWidth(v[0]), width)
|
||||
table.forEach((v) => {
|
||||
width = Math.max(
|
||||
stringWidth(modifier ? `${modifier} ${v[0]}` : v[0]),
|
||||
width
|
||||
)
|
||||
})
|
||||
|
||||
// if we've enabled 'wrap' we should limit
|
||||
// the max-width of the left-column.
|
||||
if (wrap) width = Math.min(width, parseInt(wrap * 0.5, 10))
|
||||
if (theWrap) width = Math.min(width, parseInt(theWrap * 0.5, 10))
|
||||
|
||||
return width
|
||||
}
|
||||
@ -274,63 +383,76 @@ module.exports = function (yargs, y18n) {
|
||||
// make sure any options set for aliases,
|
||||
// are copied to the keys being aliased.
|
||||
function normalizeAliases () {
|
||||
var demanded = yargs.getDemanded()
|
||||
var options = yargs.getOptions()
|
||||
// handle old demanded API
|
||||
const demandedOptions = yargs.getDemandedOptions()
|
||||
const options = yargs.getOptions()
|
||||
|
||||
;(Object.keys(options.alias) || []).forEach(function (key) {
|
||||
options.alias[key].forEach(function (alias) {
|
||||
;(Object.keys(options.alias) || []).forEach((key) => {
|
||||
options.alias[key].forEach((alias) => {
|
||||
// copy descriptions.
|
||||
if (descriptions[alias]) self.describe(key, descriptions[alias])
|
||||
// copy demanded.
|
||||
if (demanded[alias]) yargs.demand(key, demanded[alias].msg)
|
||||
if (alias in demandedOptions) yargs.demandOption(key, demandedOptions[alias])
|
||||
// type messages.
|
||||
if (~options.boolean.indexOf(alias)) yargs.boolean(key)
|
||||
if (~options.count.indexOf(alias)) yargs.count(key)
|
||||
if (~options.string.indexOf(alias)) yargs.string(key)
|
||||
if (~options.normalize.indexOf(alias)) yargs.normalize(key)
|
||||
if (~options.array.indexOf(alias)) yargs.array(key)
|
||||
if (~options.number.indexOf(alias)) yargs.number(key)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// if yargs is executing an async handler, we take a snapshot of the
|
||||
// help message to display on failure:
|
||||
let cachedHelpMessage
|
||||
self.cacheHelpMessage = function () {
|
||||
cachedHelpMessage = this.help()
|
||||
}
|
||||
|
||||
// given a set of keys, place any keys that are
|
||||
// ungrouped under the 'Options:' grouping.
|
||||
function addUngroupedKeys (keys, aliases, groups) {
|
||||
var groupedKeys = []
|
||||
var toCheck = null
|
||||
Object.keys(groups).forEach(function (group) {
|
||||
let groupedKeys = []
|
||||
let toCheck = null
|
||||
Object.keys(groups).forEach((group) => {
|
||||
groupedKeys = groupedKeys.concat(groups[group])
|
||||
})
|
||||
|
||||
keys.forEach(function (key) {
|
||||
keys.forEach((key) => {
|
||||
toCheck = [key].concat(aliases[key])
|
||||
if (!toCheck.some(function (k) {
|
||||
return groupedKeys.indexOf(k) !== -1
|
||||
})) {
|
||||
if (!toCheck.some(k => groupedKeys.indexOf(k) !== -1)) {
|
||||
groups[defaultGroup].push(key)
|
||||
}
|
||||
})
|
||||
return groupedKeys
|
||||
}
|
||||
|
||||
self.showHelp = function (level) {
|
||||
level = level || 'error'
|
||||
console[level](self.help())
|
||||
function filterHiddenOptions (key) {
|
||||
return yargs.getOptions().hiddenOptions.indexOf(key) < 0 || yargs.parsed.argv[yargs.getOptions().showHiddenOpt]
|
||||
}
|
||||
|
||||
self.functionDescription = function (fn) {
|
||||
var description = fn.name ? decamelize(fn.name, '-') : __('generated-value')
|
||||
self.showHelp = (level) => {
|
||||
const logger = yargs._getLoggerInstance()
|
||||
if (!level) level = 'error'
|
||||
const emit = typeof level === 'function' ? level : logger[level]
|
||||
emit(self.help())
|
||||
}
|
||||
|
||||
self.functionDescription = (fn) => {
|
||||
const description = fn.name ? decamelize(fn.name, '-') : __('generated-value')
|
||||
return ['(', description, ')'].join('')
|
||||
}
|
||||
|
||||
self.stringifiedValues = function (values, separator) {
|
||||
var string = ''
|
||||
var sep = separator || ', '
|
||||
var array = [].concat(values)
|
||||
self.stringifiedValues = function stringifiedValues (values, separator) {
|
||||
let string = ''
|
||||
const sep = separator || ', '
|
||||
const array = [].concat(values)
|
||||
|
||||
if (!values || !array.length) return string
|
||||
|
||||
array.forEach(function (value) {
|
||||
array.forEach((value) => {
|
||||
if (string.length) string += sep
|
||||
string += JSON.stringify(value)
|
||||
})
|
||||
@ -341,7 +463,7 @@ module.exports = function (yargs, y18n) {
|
||||
// format the default-value-string displayed in
|
||||
// the right-hand column.
|
||||
function defaultString (value, defaultDescription) {
|
||||
var string = '[' + __('default:') + ' '
|
||||
let string = `[${__('default:')} `
|
||||
|
||||
if (value === undefined && !defaultDescription) return null
|
||||
|
||||
@ -350,7 +472,7 @@ module.exports = function (yargs, y18n) {
|
||||
} else {
|
||||
switch (typeof value) {
|
||||
case 'string':
|
||||
string += JSON.stringify(value)
|
||||
string += `"${value}"`
|
||||
break
|
||||
case 'object':
|
||||
string += JSON.stringify(value)
|
||||
@ -360,23 +482,67 @@ module.exports = function (yargs, y18n) {
|
||||
}
|
||||
}
|
||||
|
||||
return string + ']'
|
||||
return `${string}]`
|
||||
}
|
||||
|
||||
// guess the width of the console window, max-width 80.
|
||||
function windowWidth () {
|
||||
return wsize.width ? Math.min(80, wsize.width) : null
|
||||
const maxWidth = 80
|
||||
if (typeof process === 'object' && process.stdout && process.stdout.columns) {
|
||||
return Math.min(maxWidth, process.stdout.columns)
|
||||
} else {
|
||||
return maxWidth
|
||||
}
|
||||
}
|
||||
|
||||
// logic for displaying application version.
|
||||
var version = null
|
||||
self.version = function (ver, opt, msg) {
|
||||
let version = null
|
||||
self.version = (ver) => {
|
||||
version = ver
|
||||
}
|
||||
|
||||
self.showVersion = function () {
|
||||
if (typeof version === 'function') console.log(version())
|
||||
else console.log(version)
|
||||
self.showVersion = () => {
|
||||
const logger = yargs._getLoggerInstance()
|
||||
logger.log(version)
|
||||
}
|
||||
|
||||
self.reset = function reset (localLookup) {
|
||||
// do not reset wrap here
|
||||
// do not reset fails here
|
||||
failMessage = null
|
||||
failureOutput = false
|
||||
usages = []
|
||||
usageDisabled = false
|
||||
epilogs = []
|
||||
examples = []
|
||||
commands = []
|
||||
descriptions = objFilter(descriptions, (k, v) => !localLookup[k])
|
||||
return self
|
||||
}
|
||||
|
||||
let frozens = []
|
||||
self.freeze = function freeze () {
|
||||
let frozen = {}
|
||||
frozens.push(frozen)
|
||||
frozen.failMessage = failMessage
|
||||
frozen.failureOutput = failureOutput
|
||||
frozen.usages = usages
|
||||
frozen.usageDisabled = usageDisabled
|
||||
frozen.epilogs = epilogs
|
||||
frozen.examples = examples
|
||||
frozen.commands = commands
|
||||
frozen.descriptions = descriptions
|
||||
}
|
||||
self.unfreeze = function unfreeze () {
|
||||
let frozen = frozens.pop()
|
||||
failMessage = frozen.failMessage
|
||||
failureOutput = frozen.failureOutput
|
||||
usages = frozen.usages
|
||||
usageDisabled = frozen.usageDisabled
|
||||
epilogs = frozen.epilogs
|
||||
examples = frozen.examples
|
||||
commands = frozen.commands
|
||||
descriptions = frozen.descriptions
|
||||
}
|
||||
|
||||
return self
|
||||
|
383
node_modules/yargs/lib/validation.js
generated
vendored
383
node_modules/yargs/lib/validation.js
generated
vendored
@ -1,85 +1,81 @@
|
||||
'use strict'
|
||||
const argsert = require('./argsert')
|
||||
const objFilter = require('./obj-filter')
|
||||
const specialKeys = ['$0', '--', '_']
|
||||
|
||||
// validation-type-stuff, missing params,
|
||||
// bad implications, custom checks.
|
||||
module.exports = function (yargs, usage, y18n) {
|
||||
var __ = y18n.__
|
||||
var __n = y18n.__n
|
||||
var self = {}
|
||||
module.exports = function validation (yargs, usage, y18n) {
|
||||
const __ = y18n.__
|
||||
const __n = y18n.__n
|
||||
const self = {}
|
||||
|
||||
// validate appropriate # of non-option
|
||||
// arguments were provided, i.e., '_'.
|
||||
self.nonOptionCount = function (argv) {
|
||||
var demanded = yargs.getDemanded()
|
||||
var _s = argv._.length
|
||||
self.nonOptionCount = function nonOptionCount (argv) {
|
||||
const demandedCommands = yargs.getDemandedCommands()
|
||||
// don't count currently executing commands
|
||||
const _s = argv._.length - yargs.getContext().commands.length
|
||||
|
||||
if (demanded._ && (_s < demanded._.count || _s > demanded._.max)) {
|
||||
if (demanded._.msg !== undefined) {
|
||||
usage.fail(demanded._.msg)
|
||||
} else if (_s < demanded._.count) {
|
||||
usage.fail(
|
||||
__('Not enough non-option arguments: got %s, need at least %s', argv._.length, demanded._.count)
|
||||
)
|
||||
} else {
|
||||
usage.fail(
|
||||
__('Too many non-option arguments: got %s, maximum of %s', argv._.length, demanded._.max)
|
||||
)
|
||||
if (demandedCommands._ && (_s < demandedCommands._.min || _s > demandedCommands._.max)) {
|
||||
if (_s < demandedCommands._.min) {
|
||||
if (demandedCommands._.minMsg !== undefined) {
|
||||
usage.fail(
|
||||
// replace $0 with observed, $1 with expected.
|
||||
demandedCommands._.minMsg ? demandedCommands._.minMsg.replace(/\$0/g, _s).replace(/\$1/, demandedCommands._.min) : null
|
||||
)
|
||||
} else {
|
||||
usage.fail(
|
||||
__('Not enough non-option arguments: got %s, need at least %s', _s, demandedCommands._.min)
|
||||
)
|
||||
}
|
||||
} else if (_s > demandedCommands._.max) {
|
||||
if (demandedCommands._.maxMsg !== undefined) {
|
||||
usage.fail(
|
||||
// replace $0 with observed, $1 with expected.
|
||||
demandedCommands._.maxMsg ? demandedCommands._.maxMsg.replace(/\$0/g, _s).replace(/\$1/, demandedCommands._.max) : null
|
||||
)
|
||||
} else {
|
||||
usage.fail(
|
||||
__('Too many non-option arguments: got %s, maximum of %s', _s, demandedCommands._.max)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// make sure that any args that require an
|
||||
// value (--foo=bar), have a value.
|
||||
self.missingArgumentValue = function (argv) {
|
||||
var defaultValues = [true, false, '']
|
||||
var options = yargs.getOptions()
|
||||
|
||||
if (options.requiresArg.length > 0) {
|
||||
var missingRequiredArgs = []
|
||||
|
||||
options.requiresArg.forEach(function (key) {
|
||||
var value = argv[key]
|
||||
|
||||
// if a value is explicitly requested,
|
||||
// flag argument as missing if it does not
|
||||
// look like foo=bar was entered.
|
||||
if (~defaultValues.indexOf(value) ||
|
||||
(Array.isArray(value) && !value.length)) {
|
||||
missingRequiredArgs.push(key)
|
||||
}
|
||||
})
|
||||
|
||||
if (missingRequiredArgs.length > 0) {
|
||||
usage.fail(__n(
|
||||
'Missing argument value: %s',
|
||||
'Missing argument values: %s',
|
||||
missingRequiredArgs.length,
|
||||
missingRequiredArgs.join(', ')
|
||||
))
|
||||
}
|
||||
// validate the appropriate # of <required>
|
||||
// positional arguments were provided:
|
||||
self.positionalCount = function positionalCount (required, observed) {
|
||||
if (observed < required) {
|
||||
usage.fail(
|
||||
__('Not enough non-option arguments: got %s, need at least %s', observed, required)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// make sure all the required arguments are present.
|
||||
self.requiredArguments = function (argv) {
|
||||
var demanded = yargs.getDemanded()
|
||||
var missing = null
|
||||
self.requiredArguments = function requiredArguments (argv) {
|
||||
const demandedOptions = yargs.getDemandedOptions()
|
||||
let missing = null
|
||||
|
||||
Object.keys(demanded).forEach(function (key) {
|
||||
if (!argv.hasOwnProperty(key)) {
|
||||
Object.keys(demandedOptions).forEach((key) => {
|
||||
if (!argv.hasOwnProperty(key) || typeof argv[key] === 'undefined') {
|
||||
missing = missing || {}
|
||||
missing[key] = demanded[key]
|
||||
missing[key] = demandedOptions[key]
|
||||
}
|
||||
})
|
||||
|
||||
if (missing) {
|
||||
var customMsgs = []
|
||||
Object.keys(missing).forEach(function (key) {
|
||||
var msg = missing[key].msg
|
||||
const customMsgs = []
|
||||
Object.keys(missing).forEach((key) => {
|
||||
const msg = missing[key]
|
||||
if (msg && customMsgs.indexOf(msg) < 0) {
|
||||
customMsgs.push(msg)
|
||||
}
|
||||
})
|
||||
|
||||
var customMsg = customMsgs.length ? '\n' + customMsgs.join('\n') : ''
|
||||
const customMsg = customMsgs.length ? `\n${customMsgs.join('\n')}` : ''
|
||||
|
||||
usage.fail(__n(
|
||||
'Missing required argument: %s',
|
||||
@ -91,27 +87,29 @@ module.exports = function (yargs, usage, y18n) {
|
||||
}
|
||||
|
||||
// check for unknown arguments (strict-mode).
|
||||
self.unknownArguments = function (argv, aliases) {
|
||||
var aliasLookup = {}
|
||||
var descriptions = usage.getDescriptions()
|
||||
var demanded = yargs.getDemanded()
|
||||
var unknown = []
|
||||
self.unknownArguments = function unknownArguments (argv, aliases, positionalMap) {
|
||||
const commandKeys = yargs.getCommandInstance().getCommands()
|
||||
const unknown = []
|
||||
const currentContext = yargs.getContext()
|
||||
|
||||
Object.keys(aliases).forEach(function (key) {
|
||||
aliases[key].forEach(function (alias) {
|
||||
aliasLookup[alias] = key
|
||||
})
|
||||
})
|
||||
|
||||
Object.keys(argv).forEach(function (key) {
|
||||
if (key !== '$0' && key !== '_' &&
|
||||
!descriptions.hasOwnProperty(key) &&
|
||||
!demanded.hasOwnProperty(key) &&
|
||||
!aliasLookup.hasOwnProperty(key)) {
|
||||
Object.keys(argv).forEach((key) => {
|
||||
if (specialKeys.indexOf(key) === -1 &&
|
||||
!positionalMap.hasOwnProperty(key) &&
|
||||
!yargs._getParseContext().hasOwnProperty(key) &&
|
||||
!self.isValidAndSomeAliasIsNotNew(key, aliases)
|
||||
) {
|
||||
unknown.push(key)
|
||||
}
|
||||
})
|
||||
|
||||
if ((currentContext.commands.length > 0) || (commandKeys.length > 0)) {
|
||||
argv._.slice(currentContext.commands.length).forEach((key) => {
|
||||
if (commandKeys.indexOf(key) === -1) {
|
||||
unknown.push(key)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (unknown.length > 0) {
|
||||
usage.fail(__n(
|
||||
'Unknown argument: %s',
|
||||
@ -122,128 +120,231 @@ module.exports = function (yargs, usage, y18n) {
|
||||
}
|
||||
}
|
||||
|
||||
// check for a key that is not an alias, or for which every alias is new,
|
||||
// implying that it was invented by the parser, e.g., during camelization
|
||||
self.isValidAndSomeAliasIsNotNew = function isValidAndSomeAliasIsNotNew (key, aliases) {
|
||||
if (!aliases.hasOwnProperty(key)) {
|
||||
return false
|
||||
}
|
||||
const newAliases = yargs.parsed.newAliases
|
||||
for (let a of [key, ...aliases[key]]) {
|
||||
if (!newAliases.hasOwnProperty(a) || !newAliases[key]) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// validate arguments limited to enumerated choices
|
||||
self.limitedChoices = function (argv) {
|
||||
var options = yargs.getOptions()
|
||||
var invalid = {}
|
||||
self.limitedChoices = function limitedChoices (argv) {
|
||||
const options = yargs.getOptions()
|
||||
const invalid = {}
|
||||
|
||||
if (!Object.keys(options.choices).length) return
|
||||
|
||||
Object.keys(argv).forEach(function (key) {
|
||||
if (key !== '$0' && key !== '_' &&
|
||||
Object.keys(argv).forEach((key) => {
|
||||
if (specialKeys.indexOf(key) === -1 &&
|
||||
options.choices.hasOwnProperty(key)) {
|
||||
[].concat(argv[key]).forEach(function (value) {
|
||||
[].concat(argv[key]).forEach((value) => {
|
||||
// TODO case-insensitive configurability
|
||||
if (options.choices[key].indexOf(value) === -1) {
|
||||
if (options.choices[key].indexOf(value) === -1 &&
|
||||
value !== undefined) {
|
||||
invalid[key] = (invalid[key] || []).concat(value)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
var invalidKeys = Object.keys(invalid)
|
||||
const invalidKeys = Object.keys(invalid)
|
||||
|
||||
if (!invalidKeys.length) return
|
||||
|
||||
var msg = __('Invalid values:')
|
||||
invalidKeys.forEach(function (key) {
|
||||
msg += '\n ' + __(
|
||||
let msg = __('Invalid values:')
|
||||
invalidKeys.forEach((key) => {
|
||||
msg += `\n ${__(
|
||||
'Argument: %s, Given: %s, Choices: %s',
|
||||
key,
|
||||
usage.stringifiedValues(invalid[key]),
|
||||
usage.stringifiedValues(options.choices[key])
|
||||
)
|
||||
)}`
|
||||
})
|
||||
usage.fail(msg)
|
||||
}
|
||||
|
||||
// custom checks, added using the `check` option on yargs.
|
||||
var checks = []
|
||||
self.check = function (f) {
|
||||
checks.push(f)
|
||||
let checks = []
|
||||
self.check = function check (f, global) {
|
||||
checks.push({
|
||||
func: f,
|
||||
global
|
||||
})
|
||||
}
|
||||
|
||||
self.customChecks = function (argv, aliases) {
|
||||
checks.forEach(function (f) {
|
||||
self.customChecks = function customChecks (argv, aliases) {
|
||||
for (let i = 0, f; (f = checks[i]) !== undefined; i++) {
|
||||
const func = f.func
|
||||
let result = null
|
||||
try {
|
||||
var result = f(argv, aliases)
|
||||
if (!result) {
|
||||
usage.fail(__('Argument check failed: %s', f.toString()))
|
||||
} else if (typeof result === 'string') {
|
||||
usage.fail(result)
|
||||
}
|
||||
result = func(argv, aliases)
|
||||
} catch (err) {
|
||||
usage.fail(err.message ? err.message : err)
|
||||
usage.fail(err.message ? err.message : err, err)
|
||||
continue
|
||||
}
|
||||
})
|
||||
|
||||
if (!result) {
|
||||
usage.fail(__('Argument check failed: %s', func.toString()))
|
||||
} else if (typeof result === 'string' || result instanceof Error) {
|
||||
usage.fail(result.toString(), result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// check implications, argument foo implies => argument bar.
|
||||
var implied = {}
|
||||
self.implies = function (key, value) {
|
||||
let implied = {}
|
||||
self.implies = function implies (key, value) {
|
||||
argsert('<string|object> [array|number|string]', [key, value], arguments.length)
|
||||
|
||||
if (typeof key === 'object') {
|
||||
Object.keys(key).forEach(function (k) {
|
||||
Object.keys(key).forEach((k) => {
|
||||
self.implies(k, key[k])
|
||||
})
|
||||
} else {
|
||||
implied[key] = value
|
||||
yargs.global(key)
|
||||
if (!implied[key]) {
|
||||
implied[key] = []
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
value.forEach((i) => self.implies(key, i))
|
||||
} else {
|
||||
implied[key].push(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
self.getImplied = function () {
|
||||
self.getImplied = function getImplied () {
|
||||
return implied
|
||||
}
|
||||
|
||||
self.implications = function (argv) {
|
||||
var implyFail = []
|
||||
function keyExists (argv, val) {
|
||||
// convert string '1' to number 1
|
||||
let num = Number(val)
|
||||
val = isNaN(num) ? val : num
|
||||
|
||||
Object.keys(implied).forEach(function (key) {
|
||||
var num
|
||||
var origKey = key
|
||||
var value = implied[key]
|
||||
if (typeof val === 'number') {
|
||||
// check length of argv._
|
||||
val = argv._.length >= val
|
||||
} else if (val.match(/^--no-.+/)) {
|
||||
// check if key/value doesn't exist
|
||||
val = val.match(/^--no-(.+)/)[1]
|
||||
val = !argv[val]
|
||||
} else {
|
||||
// check if key/value exists
|
||||
val = argv[val]
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
// convert string '1' to number 1
|
||||
num = Number(key)
|
||||
key = isNaN(num) ? key : num
|
||||
self.implications = function implications (argv) {
|
||||
const implyFail = []
|
||||
|
||||
if (typeof key === 'number') {
|
||||
// check length of argv._
|
||||
key = argv._.length >= key
|
||||
} else if (key.match(/^--no-.+/)) {
|
||||
// check if key doesn't exist
|
||||
key = key.match(/^--no-(.+)/)[1]
|
||||
key = !argv[key]
|
||||
} else {
|
||||
// check if key exists
|
||||
key = argv[key]
|
||||
}
|
||||
Object.keys(implied).forEach((key) => {
|
||||
const origKey = key
|
||||
;(implied[key] || []).forEach((value) => {
|
||||
let key = origKey
|
||||
const origValue = value
|
||||
key = keyExists(argv, key)
|
||||
value = keyExists(argv, value)
|
||||
|
||||
num = Number(value)
|
||||
value = isNaN(num) ? value : num
|
||||
|
||||
if (typeof value === 'number') {
|
||||
value = argv._.length >= value
|
||||
} else if (value.match(/^--no-.+/)) {
|
||||
value = value.match(/^--no-(.+)/)[1]
|
||||
value = !argv[value]
|
||||
} else {
|
||||
value = argv[value]
|
||||
}
|
||||
|
||||
if (key && !value) {
|
||||
implyFail.push(origKey)
|
||||
}
|
||||
if (key && !value) {
|
||||
implyFail.push(` ${origKey} -> ${origValue}`)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
if (implyFail.length) {
|
||||
var msg = __('Implications failed:') + '\n'
|
||||
let msg = `${__('Implications failed:')}\n`
|
||||
|
||||
implyFail.forEach(function (key) {
|
||||
msg += (' ' + key + ' -> ' + implied[key])
|
||||
implyFail.forEach((value) => {
|
||||
msg += (value)
|
||||
})
|
||||
|
||||
usage.fail(msg)
|
||||
}
|
||||
}
|
||||
|
||||
let conflicting = {}
|
||||
self.conflicts = function conflicts (key, value) {
|
||||
argsert('<string|object> [array|string]', [key, value], arguments.length)
|
||||
|
||||
if (typeof key === 'object') {
|
||||
Object.keys(key).forEach((k) => {
|
||||
self.conflicts(k, key[k])
|
||||
})
|
||||
} else {
|
||||
yargs.global(key)
|
||||
if (!conflicting[key]) {
|
||||
conflicting[key] = []
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
value.forEach((i) => self.conflicts(key, i))
|
||||
} else {
|
||||
conflicting[key].push(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
self.getConflicting = () => conflicting
|
||||
|
||||
self.conflicting = function conflictingFn (argv) {
|
||||
Object.keys(argv).forEach((key) => {
|
||||
if (conflicting[key]) {
|
||||
conflicting[key].forEach((value) => {
|
||||
// we default keys to 'undefined' that have been configured, we should not
|
||||
// apply conflicting check unless they are a value other than 'undefined'.
|
||||
if (value && argv[key] !== undefined && argv[value] !== undefined) {
|
||||
usage.fail(__('Arguments %s and %s are mutually exclusive', key, value))
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
self.recommendCommands = function recommendCommands (cmd, potentialCommands) {
|
||||
const distance = require('./levenshtein')
|
||||
const threshold = 3 // if it takes more than three edits, let's move on.
|
||||
potentialCommands = potentialCommands.sort((a, b) => b.length - a.length)
|
||||
|
||||
let recommended = null
|
||||
let bestDistance = Infinity
|
||||
for (let i = 0, candidate; (candidate = potentialCommands[i]) !== undefined; i++) {
|
||||
const d = distance(cmd, candidate)
|
||||
if (d <= threshold && d < bestDistance) {
|
||||
bestDistance = d
|
||||
recommended = candidate
|
||||
}
|
||||
}
|
||||
if (recommended) usage.fail(__('Did you mean %s?', recommended))
|
||||
}
|
||||
|
||||
self.reset = function reset (localLookup) {
|
||||
implied = objFilter(implied, (k, v) => !localLookup[k])
|
||||
conflicting = objFilter(conflicting, (k, v) => !localLookup[k])
|
||||
checks = checks.filter(c => c.global)
|
||||
return self
|
||||
}
|
||||
|
||||
let frozens = []
|
||||
self.freeze = function freeze () {
|
||||
let frozen = {}
|
||||
frozens.push(frozen)
|
||||
frozen.implied = implied
|
||||
frozen.checks = checks
|
||||
frozen.conflicting = conflicting
|
||||
}
|
||||
self.unfreeze = function unfreeze () {
|
||||
let frozen = frozens.pop()
|
||||
implied = frozen.implied
|
||||
checks = frozen.checks
|
||||
conflicting = frozen.conflicting
|
||||
}
|
||||
|
||||
return self
|
||||
}
|
||||
|
Reference in New Issue
Block a user