yargs: when in singleton mode, calling parse() multiple times results in undefined behavior

So my situation is that I want to use a required positional argument with some non-required options. To my surprise the positional argument is not picked up as I expected. Here is what the command look like

$ > node index.js stress-test restock
index.js stress-test <notification>

Some description

Positionals:
  notification  notification type to stress test
                                        [string] [required] [choices: "restock"]

Options:
  --version             Show version number                            [boolean]
  --help                Show help                                      [boolean]
  --amount, -n          amount of notifications to create  [number] [default: 1]
  --environment, --env  environment to conduct stress test in
                                 [choices: "dev", "stg", "pro"] [default: "stg"]

Missing required argument: notification

If I try to give an argument that is not part of the argument choice this is what I get:

$ > node index.js stress-test test
index.js stress-test <notification>

Some description

Positionals:
  notification  notification type to stress test
                                        [string] [required] [choices: "restock"]

Options:
  --version             Show version number                            [boolean]
  --help                Show help                                      [boolean]
  --amount, -n          amount of notifications to create  [number] [default: 1]
  --environment, --env  environment to conduct stress test in
                                 [choices: "dev", "stg", "pro"] [default: "stg"]

Invalid values:
  Argument: notification, Given: "test", Choices: "restock"

So obviously the argument is properly picked up. Not sure what is going on here. This is what the command option looks like:

   {
      command: 'stress-test <notification>',
      describe: 'Some description',
      builder: (yargs) => {
        return yargs.positional('notification', {
          describe: 'notification type to stress test',
          choices: ['restock'],
          type: 'string'
        }).options({
          amount: {
            default: 1,
            requiresArg: true,
            describe: 'amount of notifications to create',
            alias: 'n',
            number: true
          },
          environment: {
            default: 'stg',
            requresArg: true,
            choices: ['dev','stg','pro'],
            describe: 'environment to conduct stress test in',
            alias: 'env'
          }
        });
      }
   }

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 17 (10 by maintainers)

Commits related to this issue

Most upvoted comments

I finally found a way not to break anything by freezing/unfreezing directly in parse, as it was already done when calling parse with arguments. I should be able to submit a PR soon.