Skip to content

Commit

Permalink
feat: add conflicts and implies shorthands. (#753)
Browse files Browse the repository at this point in the history
  • Loading branch information
vseventer authored and bcoe committed Jan 16, 2017
1 parent 7931652 commit bd1472b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -1382,7 +1382,7 @@ var yargs = require("yargs")(['--help'])

Later on, `argv` can be retrieved with `yargs.argv`.

.implies(x, y)
<a name="implies"></a>.implies(x, y)
--------------

Given the key `x` is set, it is required that the key `y` is set.
Expand Down Expand Up @@ -1578,13 +1578,15 @@ Valid `opt` keys include:
- `coerce`: function, coerce or transform parsed command line values into another value, see [`coerce()`](#coerce)
- `config`: boolean, interpret option as a path to a JSON config file, see [`config()`](#config)
- `configParser`: function, provide a custom config parsing function, see [`config()`](#config)
- `conflicts`: string or object, require certain keys not to be set, see [`conflicts()`](#conflicts)
- `count`: boolean, interpret option as a count of boolean flags, see [`count()`](#count)
- `default`: value, set a default value for the option, see [`default()`](#default)
- `defaultDescription`: string, use this description for the default value in help content, see [`default()`](#default)
- `demandOption`: boolean or string, demand the option be given, with optional error message, see [`demandOption()`](#demandOption)
- `desc`/`describe`/`description`: string, the option description for help content, see [`describe()`](#describe)
- `global`: boolean, indicate that this key should not be [reset](#reset) when a command is invoked, see [`global()`](#global)
- `group`: string, when displaying usage instructions place the option under an alternative group heading, see [`group()`](#group)
- `implies`: string or object, require certain keys to be set, see [`implies()`](#implies)
- `nargs`: number, specify how many arguments should be consumed for the option, see [`nargs()`](#nargs)
- `normalize`: boolean, apply `path.normalize()` to the option, see [`normalize()`](#normalize)
- `number`: boolean, interpret option as a number, [`number()`](#number)
Expand Down
24 changes: 24 additions & 0 deletions test/validation.js
Expand Up @@ -124,6 +124,18 @@ describe('validation tests', function () {
expect(argv.noFoo).to.not.exist
expect(argv.foo).to.not.exist
})

it('allows key to be specified with option shorthand', function (done) {
yargs('--bar')
.option('bar', {
implies: 'foo'
})
.fail(function (msg) {
msg.should.match(/Implications failed/)
return done()
})
.argv
})
})

describe('conflicts', function () {
Expand Down Expand Up @@ -172,6 +184,18 @@ describe('validation tests', function () {
})
.argv
})

it('allows key to be specified with option shorthand', function (done) {
yargs(['-f', '-b'])
.option('f', {
conflicts: 'b'
})
.fail(function (msg) {
msg.should.equal('Arguments f and b are mutually exclusive')
return done()
})
.argv
})
})

describe('demand', function () {
Expand Down
8 changes: 8 additions & 0 deletions yargs.js
Expand Up @@ -535,10 +535,18 @@ function Yargs (processArgs, cwd, parentRequire) {
self.config(key, opt.configParser)
}

if ('conflicts' in opt) {
self.conflicts(key, opt.conflicts)
}

if ('default' in opt) {
self.default(key, opt.default)
}

if ('implies' in opt) {
self.implies(key, opt.implies)
}

if ('nargs' in opt) {
self.nargs(key, opt.nargs)
}
Expand Down

0 comments on commit bd1472b

Please sign in to comment.