Skip to content

Commit

Permalink
feat: make opts object optional for .option() (yargs#624)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrimue authored and bcoe committed Sep 26, 2016
1 parent c301f42 commit 4f29de6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
13 changes: 7 additions & 6 deletions README.md
Expand Up @@ -1082,8 +1082,8 @@ For example:

```js
require('yargs')
.option('foobar', {})
.option('foobaz', {})
.option('foobar')
.option('foobaz')
.completion()
.getCompletion(['./test.js', '--foo'], function (completions) {
console.log(completions)
Expand Down Expand Up @@ -1310,13 +1310,14 @@ var argv = require('yargs')
.argv
```

.option(key, opt)
.option(key, [opt])
-----------------
.options(key, opt)
.options(key, [opt])
------------------

Instead of chaining together `.alias().demand().default().describe().string()`, you can specify
keys in `opt` for each of the chainable methods.
This method can be used to make yargs aware of options that _could_
exist. You can also pass an `opt` object which can hold further
customization, like `.alias()`, `.demand()` etc. for that option.

For example:

Expand Down
10 changes: 10 additions & 0 deletions test/yargs.js
Expand Up @@ -124,6 +124,16 @@ describe('yargs dsl tests', function () {
})
})

it('should not require config object for an option', function () {
var r = checkOutput(function () {
return yargs([])
.option('x')
.argv
})

expect(r.errors).to.deep.equal([])
})

describe('showHelpOnFail', function () {
it('should display custom failure message, if string is provided as first argument', function () {
var r = checkOutput(function () {
Expand Down
5 changes: 3 additions & 2 deletions yargs.js
@@ -1,4 +1,3 @@
const assert = require('assert')
const Command = require('./lib/command')
const Completion = require('./lib/completion')
const Parser = require('yargs-parser')
Expand Down Expand Up @@ -394,7 +393,9 @@ function Yargs (processArgs, cwd, parentRequire) {
self.options(k, key[k])
})
} else {
assert(typeof opt === 'object', 'second argument to option must be an object')
if (typeof opt !== 'object') {
opt = {}
}

options.key[key] = true // track manually set keys.

Expand Down

0 comments on commit 4f29de6

Please sign in to comment.