12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127 |
- const argsert = require('./lib/argsert')
- const assign = require('./lib/assign')
- const Command = require('./lib/command')
- const Completion = require('./lib/completion')
- const Parser = require('yargs-parser')
- const path = require('path')
- const Usage = require('./lib/usage')
- const Validation = require('./lib/validation')
- const Y18n = require('y18n')
- const objFilter = require('./lib/obj-filter')
- const setBlocking = require('set-blocking')
- const applyExtends = require('./lib/apply-extends')
- const YError = require('./lib/yerror')
-
- var exports = module.exports = Yargs
- function Yargs (processArgs, cwd, parentRequire) {
- processArgs = processArgs || [] // handle calling yargs().
-
- const self = {}
- var command = null
- var completion = null
- var groups = {}
- var output = ''
- var preservedGroups = {}
- var usage = null
- var validation = null
-
- const y18n = Y18n({
- directory: path.resolve(__dirname, './locales'),
- updateFiles: false
- })
-
- if (!cwd) cwd = process.cwd()
-
- self.$0 = process.argv
- .slice(0, 2)
- .map(function (x, i) {
- // ignore the node bin, specify this in your
- // bin file with #!/usr/bin/env node
- if (i === 0 && /\b(node|iojs)(\.exe)?$/.test(x)) return
- var b = rebase(cwd, x)
- return x.match(/^(\/|([a-zA-Z]:)?\\)/) && b.length < x.length ? b : x
- })
- .join(' ').trim()
-
- if (process.env._ !== undefined && process.argv[1] === process.env._) {
- self.$0 = process.env._.replace(
- path.dirname(process.execPath) + '/', ''
- )
- }
-
- // use context object to keep track of resets, subcommand execution, etc
- // submodules should modify and check the state of context as necessary
- const context = { resets: -1, commands: [], files: [] }
- self.getContext = function () {
- return context
- }
-
- // puts yargs back into an initial state. any keys
- // that have been set to "global" will not be reset
- // by this action.
- var options
- self.resetOptions = self.reset = function (aliases) {
- context.resets++
- aliases = aliases || {}
- options = options || {}
- // put yargs back into an initial state, this
- // logic is used to build a nested command
- // hierarchy.
- var tmpOptions = {}
- tmpOptions.local = options.local ? options.local : []
- tmpOptions.configObjects = options.configObjects ? options.configObjects : []
-
- // if a key has been explicitly set as local,
- // we should reset it before passing options to command.
- var localLookup = {}
- tmpOptions.local.forEach(function (l) {
- localLookup[l] = true
- ;(aliases[l] || []).forEach(function (a) {
- localLookup[a] = true
- })
- })
-
- // preserve all groups not set to local.
- preservedGroups = Object.keys(groups).reduce(function (acc, groupName) {
- var keys = groups[groupName].filter(function (key) {
- return !(key in localLookup)
- })
- if (keys.length > 0) {
- acc[groupName] = keys
- }
- return acc
- }, {})
- // groups can now be reset
- groups = {}
-
- var arrayOptions = [
- 'array', 'boolean', 'string', 'requiresArg', 'skipValidation',
- 'count', 'normalize', 'number'
- ]
-
- var objectOptions = [
- 'narg', 'key', 'alias', 'default', 'defaultDescription',
- 'config', 'choices', 'demandedOptions', 'demandedCommands', 'coerce'
- ]
-
- arrayOptions.forEach(function (k) {
- tmpOptions[k] = (options[k] || []).filter(function (k) {
- return !localLookup[k]
- })
- })
-
- objectOptions.forEach(function (k) {
- tmpOptions[k] = objFilter(options[k], function (k, v) {
- return !localLookup[k]
- })
- })
-
- tmpOptions.envPrefix = options.envPrefix
- options = tmpOptions
-
- // if this is the first time being executed, create
- // instances of all our helpers -- otherwise just reset.
- usage = usage ? usage.reset(localLookup) : Usage(self, y18n)
- validation = validation ? validation.reset(localLookup) : Validation(self, usage, y18n)
- command = command ? command.reset() : Command(self, usage, validation)
- if (!completion) completion = Completion(self, usage, command)
-
- if (!strictGlobal) strict = false
- completionCommand = null
- output = ''
- exitError = null
- hasOutput = false
- self.parsed = false
-
- return self
- }
- self.resetOptions()
-
- // temporary hack: allow "freezing" of reset-able state for parse(msg, cb)
- var frozen
- function freeze () {
- frozen = {}
- frozen.options = options
- frozen.configObjects = options.configObjects.slice(0)
- frozen.exitProcess = exitProcess
- frozen.groups = groups
- usage.freeze()
- validation.freeze()
- command.freeze()
- frozen.strict = strict
- frozen.completionCommand = completionCommand
- frozen.output = output
- frozen.exitError = exitError
- frozen.hasOutput = hasOutput
- frozen.parsed = self.parsed
- }
- function unfreeze () {
- options = frozen.options
- options.configObjects = frozen.configObjects
- exitProcess = frozen.exitProcess
- groups = frozen.groups
- output = frozen.output
- exitError = frozen.exitError
- hasOutput = frozen.hasOutput
- self.parsed = frozen.parsed
- usage.unfreeze()
- validation.unfreeze()
- command.unfreeze()
- strict = frozen.strict
- completionCommand = frozen.completionCommand
- parseFn = null
- parseContext = null
- frozen = undefined
- }
-
- self.boolean = function (keys) {
- argsert('<array|string>', [keys], arguments.length)
- populateParserHintArray('boolean', keys)
- return self
- }
-
- self.array = function (keys) {
- argsert('<array|string>', [keys], arguments.length)
- populateParserHintArray('array', keys)
- return self
- }
-
- self.number = function (keys) {
- argsert('<array|string>', [keys], arguments.length)
- populateParserHintArray('number', keys)
- return self
- }
-
- self.normalize = function (keys) {
- argsert('<array|string>', [keys], arguments.length)
- populateParserHintArray('normalize', keys)
- return self
- }
-
- self.count = function (keys) {
- argsert('<array|string>', [keys], arguments.length)
- populateParserHintArray('count', keys)
- return self
- }
-
- self.string = function (keys) {
- argsert('<array|string>', [keys], arguments.length)
- populateParserHintArray('string', keys)
- return self
- }
-
- self.requiresArg = function (keys) {
- argsert('<array|string>', [keys], arguments.length)
- populateParserHintArray('requiresArg', keys)
- return self
- }
-
- self.skipValidation = function (keys) {
- argsert('<array|string>', [keys], arguments.length)
- populateParserHintArray('skipValidation', keys)
- return self
- }
-
- function populateParserHintArray (type, keys, value) {
- keys = [].concat(keys)
- keys.forEach(function (key) {
- options[type].push(key)
- })
- }
-
- self.nargs = function (key, value) {
- argsert('<string|object|array> [number]', [key, value], arguments.length)
- populateParserHintObject(self.nargs, false, 'narg', key, value)
- return self
- }
-
- self.choices = function (key, value) {
- argsert('<object|string|array> [string|array]', [key, value], arguments.length)
- populateParserHintObject(self.choices, true, 'choices', key, value)
- return self
- }
-
- self.alias = function (key, value) {
- argsert('<object|string|array> [string|array]', [key, value], arguments.length)
- populateParserHintObject(self.alias, true, 'alias', key, value)
- return self
- }
-
- // TODO: actually deprecate self.defaults.
- self.default = self.defaults = function (key, value, defaultDescription) {
- argsert('<object|string|array> [*] [string]', [key, value, defaultDescription], arguments.length)
- if (defaultDescription) options.defaultDescription[key] = defaultDescription
- if (typeof value === 'function') {
- if (!options.defaultDescription[key]) options.defaultDescription[key] = usage.functionDescription(value)
- value = value.call()
- }
- populateParserHintObject(self.default, false, 'default', key, value)
- return self
- }
-
- self.describe = function (key, desc) {
- argsert('<object|string|array> [string]', [key, desc], arguments.length)
- populateParserHintObject(self.describe, false, 'key', key, true)
- usage.describe(key, desc)
- return self
- }
-
- self.demandOption = function (keys, msg) {
- argsert('<object|string|array> [string]', [keys, msg], arguments.length)
- populateParserHintObject(self.demandOption, false, 'demandedOptions', keys, msg)
- return self
- }
-
- self.coerce = function (keys, value) {
- argsert('<object|string|array> [function]', [keys, value], arguments.length)
- populateParserHintObject(self.coerce, false, 'coerce', keys, value)
- return self
- }
-
- function populateParserHintObject (builder, isArray, type, key, value) {
- if (Array.isArray(key)) {
- // an array of keys with one value ['x', 'y', 'z'], function parse () {}
- var temp = {}
- key.forEach(function (k) {
- temp[k] = value
- })
- builder(temp)
- } else if (typeof key === 'object') {
- // an object of key value pairs: {'x': parse () {}, 'y': parse() {}}
- Object.keys(key).forEach(function (k) {
- builder(k, key[k])
- })
- } else {
- // a single key value pair 'x', parse() {}
- if (isArray) {
- options[type][key] = (options[type][key] || []).concat(value)
- } else {
- options[type][key] = value
- }
- }
- }
-
- self.config = function (key, msg, parseFn) {
- argsert('[object|string] [string|function] [function]', [key, msg, parseFn], arguments.length)
- // allow a config object to be provided directly.
- if (typeof key === 'object') {
- key = applyExtends(key, cwd)
- options.configObjects = (options.configObjects || []).concat(key)
- return self
- }
-
- // allow for a custom parsing function.
- if (typeof msg === 'function') {
- parseFn = msg
- msg = null
- }
-
- key = key || 'config'
- self.describe(key, msg || usage.deferY18nLookup('Path to JSON config file'))
- ;(Array.isArray(key) ? key : [key]).forEach(function (k) {
- options.config[k] = parseFn || true
- })
-
- return self
- }
-
- self.example = function (cmd, description) {
- argsert('<string> [string]', [cmd, description], arguments.length)
- usage.example(cmd, description)
- return self
- }
-
- self.command = function (cmd, description, builder, handler) {
- argsert('<string|array|object> [string|boolean] [function|object] [function]', [cmd, description, builder, handler], arguments.length)
- command.addHandler(cmd, description, builder, handler)
- return self
- }
-
- self.commandDir = function (dir, opts) {
- argsert('<string> [object]', [dir, opts], arguments.length)
- const req = parentRequire || require
- command.addDirectory(dir, self.getContext(), req, require('get-caller-file')(), opts)
- return self
- }
-
- // TODO: deprecate self.demand in favor of
- // .demandCommand() .demandOption().
- self.demand = self.required = self.require = function (keys, max, msg) {
- // you can optionally provide a 'max' key,
- // which will raise an exception if too many '_'
- // options are provided.
- if (Array.isArray(max)) {
- max.forEach(function (key) {
- self.demandOption(key, msg)
- })
- max = Infinity
- } else if (typeof max !== 'number') {
- msg = max
- max = Infinity
- }
-
- if (typeof keys === 'number') {
- self.demandCommand(keys, max, msg, msg)
- } else if (Array.isArray(keys)) {
- keys.forEach(function (key) {
- self.demandOption(key, msg)
- })
- } else {
- if (typeof msg === 'string') {
- self.demandOption(keys, msg)
- } else if (msg === true || typeof msg === 'undefined') {
- self.demandOption(keys)
- }
- }
-
- return self
- }
-
- self.demandCommand = function (min, max, minMsg, maxMsg) {
- argsert('[number] [number|string] [string|null] [string|null]', [min, max, minMsg, maxMsg], arguments.length)
-
- if (typeof min === 'undefined') min = 1
-
- if (typeof max !== 'number') {
- minMsg = max
- max = Infinity
- }
-
- self.global('_', false)
-
- options.demandedCommands._ = {
- min: min,
- max: max,
- minMsg: minMsg,
- maxMsg: maxMsg
- }
-
- return self
- }
-
- self.getDemandedOptions = function () {
- argsert([], 0)
- return options.demandedOptions
- }
-
- self.getDemandedCommands = function () {
- argsert([], 0)
- return options.demandedCommands
- }
-
- self.implies = function (key, value) {
- argsert('<string|object> [string]', [key, value], arguments.length)
- validation.implies(key, value)
- return self
- }
-
- self.conflicts = function (key1, key2) {
- argsert('<string|object> [string]', [key1, key2], arguments.length)
- validation.conflicts(key1, key2)
- return self
- }
-
- self.usage = function (msg, opts) {
- argsert('<string|null|object> [object]', [msg, opts], arguments.length)
-
- if (!opts && typeof msg === 'object') {
- opts = msg
- msg = null
- }
-
- usage.usage(msg)
-
- if (opts) self.options(opts)
-
- return self
- }
-
- self.epilogue = self.epilog = function (msg) {
- argsert('<string>', [msg], arguments.length)
- usage.epilog(msg)
- return self
- }
-
- self.fail = function (f) {
- argsert('<function>', [f], arguments.length)
- usage.failFn(f)
- return self
- }
-
- self.check = function (f, _global) {
- argsert('<function> [boolean]', [f, _global], arguments.length)
- validation.check(f, _global !== false)
- return self
- }
-
- self.global = function (globals, global) {
- argsert('<string|array> [boolean]', [globals, global], arguments.length)
- globals = [].concat(globals)
- if (global !== false) {
- options.local = options.local.filter(function (l) {
- return globals.indexOf(l) === -1
- })
- } else {
- globals.forEach(function (g) {
- if (options.local.indexOf(g) === -1) options.local.push(g)
- })
- }
- return self
- }
-
- self.pkgConf = function (key, path) {
- argsert('<string> [string]', [key, path], arguments.length)
- var conf = null
- // prefer cwd to require-main-filename in this method
- // since we're looking for e.g. "nyc" config in nyc consumer
- // rather than "yargs" config in nyc (where nyc is the main filename)
- var obj = pkgUp(path || cwd)
-
- // If an object exists in the key, add it to options.configObjects
- if (obj[key] && typeof obj[key] === 'object') {
- conf = applyExtends(obj[key], path || cwd, key)
- options.configObjects = (options.configObjects || []).concat(conf)
- }
-
- return self
- }
-
- var pkgs = {}
- function pkgUp (path) {
- var npath = path || '*'
- if (pkgs[npath]) return pkgs[npath]
- const readPkgUp = require('read-pkg-up')
-
- var obj = {}
- try {
- obj = readPkgUp.sync({
- cwd: path || require('require-main-filename')(parentRequire || require),
- normalize: false
- })
- } catch (noop) {}
-
- pkgs[npath] = obj.pkg || {}
- return pkgs[npath]
- }
-
- var parseFn = null
- var parseContext = null
- self.parse = function (args, shortCircuit, _parseFn) {
- argsert('<string|array> [function|boolean|object] [function]', [args, shortCircuit, _parseFn], arguments.length)
-
- // a context object can optionally be provided, this allows
- // additional information to be passed to a command handler.
- if (typeof shortCircuit === 'object') {
- parseContext = shortCircuit
- shortCircuit = _parseFn
- }
-
- // by providing a function as a second argument to
- // parse you can capture output that would otherwise
- // default to printing to stdout/stderr.
- if (typeof shortCircuit === 'function') {
- parseFn = shortCircuit
- shortCircuit = null
- }
- // completion short-circuits the parsing process,
- // skipping validation, etc.
- if (!shortCircuit) processArgs = args
-
- freeze()
- if (parseFn) exitProcess = false
-
- var parsed = self._parseArgs(args, shortCircuit)
- if (parseFn) parseFn(exitError, parsed, output)
- unfreeze()
-
- return parsed
- }
-
- self._getParseContext = function () {
- return parseContext || {}
- }
-
- self._hasParseCallback = function () {
- return !!parseFn
- }
-
- self.option = self.options = function (key, opt) {
- argsert('<string|object> [object]', [key, opt], arguments.length)
- if (typeof key === 'object') {
- Object.keys(key).forEach(function (k) {
- self.options(k, key[k])
- })
- } else {
- if (typeof opt !== 'object') {
- opt = {}
- }
-
- options.key[key] = true // track manually set keys.
-
- if (opt.alias) self.alias(key, opt.alias)
-
- var demand = opt.demand || opt.required || opt.require
-
- // deprecated, use 'demandOption' instead
- if (demand) {
- self.demand(key, demand)
- }
-
- if (opt.demandOption) {
- self.demandOption(key, typeof opt.demandOption === 'string' ? opt.demandOption : undefined)
- }
-
- if ('config' in opt) {
- self.config(key, opt.configParser)
- }
-
- if ('conflicts' in opt) {
- self.conflicts(key, opt.conflicts)
- }
-
- if ('default' in opt) {
- self.default(key, opt.default)
- }
-
- if ('implies' in opt) {
- self.implies(key, opt.implies)
- }
-
- if ('nargs' in opt) {
- self.nargs(key, opt.nargs)
- }
-
- if ('normalize' in opt) {
- self.normalize(key)
- }
-
- if ('choices' in opt) {
- self.choices(key, opt.choices)
- }
-
- if ('coerce' in opt) {
- self.coerce(key, opt.coerce)
- }
-
- if ('group' in opt) {
- self.group(key, opt.group)
- }
-
- if (opt.boolean || opt.type === 'boolean') {
- self.boolean(key)
- if (opt.alias) self.boolean(opt.alias)
- }
-
- if (opt.array || opt.type === 'array') {
- self.array(key)
- if (opt.alias) self.array(opt.alias)
- }
-
- if (opt.number || opt.type === 'number') {
- self.number(key)
- if (opt.alias) self.number(opt.alias)
- }
-
- if (opt.string || opt.type === 'string') {
- self.string(key)
- if (opt.alias) self.string(opt.alias)
- }
-
- if (opt.count || opt.type === 'count') {
- self.count(key)
- }
-
- if (typeof opt.global === 'boolean') {
- self.global(key, opt.global)
- }
-
- if (opt.defaultDescription) {
- options.defaultDescription[key] = opt.defaultDescription
- }
-
- if (opt.skipValidation) {
- self.skipValidation(key)
- }
-
- var desc = opt.describe || opt.description || opt.desc
- if (desc) {
- self.describe(key, desc)
- }
-
- if (opt.requiresArg) {
- self.requiresArg(key)
- }
- }
-
- return self
- }
- self.getOptions = function () {
- return options
- }
-
- self.group = function (opts, groupName) {
- argsert('<string|array> <string>', [opts, groupName], arguments.length)
- var existing = preservedGroups[groupName] || groups[groupName]
- if (preservedGroups[groupName]) {
- // we now only need to track this group name in groups.
- delete preservedGroups[groupName]
- }
-
- var seen = {}
- groups[groupName] = (existing || []).concat(opts).filter(function (key) {
- if (seen[key]) return false
- return (seen[key] = true)
- })
- return self
- }
- self.getGroups = function () {
- // combine explicit and preserved groups. explicit groups should be first
- return assign(groups, preservedGroups)
- }
-
- // as long as options.envPrefix is not undefined,
- // parser will apply env vars matching prefix to argv
- self.env = function (prefix) {
- argsert('[string|boolean]', [prefix], arguments.length)
- if (prefix === false) options.envPrefix = undefined
- else options.envPrefix = prefix || ''
- return self
- }
-
- self.wrap = function (cols) {
- argsert('<number|null>', [cols], arguments.length)
- usage.wrap(cols)
- return self
- }
-
- var strict = false
- var strictGlobal = false
- self.strict = function (global) {
- argsert('[boolean]', [global], arguments.length)
- strict = true
- strictGlobal = global !== false
- return self
- }
- self.getStrict = function () {
- return strict
- }
-
- self.showHelp = function (level) {
- argsert('[string|function]', [level], arguments.length)
- if (!self.parsed) self._parseArgs(processArgs) // run parser, if it has not already been executed.
- usage.showHelp(level)
- return self
- }
-
- var versionOpt = null
- self.version = function (opt, msg, ver) {
- argsert('[string|function] [string|function] [string]', [opt, msg, ver], arguments.length)
- if (arguments.length === 0) {
- ver = guessVersion()
- opt = 'version'
- } else if (arguments.length === 1) {
- ver = opt
- opt = 'version'
- } else if (arguments.length === 2) {
- ver = msg
- msg = null
- }
-
- versionOpt = opt
- msg = msg || usage.deferY18nLookup('Show version number')
-
- usage.version(ver || undefined)
- self.boolean(versionOpt)
- self.describe(versionOpt, msg)
- return self
- }
-
- function guessVersion () {
- var obj = pkgUp()
-
- return obj.version || 'unknown'
- }
-
- var helpOpt = null
- var useHelpOptAsCommand = false // a call to .help() will enable this
- self.addHelpOpt = self.help = function (opt, msg, addImplicitCmd) {
- argsert('[string|boolean] [string|boolean] [boolean]', [opt, msg, addImplicitCmd], arguments.length)
-
- // argument shuffle
- if (arguments.length === 0) {
- useHelpOptAsCommand = true
- } else if (arguments.length === 1) {
- if (typeof opt === 'boolean') {
- useHelpOptAsCommand = opt
- opt = null
- } else {
- useHelpOptAsCommand = true
- }
- } else if (arguments.length === 2) {
- if (typeof msg === 'boolean') {
- useHelpOptAsCommand = msg
- msg = null
- } else {
- useHelpOptAsCommand = true
- }
- } else {
- useHelpOptAsCommand = Boolean(addImplicitCmd)
- }
- // use arguments, fallback to defaults for opt and msg
- helpOpt = opt || 'help'
- self.boolean(helpOpt)
- self.describe(helpOpt, msg || usage.deferY18nLookup('Show help'))
- return self
- }
-
- self.showHelpOnFail = function (enabled, message) {
- argsert('[boolean|string] [string]', [enabled, message], arguments.length)
- usage.showHelpOnFail(enabled, message)
- return self
- }
-
- var exitProcess = true
- self.exitProcess = function (enabled) {
- argsert('[boolean]', [enabled], arguments.length)
- if (typeof enabled !== 'boolean') {
- enabled = true
- }
- exitProcess = enabled
- return self
- }
- self.getExitProcess = function () {
- return exitProcess
- }
-
- var completionCommand = null
- self.completion = function (cmd, desc, fn) {
- argsert('[string] [string|boolean|function] [function]', [cmd, desc, fn], arguments.length)
-
- // a function to execute when generating
- // completions can be provided as the second
- // or third argument to completion.
- if (typeof desc === 'function') {
- fn = desc
- desc = null
- }
-
- // register the completion command.
- completionCommand = cmd || 'completion'
- if (!desc && desc !== false) {
- desc = 'generate bash completion script'
- }
- self.command(completionCommand, desc)
-
- // a function can be provided
- if (fn) completion.registerFunction(fn)
-
- return self
- }
-
- self.showCompletionScript = function ($0) {
- argsert('[string]', [$0], arguments.length)
- $0 = $0 || self.$0
- _logger.log(completion.generateCompletionScript($0))
- return self
- }
-
- self.getCompletion = function (args, done) {
- argsert('<array> <function>', [args, done], arguments.length)
- completion.getCompletion(args, done)
- }
-
- self.locale = function (locale) {
- argsert('[string]', [locale], arguments.length)
- if (arguments.length === 0) {
- guessLocale()
- return y18n.getLocale()
- }
- detectLocale = false
- y18n.setLocale(locale)
- return self
- }
-
- self.updateStrings = self.updateLocale = function (obj) {
- argsert('<object>', [obj], arguments.length)
- detectLocale = false
- y18n.updateLocale(obj)
- return self
- }
-
- var detectLocale = true
- self.detectLocale = function (detect) {
- argsert('<boolean>', [detect], arguments.length)
- detectLocale = detect
- return self
- }
- self.getDetectLocale = function () {
- return detectLocale
- }
-
- var hasOutput = false
- var exitError = null
- // maybe exit, always capture
- // context about why we wanted to exit.
- self.exit = function (code, err) {
- hasOutput = true
- exitError = err
- if (exitProcess) process.exit(code)
- }
-
- // we use a custom logger that buffers output,
- // so that we can print to non-CLIs, e.g., chat-bots.
- var _logger = {
- log: function () {
- const args = []
- for (var i = 0; i < arguments.length; i++) args.push(arguments[i])
- if (!self._hasParseCallback()) console.log.apply(console, args)
- hasOutput = true
- if (output.length) output += '\n'
- output += args.join(' ')
- },
- error: function () {
- const args = []
- for (var i = 0; i < arguments.length; i++) args.push(arguments[i])
- if (!self._hasParseCallback()) console.error.apply(console, args)
- hasOutput = true
- if (output.length) output += '\n'
- output += args.join(' ')
- }
- }
- self._getLoggerInstance = function () {
- return _logger
- }
- // has yargs output an error our help
- // message in the current execution context.
- self._hasOutput = function () {
- return hasOutput
- }
-
- self._setHasOutput = function () {
- hasOutput = true
- }
-
- var recommendCommands
- self.recommendCommands = function (recommend) {
- argsert('[boolean]', [recommend], arguments.length)
- recommendCommands = typeof recommend === 'boolean' ? recommend : true
- return self
- }
-
- self.getUsageInstance = function () {
- return usage
- }
-
- self.getValidationInstance = function () {
- return validation
- }
-
- self.getCommandInstance = function () {
- return command
- }
-
- self.terminalWidth = function () {
- argsert([], 0)
- return typeof process.stdout.columns !== 'undefined' ? process.stdout.columns : null
- }
-
- Object.defineProperty(self, 'argv', {
- get: function () {
- return self._parseArgs(processArgs)
- },
- enumerable: true
- })
-
- self._parseArgs = function (args, shortCircuit, _skipValidation) {
- var skipValidation = !!_skipValidation
- args = args || processArgs
-
- options.__ = y18n.__
- options.configuration = pkgUp()['yargs'] || {}
- const parsed = Parser.detailed(args, options)
- var argv = parsed.argv
- if (parseContext) argv = assign(argv, parseContext)
- var aliases = parsed.aliases
-
- argv.$0 = self.$0
- self.parsed = parsed
-
- try {
- guessLocale() // guess locale lazily, so that it can be turned off in chain.
-
- // while building up the argv object, there
- // are two passes through the parser. If completion
- // is being performed short-circuit on the first pass.
- if (shortCircuit) {
- return argv
- }
-
- if (argv._.length) {
- // check for helpOpt in argv._ before running commands
- // assumes helpOpt must be valid if useHelpOptAsCommand is true
- if (useHelpOptAsCommand) {
- // consider any multi-char helpOpt alias as a valid help command
- // unless all helpOpt aliases are single-char
- // note that parsed.aliases is a normalized bidirectional map :)
- var helpCmds = [helpOpt].concat(aliases[helpOpt] || [])
- var multiCharHelpCmds = helpCmds.filter(function (k) {
- return k.length > 1
- })
- if (multiCharHelpCmds.length) helpCmds = multiCharHelpCmds
- // look for and strip any helpCmds from argv._
- argv._ = argv._.filter(function (cmd) {
- if (~helpCmds.indexOf(cmd)) {
- argv[helpOpt] = true
- return false
- }
- return true
- })
- }
-
- // if there's a handler associated with a
- // command defer processing to it.
- var handlerKeys = command.getCommands()
- if (handlerKeys.length) {
- var firstUnknownCommand
- for (var i = 0, cmd; argv._[i] !== undefined; i++) {
- cmd = String(argv._[i])
- if (~handlerKeys.indexOf(cmd) && cmd !== completionCommand) {
- setPlaceholderKeys(argv)
- return command.runCommand(cmd, self, parsed)
- } else if (!firstUnknownCommand && cmd !== completionCommand) {
- firstUnknownCommand = cmd
- }
- }
-
- // run the default command, if defined
- if (command.hasDefaultCommand() && !argv[helpOpt]) {
- setPlaceholderKeys(argv)
- return command.runCommand(null, self, parsed)
- }
-
- // recommend a command if recommendCommands() has
- // been enabled, and no commands were found to execute
- if (recommendCommands && firstUnknownCommand) {
- validation.recommendCommands(firstUnknownCommand, handlerKeys)
- }
- }
-
- // generate a completion script for adding to ~/.bashrc.
- if (completionCommand && ~argv._.indexOf(completionCommand) && !argv[completion.completionKey]) {
- if (exitProcess) setBlocking(true)
- self.showCompletionScript()
- self.exit(0)
- }
- } else if (command.hasDefaultCommand() && !argv[helpOpt]) {
- setPlaceholderKeys(argv)
- return command.runCommand(null, self, parsed)
- }
-
- // we must run completions first, a user might
- // want to complete the --help or --version option.
- if (completion.completionKey in argv) {
- if (exitProcess) setBlocking(true)
-
- // we allow for asynchronous completions,
- // e.g., loading in a list of commands from an API.
- var completionArgs = args.slice(args.indexOf('--' + completion.completionKey) + 1)
- completion.getCompletion(completionArgs, function (completions) {
- ;(completions || []).forEach(function (completion) {
- _logger.log(completion)
- })
-
- self.exit(0)
- })
- return setPlaceholderKeys(argv)
- }
-
- // Handle 'help' and 'version' options
- // if we haven't already output help!
- if (!hasOutput) {
- Object.keys(argv).forEach(function (key) {
- if (key === helpOpt && argv[key]) {
- if (exitProcess) setBlocking(true)
-
- skipValidation = true
- self.showHelp('log')
- self.exit(0)
- } else if (key === versionOpt && argv[key]) {
- if (exitProcess) setBlocking(true)
-
- skipValidation = true
- usage.showVersion()
- self.exit(0)
- }
- })
- }
-
- // Check if any of the options to skip validation were provided
- if (!skipValidation && options.skipValidation.length > 0) {
- skipValidation = Object.keys(argv).some(function (key) {
- return options.skipValidation.indexOf(key) >= 0 && argv[key] === true
- })
- }
-
- // If the help or version options where used and exitProcess is false,
- // or if explicitly skipped, we won't run validations.
- if (!skipValidation) {
- if (parsed.error) throw new YError(parsed.error.message)
-
- // if we're executed via bash completion, don't
- // bother with validation.
- if (!argv[completion.completionKey]) {
- self._runValidation(argv, aliases, {}, parsed.error)
- }
- }
- } catch (err) {
- if (err instanceof YError) usage.fail(err.message, err)
- else throw err
- }
-
- return setPlaceholderKeys(argv)
- }
-
- self._runValidation = function (argv, aliases, positionalMap, parseErrors) {
- if (parseErrors) throw new YError(parseErrors.message)
- validation.nonOptionCount(argv)
- validation.missingArgumentValue(argv)
- validation.requiredArguments(argv)
- if (strict) validation.unknownArguments(argv, aliases, positionalMap)
- validation.customChecks(argv, aliases)
- validation.limitedChoices(argv)
- validation.implications(argv)
- validation.conflicting(argv)
- }
-
- function guessLocale () {
- if (!detectLocale) return
-
- try {
- const osLocale = require('os-locale')
- self.locale(osLocale.sync({ spawn: false }))
- } catch (err) {
- // if we explode looking up locale just noop
- // we'll keep using the default language 'en'.
- }
- }
-
- function setPlaceholderKeys (argv) {
- Object.keys(options.key).forEach(function (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
- }
-
- return self
- }
-
- // rebase an absolute path to a relative one with respect to a base directory
- // exported for tests
- exports.rebase = rebase
- function rebase (base, dir) {
- return path.relative(base, dir)
- }
|