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
Configuration parser called twice #659
Comments
Hi. I made a pull request with my proposed solution. |
This is a problem concerning the relationship between argv parsing (via yargs-parser) and "lazy" command execution (via yargs), as described below:
Since command configuration is lazy/dynamic in this approach, it is necessary to do argv parsing more than once (N+1 times, in fact, where N represents the number of nested subcommands given on the command line). And since the config file loading/parsing is handled by the argv parsing logic in yargs-parser (which has no concept of commands), it is executed each time argv parsing occurs (assuming the config option is valid for that command). When the config option is marked as global, it becomes valid for every command and every level of parsing/execution. The short-term workaround is to not use a global config option, but rather define it as an option only at the "innermost" commands. For the example above, it would look more like this: function load (path) {
console.warn("warning: %s not found", path)
return { hello: 'world' }
}
require('yargs')
.command('command', 'Description', yargs => yargs.config('config', load), argv => {
// by this time, load has been called and its result has been applied to argv
console.log(argv.config) // path given on command line
console.log(argv.hello) // value from load function
})
.argv I'm not quite sure what the best long-term solution is, but I think we need to add some concept of commands down to the yargs-parser level, which would allow us to parse positional args there (giving them 1st-class citizen status) as well as defer/cache config file loading/parsing as necessary. Perhaps we also need to take a harder look at changing the command API to better support a fully declarative DAG instead of a lazily-built one. |
I think the whole approach of global-vs-local options is a bad idea. It is very annoying that I have to mark every single option as global, and work around issues that exist just to support some unnecessary abstraction nobody needs. When someone starts to call a bug a feature, it is a good sign it is time to leave. So, I am thinking of using some other library instead. |
@codedot in |
FWIW, the lazy invocation of the Just thought I'd mention the use case given @bcoe's comment about investigating a fully declarative command API vs. the current lazy invocation approach. |
@codedot @davewasmer we've just released a beta of yargs 7.x, which now defaults most options to being applied globally. Would love your help testing the next major release, before we promote it to a wider audience:
|
This bug is still reproduced for |
The text was updated successfully, but these errors were encountered: