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

implies behaves incorrectly on boolean options #2372

Closed
getchardy opened this issue Nov 2, 2023 · 4 comments
Closed

implies behaves incorrectly on boolean options #2372

getchardy opened this issue Nov 2, 2023 · 4 comments

Comments

@getchardy
Copy link

getchardy commented Nov 2, 2023

As of 17.1.0, the behavior of implies on a boolean option is incorrect.

// test.js

const yargs = require('yargs')

const argv = yargs
  .options({
    boolFlag: {
      type: 'boolean',
      default: false,
      implies: ['otherArg'],
    },
    otherArg: {
      type: 'string',
    },
  })
  .help()
  .parseSync()

console.log(argv)

On 17.1.0 running the script without the boolFlag gives the following:

> node test.js
test

test command

Options:
  --version   Show version number                                      [boolean]
  --help      Show help                                                [boolean]
  --boolFlag                                          [boolean] [default: false]
  --otherArg                                                            [string]

Missing dependent arguments:
 boolFlag -> otherArg

On 17.0.0 running the script without the boolFlag gives the following which is what I believe is correct:

>  node test.js 
{ _: [], boolFlag: false, 'bool-flag': false, '$0': 'test.js' }
{ _: [], boolFlag: false, 'bool-flag': false, '$0': 'test.js' }

I think this is the PR where the defect was introduced: #1985

@shadowspawn
Copy link
Member

Thanks for the clear example code and usage.

The 17.1.0 behaviour is as intended. Default values are not treated specially here and since they set the option key, the implied option must also be set. The PR makes the behaviour consistent based on whether the option is set rather than depending on the option value.

Given the key x is set, it is required that the key y is set.

https://yargs.js.org/docs/#api-reference-impliesx-y

@getchardy
Copy link
Author

Thanks for the info and good explanation about the default.

I think I need to adjust my mindset as to how this applies to boolean values, can you please confirm if I'm on the right wavelength now?

Before: "setting a flag X to true can make some additional arguments mandatory"
Now: "X and its related options must all be set, or not set at all"

Is there anything else in the API that supports conditionally mandatory arguments? Otherwise I'll just manually validate.

@shadowspawn
Copy link
Member

Now: "X and its related options must all be set, or not set at all"

To be clear, you can set the implied option (otherArg in your example) without needing to set X (boolFlag in your example).

Is there anything else in the API that supports conditionally mandatory arguments?

No. You may get most of what you want if you simply omit the default value for boolFlag.

However, --no-boolFlag will trigger the implied mandatory arguments too. To get conditional mandatory arguments based on whether the option is true rather than just being set, manually validate.

@shadowspawn
Copy link
Member

An answer was provided, and no further activity in a month. Closing this as resolved.

Feel free to open a new issue if it comes up again, with new information and renewed interest.

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

2 participants