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

Add option shorthand for conflicts / implies #753

Merged
merged 2 commits into from Jan 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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