-
Notifications
You must be signed in to change notification settings - Fork 991
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
coerce works... and then unworks #2430
Comments
A quick answer. Despite appearances In particular, you want to store the results of the parse and not parse twice. Then you should "obviously" get the same results twice! e.g.
|
Ah thanks for the quick reply. The trouble is, I'm exporting a builder function and a handler function and argv is not the same in each. export const builder = (yargs) => {
const argv = yargs.argv; // correct here
};
export const handler = async (argv) => {
console.log(argv); // wrong here
} I can hack it like this: let argv;
export const builder = (yargs) => {
argv = yargs.argv; // correct here
};
export const handler = async () => {
console.log(argv); // correct here
} but this kinda defeats the purpose of the coercing upstream |
Basically, it does not make sense to call parse (or The relevant part of the documentation is:
https://yargs.js.org/docs/#api-reference-commandcmd-desc-builder-handler |
I can't really as we dynamically set defaults in the builder function based on a config file we lookup based on user input. eg. export const builder = (yargs) => {
const argv = yargs.argv;
const defaults = getDefaults(argv.config);
yargs
.positional("name", {
describe: "Name matching a package or import map on the Eik server",
type: "string",
default: defaults.name,
})
.... If you have any suggestions about how a more yargs idiomatic way of doing this is that would be great but otherwise It's ok, I can just not use the coerce function. And thanks for your help :) |
I wondered whether your simpler examples were leading to a more complex scenario. 😄 What you are currently doing is parsing the inputs in the builder (by calling A couple of ideas:
I have experimented with the double-pass approach with a different parsing library, but not tried it with yargs myself. If that doesn't make sense I can try and knock up an example but that will be some days away. |
Ill look into the configuration file thing you mention, thanks so much for your help! |
Would it make sense to actively throw when |
I have some code that looks like this:
and I use it with something like:
And when I log yarns.argv 2x like this:
The first is correct and the second has reverted to the value before coercion.
Any idea what I'm doing wrong?
The text was updated successfully, but these errors were encountered: