Skip to content

Commit

Permalink
feat: support single dash as option value for long option
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspawn committed Apr 15, 2023
1 parent 3aba24c commit 877c819
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/yargs-parser.ts
Expand Up @@ -273,7 +273,7 @@ export class YargsParser {
} else {
next = args[i + 1]

if (next !== undefined && (!next.match(/^-/) ||
if (next !== undefined && (!next.match(/^-./) ||
next.match(negative)) &&
!checkAllAliases(key, flags.bools) &&
!checkAllAliases(key, flags.counts)) {
Expand Down
26 changes: 23 additions & 3 deletions test/yargs-parser.cjs
Expand Up @@ -1487,23 +1487,31 @@ describe('yargs-parser', function () {
})

describe('-', function () {
it('should set - as value of n', function () {
it('should set - as space separated value of n', function () {
const argv = parser(['-n', '-'])
argv.should.have.property('n', '-')
argv.should.have.property('_').with.length(0)
})

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

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

it('should set - as an embedded value of f with =', function () {
// usual style for embedded short option value
const argv = parser(['-f=-'])
argv.should.have.property('f', '-')
argv.should.have.property('_').with.length(0)
})

it('should set b to true and set - as a non-hyphenated value when b is set as a boolean', function () {
const argv = parser(['-b', '-'], {
boolean: ['b']
Expand All @@ -1521,6 +1529,18 @@ describe('yargs-parser', function () {
argv.should.have.property('s', '-')
argv.should.have.property('_').with.length(0)
})

it('should set - as space separated value of foo', function () {
const argv = parser(['--foo', '-'])
argv.should.have.property('foo', '-')
argv.should.have.property('_').with.length(0)
})

it('should set - as embedded value of foo', function () {
const argv = parser(['--foo=-'])
argv.should.have.property('foo', '-')
argv.should.have.property('_').with.length(0)
})
})

describe('count', function () {
Expand Down

0 comments on commit 877c819

Please sign in to comment.