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

Coerce is called multiple times #1802

Closed
lewisdiamond opened this issue Nov 11, 2020 · 2 comments
Closed

Coerce is called multiple times #1802

lewisdiamond opened this issue Nov 11, 2020 · 2 comments
Labels

Comments

@lewisdiamond
Copy link

Coerce should always be run once and only once.

const yargs = require("yargs/yargs");
const { hideBin } = require("yargs/helpers");
const cmd1 = {
    command: "cmd1",
    desc: "cmd1",
    builder: (yargs) => {
        yargs.options({
            opt1: {
                type: "string",
                coerce: (opt1) => {
                    console.log("opt1", opt1);
                    return opt1;
                },
            },
        });
    },
    handler: (argv) => {
        console.log(argv);
    },
};
const argv = yargs(hideBin(process.argv))
    .options({
        opt: {
            coerce: (opt) => {
                console.log("opt", opt);
                return opt;
            },
        },
    })
    .command(cmd1)
    .help().argv;
$ node index.js --opt test cmd1 --opt1 test1
opt test
opt test
opt1 test1
{ _: [ 'cmd1' ], opt: 'test', opt1: 'test1', '$0': 'index.js' }
@jly36963
Copy link
Contributor

jly36963 commented Jul 19, 2021

Hey! I believe I fixed this bug with this PR. Ben has already published this fix to yargs@next (details in my PR). Can you test it out?

I ran the following code locally (with and without the fix) and it worked correctly. No recursive calling of coerce middleware.

yargs
  .options({
    opt: {
      coerce: (opt) => {
        console.log("opt coerce middleware called");
        console.log(`opt: ${opt}`);
        return opt;
      },
    },
  })
  .command({
    command: "cmd1",
    desc: "cmd1 desc",
    builder: (yargs) =>
      yargs
        .options({
          opt1: {
            type: "string",
            coerce: (opt1) => {
              console.log("opt1 coerce middleware called");
              console.log(`opt1: ${opt1}`);
              return opt1;
            },
          },
        })
        .command({
          command: "cmd2",
          desc: "cmd2 desc",
          builder: (yargs) =>
            yargs.options({
              opt2: {
                type: "string",
                coerce: (opt2) => {
                  console.log("opt2 coerce middleware called");
                  console.log(`opt2: ${opt2}`);
                  return opt2;
                },
              },
            }),
          handler: (argv) => {
            console.log(argv);
          },
        }),
    handler: (argv) => {
      console.log(argv);
    },
  })
  .help()
  .parse("cmd1 cmd2 --opt test --opt1 test1 --opt2 test2");

logs with fix:

opt coerce middleware called
opt: test
opt1 coerce middleware called
opt1: test1
opt2 coerce middleware called
opt2: test2
{
  _: [ 'cmd1', 'cmd2' ],
  opt: 'test',
  opt1: 'test1',
  opt2: 'test2',
  '$0': '2021-07-19-coerce-nested-commands-2.js'
}

logs without fix:

opt coerce middleware called
opt: test
opt1 coerce middleware called
opt1: test1
opt2 coerce middleware called
opt2: test2
{
  _: [ 'cmd1', 'cmd2' ],
  opt: 'test',
  opt1: 'test1',
  opt2: 'test2',
  '$0': '2021-07-19-coerce-nested-commands-2.js'
}
opt coerce middleware called
opt: test
opt1 coerce middleware called
opt1: test1
opt2 coerce middleware called
opt2: test2

@shadowspawn
Copy link
Member

Fixed by #1978 (thanks @jly36963)

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

No branches or pull requests

4 participants