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

Migrating an option value from boolean to choices requires breaking changes #412

Open
eemeli opened this issue Sep 12, 2021 · 0 comments
Open

Comments

@eemeli
Copy link

eemeli commented Sep 12, 2021

Related: yargs/yargs#1599

Boolean options have this special property:

If a non-flag option - unless true or false - follows key in process.argv, that string won’t get set as the value of key.

This means that if a CLI defines an option as a boolean, it isn't currently possible to later change that option to support more choices than true or false without creating a breaking change for the CLI's interface.

In other words, this happens currently:

require('yargs-parser')('--foo bar')
> { _: [], foo: 'bar' }

require('yargs-parser')('--foo bar', { boolean: ['foo'] })
> { _: [ 'bar' ], foo: true }

If the choices option were handled already in the parser, it'd be possible to (optionally?) allow similar behaviour for it:

require('yargs-parser')('--foo bar', {
  choices: ['true', 'false', 'maybe'], // not a current parser option
  default: { foo: 'maybe' }
})
> { _: [ 'bar' ], foo: 'maybe' }

A similar result could also be achieved if the coerce function could indicate that the given value should not be consumed for the current key, but parsed independently:

require('yargs-parser')('--foo bar', {
  coerce: {
    foo(value) {
      if (['true', 'false', 'maybe'].includes(value) return value
      this.doNotEat() // not currently defined; could also be an argument
      return 'maybe'
    }
  }
})
> { _: [ 'bar' ], foo: 'maybe' }
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

1 participant