Skip to content

Commit 877c819

Browse files
committed
feat: support single dash as option value for long option
1 parent 3aba24c commit 877c819

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
@@ -273,7 +273,7 @@ export class YargsParser {
273273
} else {
274274
next = args[i + 1]
275275

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

test/yargs-parser.cjs

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

14891489
describe('-', function () {
1490-
it('should set - as value of n', function () {
1490+
it('should set - as space separated value of n', function () {
14911491
const argv = parser(['-n', '-'])
14921492
argv.should.have.property('n', '-')
14931493
argv.should.have.property('_').with.length(0)
14941494
})
14951495

1496-
it('should set - as a non-hyphenated value', function () {
1496+
it('should set - as a non-hyphenated value (positional)', function () {
14971497
const argv = parser(['-'])
14981498
argv.should.have.property('_').and.deep.equal(['-'])
14991499
})
15001500

1501-
it('should set - as a value of f', function () {
1501+
it('should set - as an embedded value of f', function () {
1502+
// special case dash trailing short option
15021503
const argv = parser(['-f-'])
15031504
argv.should.have.property('f', '-')
15041505
argv.should.have.property('_').with.length(0)
15051506
})
15061507

1508+
it('should set - as an embedded value of f with =', function () {
1509+
// usual style for embedded short option value
1510+
const argv = parser(['-f=-'])
1511+
argv.should.have.property('f', '-')
1512+
argv.should.have.property('_').with.length(0)
1513+
})
1514+
15071515
it('should set b to true and set - as a non-hyphenated value when b is set as a boolean', function () {
15081516
const argv = parser(['-b', '-'], {
15091517
boolean: ['b']
@@ -1521,6 +1529,18 @@ describe('yargs-parser', function () {
15211529
argv.should.have.property('s', '-')
15221530
argv.should.have.property('_').with.length(0)
15231531
})
1532+
1533+
it('should set - as space separated value of foo', function () {
1534+
const argv = parser(['--foo', '-'])
1535+
argv.should.have.property('foo', '-')
1536+
argv.should.have.property('_').with.length(0)
1537+
})
1538+
1539+
it('should set - as embedded value of foo', function () {
1540+
const argv = parser(['--foo=-'])
1541+
argv.should.have.property('foo', '-')
1542+
argv.should.have.property('_').with.length(0)
1543+
})
15241544
})
15251545

15261546
describe('count', function () {

0 commit comments

Comments
 (0)