Skip to content

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

@Alive-Fish

Description

@Alive-Fish

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions