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

feat: implement conflicts() for defining mutually exclusive arguments #741

Merged
merged 5 commits into from Dec 29, 2016

Conversation

bcoe
Copy link
Member

@bcoe bcoe commented Dec 27, 2016

This pull request sees @madcampos' hard work over the finish line 🎉

I've made a few minor changes to make the conflicts() api almost identical to its cousin implies().

madcampos and others added 5 commits December 26, 2016 22:41
Addresses #534.

Basic implementation of mutually exclusive arguments option, so if the user supplies both arguments the command fails.
The new option is implemented with the signature: `.conflicts(key1, key2)`

This adds a new message `Arguments %s and %s are mutually exclusive` and it's pt and pt-br translations.
Supports passing both arguments as strings, as arrarys or only an array.
@bcoe bcoe requested review from nexdrew and lrlna December 27, 2016 07:11
@bcoe bcoe merged commit 5883779 into master Dec 29, 2016
@bcoe bcoe deleted the exclusive-arguments branch December 29, 2016 06:22
@bcoe
Copy link
Member Author

bcoe commented Dec 29, 2016

@madcampos this is slated to go out with the next release of yargs, I would love the extra help testing:

npm cache clear; npm i yargs@next

Thanks for your contribution!


Given the key `x` is set, the key `y` must not be set.

Optionally `.conflicts()` can accept an object specifying multiple conflicting keys.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we give an example of how this might work, even like so .conflicts({ 'c': 'a', 's': 't'}), or nah?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh woops! i am sorry! i didn't refresh yesterday's page to see if you merged this; ignore me :D

@madcampos
Copy link
Contributor

madcampos commented Dec 29, 2016 via email

@JeroenVdb
Copy link

@madcampos I was exited to use this new feature but it doesn't seem to work with dot-notations? Is this intentionally left out or should it be possible to implement?

var args = require('yargs')
  .option('some.foo', {
	  type: 'string'
  })
  .option('some.bar', {
	  type: 'string'
  })
  .conflicts('some.bar', 'some.foo')
  .argv;

@madcampos
Copy link
Contributor

Sorry, didn't have this case in mind...
I'll review the code and see if it's hard or not to do.

@pensive-platypus
Copy link

Is it possible to implement it in a way where one key could conflict with many keys.

.conflicts('foo', ['bar', 'baz'])
//or
.conflicts({'foo': ['bar', 'baz']})

Great work on this btw. :D

@madcampos
Copy link
Contributor

Sorry for the long due feedback…
To implement it as properties of an object the complexity would grow as the object properties would need to be mapped so conflicts knows which are the conflicting keys.

I could not fined a easy and performant object mapper/flattener so it became a problem.

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

Successfully merging this pull request may close these issues.

None yet

5 participants