Skip to content

Commit b1a0d98

Browse files
committed
feat: support single dash as option value for long option
1 parent 60f2db5 commit b1a0d98

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

lib/yargs-parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ export class YargsParser {
274274
} else {
275275
next = args[i + 1]
276276

277-
if (next !== undefined && (!next.match(/^-/) ||
277+
if (next !== undefined && (!next.match(/^-./) ||
278278
next.match(negative)) &&
279279
!checkAllAliases(key, flags.bools) &&
280280
!checkAllAliases(key, flags.counts)) {

test/yargs-parser.mjs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,23 +1493,31 @@ describe('yargs-parser', function () {
14931493
})
14941494

14951495
describe('-', function () {
1496-
it('should set - as value of n', function () {
1496+
it('should set - as space separated value of n', function () {
14971497
const argv = parser(['-n', '-'])
14981498
argv.should.have.property('n', '-')
14991499
argv.should.have.property('_').with.length(0)
15001500
})
15011501

1502-
it('should set - as a non-hyphenated value', function () {
1502+
it('should set - as a non-hyphenated value (positional)', function () {
15031503
const argv = parser(['-'])
15041504
argv.should.have.property('_').and.deep.equal(['-'])
15051505
})
15061506

1507-
it('should set - as a value of f', function () {
1507+
it('should set - as an embedded value of f', function () {
1508+
// special case dash trailing short option
15081509
const argv = parser(['-f-'])
15091510
argv.should.have.property('f', '-')
15101511
argv.should.have.property('_').with.length(0)
15111512
})
15121513

1514+
it('should set - as an embedded value of f with =', function () {
1515+
// usual style for embedded short option value
1516+
const argv = parser(['-f=-'])
1517+
argv.should.have.property('f', '-')
1518+
argv.should.have.property('_').with.length(0)
1519+
})
1520+
15131521
it('should set b to true and set - as a non-hyphenated value when b is set as a boolean', function () {
15141522
const argv = parser(['-b', '-'], {
15151523
boolean: ['b']
@@ -1527,6 +1535,18 @@ describe('yargs-parser', function () {
15271535
argv.should.have.property('s', '-')
15281536
argv.should.have.property('_').with.length(0)
15291537
})
1538+
1539+
it('should set - as space separated value of foo', function () {
1540+
const argv = parser(['--foo', '-'])
1541+
argv.should.have.property('foo', '-')
1542+
argv.should.have.property('_').with.length(0)
1543+
})
1544+
1545+
it('should set - as embedded value of foo', function () {
1546+
const argv = parser(['--foo=-'])
1547+
argv.should.have.property('foo', '-')
1548+
argv.should.have.property('_').with.length(0)
1549+
})
15301550
})
15311551

15321552
describe('count', function () {

0 commit comments

Comments
 (0)