New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: rethink how options are inherited by commands #766
Changes from all commits
aca9d64
5218040
1a7e703
695e15f
0dd09ef
db25fb8
0c6a1ec
8de98a8
3d7759a
bae1d2d
4cff044
5e6a042
dadd300
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,47 +131,62 @@ module.exports = function (yargs, usage, validation) { | |
} | ||
|
||
self.runCommand = function (command, yargs, parsed) { | ||
var argv = parsed.argv | ||
var aliases = parsed.aliases | ||
var commandHandler = handlers[command] || handlers[aliasMap[command]] | ||
var innerArgv = argv | ||
var currentContext = yargs.getContext() | ||
var numFiles = currentContext.files.length | ||
var parentCommands = currentContext.commands.slice() | ||
|
||
// what does yargs look like after the buidler is run? | ||
var innerArgv = parsed.argv | ||
var innerYargs = null | ||
|
||
currentContext.commands.push(command) | ||
if (typeof commandHandler.builder === 'function') { | ||
// a function can be provided, which builds | ||
// up a yargs chain and possibly returns it. | ||
innerArgv = commandHandler.builder(yargs.reset(parsed.aliases)) | ||
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 | ||
// options object below. | ||
if (yargs.parsed === false) { | ||
if (typeof yargs.getUsageInstance().getUsage() === 'undefined') { | ||
yargs.usage('$0 ' + (parentCommands.length ? parentCommands.join(' ') + ' ' : '') + commandHandler.original) | ||
} | ||
innerArgv = innerArgv ? innerArgv.argv : yargs.argv | ||
innerArgv = innerYargs ? innerYargs._parseArgs(null, null, true) : yargs._parseArgs(null, null, true) | ||
} else { | ||
innerArgv = yargs.parsed.argv | ||
} | ||
|
||
if (innerYargs && yargs.parsed === false) aliases = innerYargs.parsed.aliases | ||
else aliases = yargs.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. | ||
innerArgv = yargs.reset(parsed.aliases) | ||
innerArgv.usage('$0 ' + (parentCommands.length ? parentCommands.join(' ') + ' ' : '') + commandHandler.original) | ||
innerYargs = yargs.reset(parsed.aliases) | ||
innerYargs.usage('$0 ' + (parentCommands.length ? parentCommands.join(' ') + ' ' : '') + commandHandler.original) | ||
Object.keys(commandHandler.builder).forEach(function (key) { | ||
innerArgv.option(key, commandHandler.builder[key]) | ||
innerYargs.option(key, commandHandler.builder[key]) | ||
}) | ||
innerArgv = innerArgv.argv | ||
innerArgv = innerYargs._parseArgs(null, null, true) | ||
aliases = innerYargs.parsed.aliases | ||
} | ||
|
||
if (!yargs._hasOutput()) populatePositionals(commandHandler, innerArgv, currentContext, yargs) | ||
|
||
if (commandHandler.handler && !yargs._hasOutput()) { | ||
commandHandler.handler(innerArgv) | ||
} | ||
|
||
// we apply validation post-hoc, so that custom | ||
// checks get passed populated positional arguments. | ||
yargs._runValidation(innerArgv, aliases) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the little bit of refactoring in this method was to hold off on applying validation until after we've populated variadic values -- this was largely so that one can provide a |
||
|
||
currentContext.commands.pop() | ||
numFiles = currentContext.files.length - numFiles | ||
if (numFiles > 0) currentContext.files.splice(numFiles * -1, numFiles) | ||
|
||
return innerArgv | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very awesome, I love this one