Skip to content
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

Global false does not work when also set the coerce property in v17 #2162

Closed
Alive-Fish opened this issue Apr 6, 2022 · 4 comments
Closed

Comments

@Alive-Fish
Copy link

Alive-Fish commented Apr 6, 2022

I am upgrading the yargs version from 16.2.0 to 17.4.0 in order to use the async builder feature, but after upgrading, our sub commands do not work well because the global property seems being broken.

I write a piece of code for testing the global property and the true reason may be the coerce property conflicts with the global.

import yargs from "yargs";
import { hideBin } from "yargs/helpers";

yargs(hideBin(process.argv))
  .command("new", "new a project", async (yargs) => {
    return yargs
      .options("op1", {
        description: "option1",
        default: "option1",
        global: false,
        type: "string",
        coerce: (arg?: string) => arg?.toLocaleLowerCase(), // if comment this line, it works well.
      })
      .command(
        "template",
        "new a project from a template",
        async (yargs) => {
          return yargs.options("op2", {
            description: "option2",
            default: "option2",
          });
        },
        async (args: any) => {
          console.log(args);
        }
      );
  })
  .strict()
  .help().argv;

The following is the result when run this code.
image

Is this a bug or there are some different usages about the global property?

Thanks.

@Alive-Fish Alive-Fish changed the title Global false seems not working well in 17.4.0 Global false does not work when also set the coerce property in v17 Apr 6, 2022
@jly36963
Copy link
Contributor

jly36963 commented Apr 9, 2022

It's a problem with coerce. I mentioned it in a PR that fixes the upstream problem.
coerce was adding keys to argv that weren't provided by the user. This causes strict to fail (because argv is polluted with arguments that weren't provided, and the template command is unaware of the accidentally-added argument)

I tested your example locally with/without my fix, and it seems my fix solves the issue you're seeing

@jly36963 jly36963 self-assigned this Apr 9, 2022
@Alive-Fish
Copy link
Author

@jly36963 Thank you. Do you mean it will be fixed in the next yargs version? Is it the version 17.4.1?

@jly36963
Copy link
Contributor

I believe so -- the changelog has it listed under 17.4.1

@jly36963 jly36963 removed their assignment Jan 29, 2023
@shadowspawn
Copy link
Member

Closing as fixed by #2161.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants