Skip to content

Commit 2e43692

Browse files
authored
fix: better parsing of negative values (#44)
1 parent 7b49cd2 commit 2e43692

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ function parse (args, opts) {
4040
nargs: {},
4141
coercions: {}
4242
}
43+
var negative = /^-[0-9]+(\.[0-9]+)?/
4344

4445
;[].concat(opts.array).filter(Boolean).forEach(function (key) {
4546
flags.arrays[key] = true
@@ -155,7 +156,8 @@ function parse (args, opts) {
155156
} else {
156157
next = args[i + 1]
157158

158-
if (next !== undefined && !next.match(/^-/) &&
159+
if (next !== undefined && (!next.match(/^-/) ||
160+
next.match(negative)) &&
159161
!checkAllAliases(key, flags.bools) &&
160162
!checkAllAliases(key, flags.counts)) {
161163
setArg(key, next)
@@ -186,7 +188,7 @@ function parse (args, opts) {
186188
} else {
187189
setArg(key, defaultForType(guessType(key, flags)))
188190
}
189-
} else if (arg.match(/^-[^-]+/)) {
191+
} else if (arg.match(/^-[^-]+/) && !arg.match(negative)) {
190192
letters = arg.slice(1, -1).split('')
191193
broken = false
192194

@@ -247,7 +249,8 @@ function parse (args, opts) {
247249
} else {
248250
next = args[i + 1]
249251

250-
if (next !== undefined && !/^(-|--)[^-]/.test(next) &&
252+
if (next !== undefined && (!/^(-|--)[^-]/.test(next) ||
253+
next.match(negative)) &&
251254
!checkAllAliases(key, flags.bools) &&
252255
!checkAllAliases(key, flags.counts)) {
253256
setArg(key, next)
@@ -308,7 +311,7 @@ function parse (args, opts) {
308311
function eatArray (i, key, args) {
309312
var start = i + 1
310313
for (var ii = i + 1; ii < args.length; ii++) {
311-
if (/^-/.test(args[ii])) {
314+
if (/^-/.test(args[ii]) && !negative.test(args[ii])) {
312315
if (ii === start) {
313316
setArg(key, defaultForType('array'))
314317
}

test/yargs-parser.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,32 @@ describe('yargs-parser', function () {
127127
argv._[0].should.be.a('number')
128128
})
129129

130+
// addresses: https://github.com/yargs/yargs-parser/issues/33
131+
it('should handle parsing negative #s', function () {
132+
var argv = parser([
133+
'-33', '-177', '33',
134+
'--n1', '-33',
135+
'-n', '-44',
136+
'--n2=-55',
137+
'--foo.bar', '-33',
138+
'-o=-55',
139+
'--bounds', '-180', '99', '-180', '90',
140+
'--other', '-99', '-220'
141+
], {
142+
array: 'bounds',
143+
narg: {'other': 2}
144+
})
145+
146+
argv._.should.deep.equal([-33, -177, 33])
147+
argv.n1.should.equal(-33)
148+
argv.n.should.equal(-44)
149+
argv.n2.should.equal(-55)
150+
argv.foo.bar.should.equal(-33)
151+
argv.o.should.equal(-55)
152+
argv.bounds.should.deep.equal([-180, 99, -180, 90])
153+
argv.other.should.deep.equal([-99, -220])
154+
})
155+
130156
it('should set the value of a single short option to the next supplied value, even if the value is empty', function () {
131157
var parse = parser(['-p', ''])
132158
parse.should.have.property('p', '')
@@ -901,13 +927,6 @@ describe('yargs-parser', function () {
901927
argv.should.have.property('n', 123)
902928
argv.should.have.property('_').with.length(0)
903929
})
904-
905-
it('should set option "1" to true, option "2" to true, and option "3" to numeric value 456', function () {
906-
var argv = parser([ '-123', '456' ])
907-
argv.should.have.property('1', true)
908-
argv.should.have.property('2', true)
909-
argv.should.have.property('3', 456)
910-
})
911930
})
912931

913932
describe('whitespace', function () {

0 commit comments

Comments
 (0)