Skip to content

Commit

Permalink
Merge pull request #387 from bcoe/number-option-shorthand
Browse files Browse the repository at this point in the history
number shot-hand for options
  • Loading branch information
bcoe committed Feb 15, 2016
2 parents 8087285 + 6a860d0 commit 0896aee
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,7 @@ Valid `opt` keys include:
- `'boolean'`: synonymous for `boolean: true`, see [`boolean()`](#boolean)
- `'count'`: synonymous for `count: true`, see [`count()`](#count)
- `'string'`: synonymous for `string: true`, see [`string()`](#string)
- `'number'`: synonymous for `number: true`, see [`number()`](#number)

.parse(args)
------------
Expand Down
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,9 @@ function Argv (processArgs, cwd) {
} if (opt.array || opt.type === 'array') {
self.array(key)
if (opt.alias) self.array(opt.alias)
} if (opt.number || opt.type === 'number') {
self.number(key)
if (opt.alias) self.number(opt.alias)
} if (opt.string || opt.type === 'string') {
self.string(key)
if (opt.alias) self.string(opt.alias)
Expand Down
17 changes: 15 additions & 2 deletions test/yargs.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,23 @@ describe('yargs dsl tests', function () {

describe('number', function () {
it('accepts number arguments when a number type is specified', function () {
var argv = yargs('-w 10')
var argv = yargs('-w banana')
.number('w')
.argv
argv.w.should.equal(10)

expect(typeof argv.w).to.equal('number')
})

it('should expose an options short-hand for numbers', function () {
var argv = yargs('-w banana')
.option('w', {
number: true
})
.alias('w', 'x')
.argv

expect(typeof argv.w).to.equal('number')
expect(typeof argv.x).to.equal('number')
})
})

Expand Down

0 comments on commit 0896aee

Please sign in to comment.