Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
self._parseArgs = function parseArgs (args, shortCircuit, _skipValidation, commandIndex) {
let skipValidation = !!_skipValidation
args = args || processArgs
options.__ = y18n.__
options.configuration = self.getParserConfiguration()
// Deprecated
let pkgConfig = pkgUp()['yargs']
if (pkgConfig) {
console.warn('Configuring yargs through package.json is deprecated and will be removed in the next major release, please use the JS API instead.')
options.configuration = Object.assign({}, pkgConfig, options.configuration)
}
const parsed = Parser.detailed(args, options)
let argv = parsed.argv
if (parseContext) argv = Object.assign({}, argv, parseContext)
const 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
}
function parseArgs (args, shortCircuit) {
options.__ = y18n.__
options.configuration = pkgConf.sync('yargs', {
defaults: {},
cwd: requireMainFilename(require)
})
const parsed = Parser.detailed(args, options)
const argv = parsed.argv
var aliases = parsed.aliases
argv.$0 = self.$0
self.parsed = parsed
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 there's a handler associated with a
function parseArgs (args, shortCircuit) {
options.__ = y18n.__
options.configuration = pkgUp(cwd)['yargs'] || {}
const parsed = Parser.detailed(args, options)
const argv = parsed.argv
var aliases = parsed.aliases
argv.$0 = self.$0
self.parsed = parsed
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 there's a handler associated with a
const unparsed = []
Object.keys(positionalMap).forEach((key) => {
positionalMap[key].map((value) => {
unparsed.push(`--${key}`)
unparsed.push(value)
})
})
// short-circuit parse.
if (!unparsed.length) return
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)
} else {
// only copy over positional keys (don't overwrite
// flag arguments that were already parsed).
const positionalKeys = Object.keys(positionalMap)
Object.keys(positionalMap).forEach((key) => {
[].push.apply(positionalKeys, parsed.aliases[key])
})
Object.keys(parsed.argv).forEach((key) => {
if (positionalKeys.indexOf(key) !== -1) {
// any new aliases need to be placed in positionalMap, which
options.default = Object.assign(parseOptions.default, options.default)
options.alias = Object.assign(parseOptions.alias, options.alias)
options.array = options.array.concat(parseOptions.array)
const unparsed = []
Object.keys(positionalMap).forEach((key) => {
positionalMap[key].map((value) => {
unparsed.push(`--${key}`)
unparsed.push(value)
})
})
// short-circuit parse.
if (!unparsed.length) return
const parsed = Parser.detailed(unparsed, options)
if (parsed.error) {
yargs.getUsageInstance().fail(parsed.error.message, parsed.error)
} else {
// only copy over positional keys (don't overwrite
// flag arguments that were already parsed).
const positionalKeys = Object.keys(positionalMap)
Object.keys(positionalMap).forEach((key) => {
[].push.apply(positionalKeys, parsed.aliases[key])
})
Object.keys(parsed.argv).forEach((key) => {
if (positionalKeys.indexOf(key) !== -1) {
argv[key] = parsed.argv[key]
}
})
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
}
self._parseArgs = function (args, shortCircuit, _skipValidation, commandIndex) {
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
}
options.default = Object.assign(parseOptions.default, options.default)
options.alias = Object.assign(parseOptions.alias, options.alias)
options.array = options.array.concat(parseOptions.array)
const unparsed = []
Object.keys(positionalMap).forEach((key) => {
positionalMap[key].map((value) => {
unparsed.push(`--${key}`)
unparsed.push(value)
})
})
// short-circuit parse.
if (!unparsed.length) return
const parsed = Parser.detailed(unparsed, options)
if (parsed.error) {
yargs.getUsageInstance().fail(parsed.error.message, parsed.error)
} else {
// only copy over positional keys (don't overwrite
// flag arguments that were already parsed).
const positionalKeys = Object.keys(positionalMap)
Object.keys(positionalMap).forEach((key) => {
[].push.apply(positionalKeys, parsed.aliases[key])
})
Object.keys(parsed.argv).forEach((key) => {
if (positionalKeys.indexOf(key) !== -1) {
argv[key] = parsed.argv[key]
}
})
export function parse(
argv: string[],
cmd: Pick, "options" | "args">,
config: Partial = {}
): ParseResult {
const detail = parser.detailed(argv, {
configuration: config,
...getParserOptions(cmd.options || {})
});
const { options, errors: optErrors } = validateOptions(
detail,
cmd.options || {}
);
const { args, errors: argErrors } = validateArgs(detail, cmd.args || []);
return {
detail,
options,
args,
errors: [...optErrors, ...argErrors]
};