Skip to content

Commit bd1472b

Browse files
vseventerbcoe
authored andcommitted
feat: add conflicts and implies shorthands. (#753)
1 parent 7931652 commit bd1472b

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1382,7 +1382,7 @@ var yargs = require("yargs")(['--help'])
13821382

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

1385-
.implies(x, y)
1385+
<a name="implies"></a>.implies(x, y)
13861386
--------------
13871387

13881388
Given the key `x` is set, it is required that the key `y` is set.
@@ -1578,13 +1578,15 @@ Valid `opt` keys include:
15781578
- `coerce`: function, coerce or transform parsed command line values into another value, see [`coerce()`](#coerce)
15791579
- `config`: boolean, interpret option as a path to a JSON config file, see [`config()`](#config)
15801580
- `configParser`: function, provide a custom config parsing function, see [`config()`](#config)
1581+
- `conflicts`: string or object, require certain keys not to be set, see [`conflicts()`](#conflicts)
15811582
- `count`: boolean, interpret option as a count of boolean flags, see [`count()`](#count)
15821583
- `default`: value, set a default value for the option, see [`default()`](#default)
15831584
- `defaultDescription`: string, use this description for the default value in help content, see [`default()`](#default)
15841585
- `demandOption`: boolean or string, demand the option be given, with optional error message, see [`demandOption()`](#demandOption)
15851586
- `desc`/`describe`/`description`: string, the option description for help content, see [`describe()`](#describe)
15861587
- `global`: boolean, indicate that this key should not be [reset](#reset) when a command is invoked, see [`global()`](#global)
15871588
- `group`: string, when displaying usage instructions place the option under an alternative group heading, see [`group()`](#group)
1589+
- `implies`: string or object, require certain keys to be set, see [`implies()`](#implies)
15881590
- `nargs`: number, specify how many arguments should be consumed for the option, see [`nargs()`](#nargs)
15891591
- `normalize`: boolean, apply `path.normalize()` to the option, see [`normalize()`](#normalize)
15901592
- `number`: boolean, interpret option as a number, [`number()`](#number)

test/validation.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,18 @@ describe('validation tests', function () {
124124
expect(argv.noFoo).to.not.exist
125125
expect(argv.foo).to.not.exist
126126
})
127+
128+
it('allows key to be specified with option shorthand', function (done) {
129+
yargs('--bar')
130+
.option('bar', {
131+
implies: 'foo'
132+
})
133+
.fail(function (msg) {
134+
msg.should.match(/Implications failed/)
135+
return done()
136+
})
137+
.argv
138+
})
127139
})
128140

129141
describe('conflicts', function () {
@@ -172,6 +184,18 @@ describe('validation tests', function () {
172184
})
173185
.argv
174186
})
187+
188+
it('allows key to be specified with option shorthand', function (done) {
189+
yargs(['-f', '-b'])
190+
.option('f', {
191+
conflicts: 'b'
192+
})
193+
.fail(function (msg) {
194+
msg.should.equal('Arguments f and b are mutually exclusive')
195+
return done()
196+
})
197+
.argv
198+
})
175199
})
176200

177201
describe('demand', function () {

yargs.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,10 +535,18 @@ function Yargs (processArgs, cwd, parentRequire) {
535535
self.config(key, opt.configParser)
536536
}
537537

538+
if ('conflicts' in opt) {
539+
self.conflicts(key, opt.conflicts)
540+
}
541+
538542
if ('default' in opt) {
539543
self.default(key, opt.default)
540544
}
541545

546+
if ('implies' in opt) {
547+
self.implies(key, opt.implies)
548+
}
549+
542550
if ('nargs' in opt) {
543551
self.nargs(key, opt.nargs)
544552
}

0 commit comments

Comments
 (0)