Skip to content

Commit f743236

Browse files
elas7bcoe
authored andcommitted
fix: parsing issue with numeric character in group of options (#19)
BREAKING CHANGE: subtle change to how values are parsed in a group of single-character arguments.
1 parent 3976d66 commit f743236

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,9 @@ function parse (args, opts) {
213213
continue
214214
}
215215

216+
// current letter is an alphabetic character and next value is a number
216217
if (/[A-Za-z]/.test(letters[j]) &&
217-
/-?\d+(\.\d*)?(e-?\d+)?$/.test(next)) {
218+
/^-?\d+(\.\d*)?(e-?\d+)?$/.test(next)) {
218219
setArg(letters[j], next)
219220
broken = true
220221
break

test/yargs-parser.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,29 @@ describe('yargs-parser', function () {
879879
argv.should.have.property('n', 123)
880880
})
881881

882+
it('should set n to the numeric value 123, with n at the end of a group', function () {
883+
var argv = parser([ '-ab5n123' ])
884+
argv.should.have.property('a', true)
885+
argv.should.have.property('b', true)
886+
argv.should.have.property('5', true)
887+
argv.should.have.property('n', 123)
888+
argv.should.have.property('_').with.length(0)
889+
})
890+
891+
it('should set n to the numeric value 123, with = as separator', function () {
892+
var argv = parser([ '-n=123' ])
893+
argv.should.have.property('n', 123)
894+
})
895+
896+
it('should set n to the numeric value 123, with n at the end of a group and = as separator', function () {
897+
var argv = parser([ '-ab5n=123' ])
898+
argv.should.have.property('a', true)
899+
argv.should.have.property('b', true)
900+
argv.should.have.property('5', true)
901+
argv.should.have.property('n', 123)
902+
argv.should.have.property('_').with.length(0)
903+
})
904+
882905
it('should set option "1" to true, option "2" to true, and option "3" to numeric value 456', function () {
883906
var argv = parser([ '-123', '456' ])
884907
argv.should.have.property('1', true)

0 commit comments

Comments
 (0)