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

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/yargs/lib')
-rw-r--r--node_modules/yargs/lib/apply-extends.js24
-rw-r--r--node_modules/yargs/lib/argsert.js8
-rw-r--r--node_modules/yargs/lib/command.js93
-rw-r--r--node_modules/yargs/lib/completion-templates.js49
-rw-r--r--node_modules/yargs/lib/completion.js29
-rw-r--r--node_modules/yargs/lib/is-promise.js3
-rw-r--r--node_modules/yargs/lib/levenshtein.js25
-rw-r--r--node_modules/yargs/lib/middleware.js64
-rw-r--r--node_modules/yargs/lib/usage.js89
-rw-r--r--node_modules/yargs/lib/validation.js79
10 files changed, 336 insertions, 127 deletions
diff --git a/node_modules/yargs/lib/apply-extends.js b/node_modules/yargs/lib/apply-extends.js
index 530b022ac..643c91335 100644
--- a/node_modules/yargs/lib/apply-extends.js
+++ b/node_modules/yargs/lib/apply-extends.js
@@ -16,12 +16,26 @@ function getPathToDefaultConfig (cwd, pathToExtend) {
return path.resolve(cwd, pathToExtend)
}
-function applyExtends (config, cwd) {
+function mergeDeep (config1, config2) {
+ const target = {}
+ const isObject = obj => obj && typeof obj === 'object' && !Array.isArray(obj)
+ Object.assign(target, config1)
+ for (let key of Object.keys(config2)) {
+ if (isObject(config2[key]) && isObject(target[key])) {
+ target[key] = mergeDeep(config1[key], config2[key])
+ } else {
+ target[key] = config2[key]
+ }
+ }
+ return target
+}
+
+function applyExtends (config, cwd, mergeExtends) {
let defaultConfig = {}
- if (config.hasOwnProperty('extends')) {
+ if (Object.prototype.hasOwnProperty.call(config, 'extends')) {
if (typeof config.extends !== 'string') return defaultConfig
- const isPath = /\.json$/.test(config.extends)
+ const isPath = /\.json|\..*rc$/.test(config.extends)
let pathToDefault = null
if (!isPath) {
try {
@@ -42,12 +56,12 @@ function applyExtends (config, cwd) {
defaultConfig = isPath ? JSON.parse(fs.readFileSync(pathToDefault, 'utf8')) : require(config.extends)
delete config.extends
- defaultConfig = applyExtends(defaultConfig, path.dirname(pathToDefault))
+ defaultConfig = applyExtends(defaultConfig, path.dirname(pathToDefault), mergeExtends)
}
previouslyVisitedConfigs = []
- return Object.assign({}, defaultConfig, config)
+ return mergeExtends ? mergeDeep(defaultConfig, config) : Object.assign({}, defaultConfig, config)
}
module.exports = applyExtends
diff --git a/node_modules/yargs/lib/argsert.js b/node_modules/yargs/lib/argsert.js
index ed1d59871..f310b4e74 100644
--- a/node_modules/yargs/lib/argsert.js
+++ b/node_modules/yargs/lib/argsert.js
@@ -1,16 +1,18 @@
'use strict'
+
+// hoisted due to circular dependency on command.
+module.exports = argsert
const command = require('./command')()
const YError = require('./yerror')
const positionName = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth']
-
-module.exports = function argsert (expected, callerArguments, length) {
+function argsert (expected, callerArguments, length) {
// TODO: should this eventually raise an exception.
try {
// preface the argument description with "cmd", so
// that we can run it through yargs' command parser.
let position = 0
- let parsed = {demanded: [], optional: []}
+ let parsed = { demanded: [], optional: [] }
if (typeof expected === 'object') {
length = callerArguments
callerArguments = expected
diff --git a/node_modules/yargs/lib/command.js b/node_modules/yargs/lib/command.js
index 65322dbbd..d2a6e4d49 100644
--- a/node_modules/yargs/lib/command.js
+++ b/node_modules/yargs/lib/command.js
@@ -1,6 +1,8 @@
'use strict'
const inspect = require('util').inspect
+const isPromise = require('./is-promise')
+const { applyMiddleware, commandMiddlewareFactory } = require('./middleware')
const path = require('path')
const Parser = require('yargs-parser')
@@ -9,15 +11,18 @@ const DEFAULT_MARKER = /(^\*)|(^\$0)/
// handles parsing positional arguments,
// and populating argv with said positional
// arguments.
-module.exports = function command (yargs, usage, validation) {
+module.exports = function command (yargs, usage, validation, globalMiddleware) {
const self = {}
let handlers = {}
let aliasMap = {}
let defaultCommand
- self.addHandler = function addHandler (cmd, description, builder, handler, middlewares) {
+ globalMiddleware = globalMiddleware || []
+
+ self.addHandler = function addHandler (cmd, description, builder, handler, commandMiddleware) {
let aliases = []
+ const middlewares = commandMiddlewareFactory(commandMiddleware)
handler = handler || (() => {})
- middlewares = middlewares || []
+
if (Array.isArray(cmd)) {
aliases = cmd.slice(1)
cmd = cmd[0]
@@ -169,7 +174,7 @@ module.exports = function command (yargs, usage, validation) {
let numFiles = currentContext.files.length
const parentCommands = currentContext.commands.slice()
- // what does yargs look like after the buidler is run?
+ // what does yargs look like after the builder is run?
let innerArgv = parsed.argv
let innerYargs = null
let positionalMap = {}
@@ -181,24 +186,17 @@ module.exports = function command (yargs, usage, validation) {
// a function can be provided, which builds
// up a yargs chain and possibly returns it.
innerYargs = commandHandler.builder(yargs.reset(parsed.aliases))
- // if the builder function did not yet parse argv with reset yargs
- // and did not explicitly set a usage() string, then apply the
- // original command string as usage() for consistent behavior with
- // options object below.
- if (yargs.parsed === false) {
- if (shouldUpdateUsage(yargs)) {
- yargs.getUsageInstance().usage(
- usageFromParentCommandsCommandHandler(parentCommands, commandHandler),
- commandHandler.description
- )
- }
- innerArgv = innerYargs ? innerYargs._parseArgs(null, null, true, commandIndex) : yargs._parseArgs(null, null, true, commandIndex)
- } else {
- innerArgv = yargs.parsed.argv
+ if (!innerYargs || (typeof innerYargs._parseArgs !== 'function')) {
+ innerYargs = yargs
}
-
- if (innerYargs && yargs.parsed === false) aliases = innerYargs.parsed.aliases
- else aliases = yargs.parsed.aliases
+ if (shouldUpdateUsage(innerYargs)) {
+ innerYargs.getUsageInstance().usage(
+ usageFromParentCommandsCommandHandler(parentCommands, commandHandler),
+ commandHandler.description
+ )
+ }
+ innerArgv = innerYargs._parseArgs(null, null, true, commandIndex)
+ aliases = innerYargs.parsed.aliases
} else if (typeof commandHandler.builder === 'object') {
// as a short hand, an object can instead be provided, specifying
// the options that a command takes.
@@ -220,24 +218,37 @@ module.exports = function command (yargs, usage, validation) {
positionalMap = populatePositionals(commandHandler, innerArgv, currentContext, yargs)
}
+ const middlewares = globalMiddleware.slice(0).concat(commandHandler.middlewares || [])
+ applyMiddleware(innerArgv, yargs, middlewares, true)
+
// we apply validation post-hoc, so that custom
// checks get passed populated positional arguments.
if (!yargs._hasOutput()) yargs._runValidation(innerArgv, aliases, positionalMap, yargs.parsed.error)
if (commandHandler.handler && !yargs._hasOutput()) {
yargs._setHasOutput()
- if (commandHandler.middlewares.length > 0) {
- const middlewareArgs = commandHandler.middlewares.reduce(function (initialObj, middleware) {
- return Object.assign(initialObj, middleware(innerArgv))
- }, {})
- Object.assign(innerArgv, middlewareArgs)
+ // to simplify the parsing of positionals in commands,
+ // we temporarily populate '--' rather than _, with arguments
+ const populateDoubleDash = !!yargs.getOptions().configuration['populate--']
+ if (!populateDoubleDash) yargs._copyDoubleDash(innerArgv)
+
+ innerArgv = applyMiddleware(innerArgv, yargs, middlewares, false)
+ let handlerResult
+ if (isPromise(innerArgv)) {
+ handlerResult = innerArgv.then(argv => commandHandler.handler(argv))
+ } else {
+ handlerResult = commandHandler.handler(innerArgv)
}
- const handlerResult = commandHandler.handler(innerArgv)
- if (handlerResult && typeof handlerResult.then === 'function') {
- handlerResult.then(
- null,
- (error) => yargs.getUsageInstance().fail(null, error)
- )
+
+ if (isPromise(handlerResult)) {
+ yargs.getUsageInstance().cacheHelpMessage()
+ handlerResult.catch(error => {
+ try {
+ yargs.getUsageInstance().fail(null, error)
+ } catch (err) {
+ // fail's throwing would cause an unhandled rejection.
+ }
+ })
}
}
@@ -328,6 +339,7 @@ module.exports = function command (yargs, usage, validation) {
options.default = Object.assign(parseOptions.default, options.default)
options.alias = Object.assign(parseOptions.alias, options.alias)
options.array = options.array.concat(parseOptions.array)
+ delete options.config // don't load config when processing positionals.
const unparsed = []
Object.keys(positionalMap).forEach((key) => {
@@ -340,7 +352,12 @@ module.exports = function command (yargs, usage, validation) {
// short-circuit parse.
if (!unparsed.length) return
- const parsed = Parser.detailed(unparsed, options)
+ const config = Object.assign({}, options.configuration, {
+ 'populate--': true
+ })
+ const parsed = Parser.detailed(unparsed, Object.assign({}, options, {
+ configuration: config
+ }))
if (parsed.error) {
yargs.getUsageInstance().fail(parsed.error.message, parsed.error)
@@ -354,6 +371,9 @@ module.exports = function command (yargs, usage, validation) {
Object.keys(parsed.argv).forEach((key) => {
if (positionalKeys.indexOf(key) !== -1) {
+ // any new aliases need to be placed in positionalMap, which
+ // is used for validation.
+ if (!positionalMap[key]) positionalMap[key] = parsed.argv[key]
argv[key] = parsed.argv[key]
}
})
@@ -408,18 +428,19 @@ module.exports = function command (yargs, usage, validation) {
// the state of commands such that
// we can apply .parse() multiple times
// with the same yargs instance.
- let frozen
+ let frozens = []
self.freeze = () => {
- frozen = {}
+ let frozen = {}
+ frozens.push(frozen)
frozen.handlers = handlers
frozen.aliasMap = aliasMap
frozen.defaultCommand = defaultCommand
}
self.unfreeze = () => {
+ let frozen = frozens.pop()
handlers = frozen.handlers
aliasMap = frozen.aliasMap
defaultCommand = frozen.defaultCommand
- frozen = undefined
}
return self
diff --git a/node_modules/yargs/lib/completion-templates.js b/node_modules/yargs/lib/completion-templates.js
new file mode 100644
index 000000000..43714fb6e
--- /dev/null
+++ b/node_modules/yargs/lib/completion-templates.js
@@ -0,0 +1,49 @@
+exports.completionShTemplate =
+`###-begin-{{app_name}}-completions-###
+#
+# yargs command completion script
+#
+# Installation: {{app_path}} {{completion_command}} >> ~/.bashrc
+# or {{app_path}} {{completion_command}} >> ~/.bash_profile on OSX.
+#
+_yargs_completions()
+{
+ local cur_word args type_list
+
+ cur_word="\${COMP_WORDS[COMP_CWORD]}"
+ args=("\${COMP_WORDS[@]}")
+
+ # ask yargs to generate completions.
+ type_list=$({{app_path}} --get-yargs-completions "\${args[@]}")
+
+ COMPREPLY=( $(compgen -W "\${type_list}" -- \${cur_word}) )
+
+ # if no match was found, fall back to filename completion
+ if [ \${#COMPREPLY[@]} -eq 0 ]; then
+ COMPREPLY=()
+ fi
+
+ return 0
+}
+complete -o default -F _yargs_completions {{app_name}}
+###-end-{{app_name}}-completions-###
+`
+
+exports.completionZshTemplate = `###-begin-{{app_name}}-completions-###
+#
+# yargs command completion script
+#
+# Installation: {{app_path}} {{completion_command}} >> ~/.zshrc
+# or {{app_path}} {{completion_command}} >> ~/.zsh_profile on OSX.
+#
+_{{app_name}}_yargs_completions()
+{
+ local reply
+ local si=$IFS
+ IFS=$'\n' reply=($(COMP_CWORD="$((CURRENT-1))" COMP_LINE="$BUFFER" COMP_POINT="$CURSOR" {{app_path}} --get-yargs-completions "\${words[@]}"))
+ IFS=$si
+ _describe 'values' reply
+}
+compdef _{{app_name}}_yargs_completions {{app_name}}
+###-end-{{app_name}}-completions-###
+`
diff --git a/node_modules/yargs/lib/completion.js b/node_modules/yargs/lib/completion.js
index ad6969a2d..3f3bf16e1 100644
--- a/node_modules/yargs/lib/completion.js
+++ b/node_modules/yargs/lib/completion.js
@@ -1,5 +1,4 @@
'use strict'
-const fs = require('fs')
const path = require('path')
// add bash completions to your
@@ -9,6 +8,8 @@ module.exports = function completion (yargs, usage, command) {
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.
// 'args' is the array of strings from the line to be completed
self.getCompletion = function getCompletion (args, done) {
@@ -16,6 +17,7 @@ module.exports = function completion (yargs, usage, command) {
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().
@@ -54,22 +56,33 @@ module.exports = function completion (yargs, usage, command) {
}
}
- if (!current.match(/^-/)) {
+ if (!current.match(/^-/) && parentCommands[parentCommands.length - 1] !== current) {
usage.getCommands().forEach((usageCommand) => {
const commandName = command.parseCommand(usageCommand[0]).cmd
if (args.indexOf(commandName) === -1) {
- completions.push(commandName)
+ if (!zshShell) {
+ completions.push(commandName)
+ } else {
+ const desc = usageCommand[1] || ''
+ completions.push(commandName.replace(/:/g, '\\:') + ':' + desc)
+ }
}
})
}
- if (current.match(/^-/)) {
+ 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) {
- completions.push(`--${key}`)
+ if (!zshShell) {
+ completions.push(`--${key}`)
+ } else {
+ const desc = descs[key] || ''
+ completions.push(`--${key.replace(/:/g, '\\:')}:${desc.replace('__yargsString__:', '')}`)
+ }
}
})
}
@@ -79,10 +92,8 @@ module.exports = function completion (yargs, usage, command) {
// generate the completion script to add to your .bashrc.
self.generateCompletionScript = function generateCompletionScript ($0, cmd) {
- let script = fs.readFileSync(
- path.resolve(__dirname, '../completion.sh.hbs'),
- 'utf-8'
- )
+ 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.
diff --git a/node_modules/yargs/lib/is-promise.js b/node_modules/yargs/lib/is-promise.js
new file mode 100644
index 000000000..271d93b2d
--- /dev/null
+++ b/node_modules/yargs/lib/is-promise.js
@@ -0,0 +1,3 @@
+module.exports = function isPromise (maybePromise) {
+ return !!maybePromise && !!maybePromise.then && (typeof maybePromise.then === 'function')
+}
diff --git a/node_modules/yargs/lib/levenshtein.js b/node_modules/yargs/lib/levenshtein.js
index f32b0c277..c66c1babb 100644
--- a/node_modules/yargs/lib/levenshtein.js
+++ b/node_modules/yargs/lib/levenshtein.js
@@ -1,11 +1,22 @@
/*
Copyright (c) 2011 Andrei Mackenzie
-Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// levenshtein distance algorithm, pulled from Andrei Mackenzie's MIT licensed.
@@ -37,8 +48,8 @@ module.exports = function levenshtein (a, b) {
matrix[i][j] = matrix[i - 1][j - 1]
} else {
matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1, // substitution
- Math.min(matrix[i][j - 1] + 1, // insertion
- matrix[i - 1][j] + 1)) // deletion
+ Math.min(matrix[i][j - 1] + 1, // insertion
+ matrix[i - 1][j] + 1)) // deletion
}
}
}
diff --git a/node_modules/yargs/lib/middleware.js b/node_modules/yargs/lib/middleware.js
new file mode 100644
index 000000000..56dab7527
--- /dev/null
+++ b/node_modules/yargs/lib/middleware.js
@@ -0,0 +1,64 @@
+'use strict'
+
+// hoisted due to circular dependency on command.
+module.exports = {
+ applyMiddleware,
+ commandMiddlewareFactory,
+ globalMiddlewareFactory
+}
+const isPromise = require('./is-promise')
+const argsert = require('./argsert')
+
+function globalMiddlewareFactory (globalMiddleware, context) {
+ return function (callback, applyBeforeValidation = false) {
+ argsert('<array|function> [boolean]', [callback, applyBeforeValidation], arguments.length)
+ if (Array.isArray(callback)) {
+ for (let i = 0; i < callback.length; i++) {
+ if (typeof callback[i] !== 'function') {
+ throw Error('middleware must be a function')
+ }
+ callback[i].applyBeforeValidation = applyBeforeValidation
+ }
+ Array.prototype.push.apply(globalMiddleware, callback)
+ } else if (typeof callback === 'function') {
+ callback.applyBeforeValidation = applyBeforeValidation
+ globalMiddleware.push(callback)
+ }
+ return context
+ }
+}
+
+function commandMiddlewareFactory (commandMiddleware) {
+ if (!commandMiddleware) return []
+ return commandMiddleware.map(middleware => {
+ middleware.applyBeforeValidation = false
+ return middleware
+ })
+}
+
+function applyMiddleware (argv, yargs, middlewares, beforeValidation) {
+ const beforeValidationError = new Error('middleware cannot return a promise when applyBeforeValidation is true')
+ return middlewares
+ .reduce((accumulation, middleware) => {
+ if (middleware.applyBeforeValidation !== beforeValidation) {
+ return accumulation
+ }
+
+ if (isPromise(accumulation)) {
+ return accumulation
+ .then(initialObj =>
+ Promise.all([initialObj, middleware(initialObj, yargs)])
+ )
+ .then(([initialObj, middlewareObj]) =>
+ Object.assign(initialObj, middlewareObj)
+ )
+ } else {
+ const result = middleware(argv, yargs)
+ if (beforeValidation && isPromise(result)) throw beforeValidationError
+
+ return isPromise(result)
+ ? result.then(middlewareObj => Object.assign(accumulation, middlewareObj))
+ : Object.assign(accumulation, result)
+ }
+ }, argv)
+}
diff --git a/node_modules/yargs/lib/usage.js b/node_modules/yargs/lib/usage.js
index bd0906a89..92bf37862 100644
--- a/node_modules/yargs/lib/usage.js
+++ b/node_modules/yargs/lib/usage.js
@@ -1,6 +1,7 @@
'use strict'
// this file handles outputting usage instructions,
// failures, etc. keeps logging in one place.
+const decamelize = require('decamelize')
const stringWidth = require('string-width')
const objFilter = require('./obj-filter')
const path = require('path')
@@ -45,7 +46,10 @@ module.exports = function usage (yargs, y18n) {
// don't output failure message more than once
if (!failureOutput) {
failureOutput = true
- if (showHelpOnFail) yargs.showHelp('error')
+ if (showHelpOnFail) {
+ yargs.showHelp('error')
+ logger.error()
+ }
if (msg || err) logger.error(msg || err)
if (failMessage) {
if (msg || err) logger.error('')
@@ -118,9 +122,9 @@ module.exports = function usage (yargs, y18n) {
}
self.getDescriptions = () => descriptions
- let epilog
+ let epilogs = []
self.epilog = (msg) => {
- epilog = msg
+ epilogs.push(msg)
}
let wrapSet = false
@@ -144,24 +148,26 @@ module.exports = function usage (yargs, y18n) {
const defaultGroup = 'Options:'
self.help = function help () {
+ if (cachedHelpMessage) return cachedHelpMessage
normalizeAliases()
// handle old demanded API
- const base$0 = path.basename(yargs.$0)
+ 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 = Object.keys(
- Object.keys(descriptions)
- .concat(Object.keys(demandedOptions))
- .concat(Object.keys(demandedCommands))
- .concat(Object.keys(options.default))
- .reduce((acc, key) => {
- if (key !== '_') acc[key] = true
- return acc
- }, {})
- )
+
+ 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')({
@@ -176,7 +182,7 @@ module.exports = function usage (yargs, y18n) {
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({ text: `${usage[1]}`, padding: [1, 0, 0, 0] })
}
})
ui.div()
@@ -200,6 +206,10 @@ module.exports = function usage (yargs, y18n) {
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(
@@ -208,7 +218,7 @@ module.exports = function usage (yargs, y18n) {
padding: [0, 2, 0, 2],
width: maxWidth(commands, theWrap, `${base$0}${parentCommands}`) + 4
},
- {text: command[1]}
+ { text: command[1] }
)
const hints = []
if (command[2]) hints.push(`[${__('default:').slice(0, -1)}]`) // TODO hacking around i18n here
@@ -216,7 +226,7 @@ module.exports = function usage (yargs, y18n) {
hints.push(`[${__('aliases:')} ${command[3].join(', ')}]`)
}
if (hints.length) {
- ui.div({text: hints.join(' '), padding: [0, 0, 0, 2], align: 'right'})
+ ui.div({ text: hints.join(' '), padding: [0, 0, 0, 2], align: 'right' })
} else {
ui.div()
}
@@ -241,11 +251,9 @@ module.exports = function usage (yargs, y18n) {
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'.
- const normalizedKeys = groups[groupName].map((key) => {
+ const normalizedKeys = groups[groupName].filter(filterHiddenOptions).map((key) => {
if (~aliasKeys.indexOf(key)) return key
for (let i = 0, aliasKey; (aliasKey = aliasKeys[i]) !== undefined; i++) {
if (~(options.alias[aliasKey] || []).indexOf(key)) return aliasKey
@@ -253,6 +261,10 @@ module.exports = function usage (yargs, y18n) {
return key
})
+ if (normalizedKeys.length < 1) return
+
+ ui.div(__(groupName))
+
// actually generate the switches string --foo, -f, --bar.
const switches = normalizedKeys.reduce((acc, key) => {
acc[key] = [ key ].concat(options.alias[key] || [])
@@ -290,11 +302,11 @@ module.exports = function usage (yargs, y18n) {
].filter(Boolean).join(' ')
ui.span(
- {text: kswitch, padding: [0, 2, 0, 2], width: maxWidth(switches, theWrap) + 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()
})
@@ -334,12 +346,13 @@ module.exports = function usage (yargs, y18n) {
}
// the usage string.
- if (epilog) {
- const e = epilog.replace(/\$0/g, base$0)
+ 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
@@ -391,6 +404,13 @@ module.exports = function usage (yargs, y18n) {
})
}
+ // 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) {
@@ -409,6 +429,10 @@ module.exports = function usage (yargs, y18n) {
return groupedKeys
}
+ function filterHiddenOptions (key) {
+ return yargs.getOptions().hiddenOptions.indexOf(key) < 0 || yargs.parsed.argv[yargs.getOptions().showHiddenOpt]
+ }
+
self.showHelp = (level) => {
const logger = yargs._getLoggerInstance()
if (!level) level = 'error'
@@ -417,7 +441,7 @@ module.exports = function usage (yargs, y18n) {
}
self.functionDescription = (fn) => {
- const description = fn.name ? require('decamelize')(fn.name, '-') : __('generated-value')
+ const description = fn.name ? decamelize(fn.name, '-') : __('generated-value')
return ['(', description, ')'].join('')
}
@@ -489,35 +513,36 @@ module.exports = function usage (yargs, y18n) {
failureOutput = false
usages = []
usageDisabled = false
- epilog = undefined
+ epilogs = []
examples = []
commands = []
descriptions = objFilter(descriptions, (k, v) => !localLookup[k])
return self
}
- let frozen
+ let frozens = []
self.freeze = function freeze () {
- frozen = {}
+ let frozen = {}
+ frozens.push(frozen)
frozen.failMessage = failMessage
frozen.failureOutput = failureOutput
frozen.usages = usages
frozen.usageDisabled = usageDisabled
- frozen.epilog = epilog
+ 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
- epilog = frozen.epilog
+ epilogs = frozen.epilogs
examples = frozen.examples
commands = frozen.commands
descriptions = frozen.descriptions
- frozen = undefined
}
return self
diff --git a/node_modules/yargs/lib/validation.js b/node_modules/yargs/lib/validation.js
index f4655b4fd..35659a356 100644
--- a/node_modules/yargs/lib/validation.js
+++ b/node_modules/yargs/lib/validation.js
@@ -37,7 +37,7 @@ module.exports = function validation (yargs, usage, y18n) {
)
} else {
usage.fail(
- __('Too many non-option arguments: got %s, maximum of %s', _s, demandedCommands._.max)
+ __('Too many non-option arguments: got %s, maximum of %s', _s, demandedCommands._.max)
)
}
}
@@ -96,13 +96,13 @@ module.exports = function validation (yargs, usage, y18n) {
if (specialKeys.indexOf(key) === -1 &&
!positionalMap.hasOwnProperty(key) &&
!yargs._getParseContext().hasOwnProperty(key) &&
- !aliases.hasOwnProperty(key)
+ !self.isValidAndSomeAliasIsNotNew(key, aliases)
) {
unknown.push(key)
}
})
- if (commandKeys.length > 0) {
+ if ((currentContext.commands.length > 0) || (commandKeys.length > 0)) {
argv._.slice(currentContext.commands.length).forEach((key) => {
if (commandKeys.indexOf(key) === -1) {
unknown.push(key)
@@ -120,6 +120,21 @@ module.exports = function validation (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 limitedChoices (argv) {
const options = yargs.getOptions()
@@ -209,43 +224,36 @@ module.exports = function validation (yargs, usage, y18n) {
return implied
}
+ function keyExists (argv, val) {
+ // convert string '1' to number 1
+ let num = Number(val)
+ val = isNaN(num) ? val : num
+
+ 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
+ }
+
self.implications = function implications (argv) {
const implyFail = []
Object.keys(implied).forEach((key) => {
const origKey = key
;(implied[key] || []).forEach((value) => {
- let num
let key = origKey
const origValue = value
+ key = keyExists(argv, key)
+ value = keyExists(argv, value)
- // convert string '1' to number 1
- num = Number(key)
- key = isNaN(num) ? key : num
-
- 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]
- }
-
- 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} -> ${origValue}`)
}
@@ -292,7 +300,7 @@ module.exports = function validation (yargs, usage, y18n) {
// 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 ${key} and ${value} are mutually exclusive`))
+ usage.fail(__('Arguments %s and %s are mutually exclusive', key, value))
}
})
}
@@ -323,18 +331,19 @@ module.exports = function validation (yargs, usage, y18n) {
return self
}
- let frozen
+ let frozens = []
self.freeze = function freeze () {
- frozen = {}
+ 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
- frozen = undefined
}
return self